@Test public void testSaveLoadedWithNonEmptyProjectAndNonEmptyDataMap() throws Exception { Logger log = mock(Logger.class); Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule()); DbImportConfiguration params = mock(DbImportConfiguration.class); URL projectURL = new URL(getPackageURL(), "dbimport/cayenne-testProject4.map.xml"); File projectFile = new File(projectURL.toURI()); projectFile.delete(); assertFalse(projectFile.exists()); Files.write(projectFile.toPath(), ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<domain xmlns=\"http: "\t xmlns:xsi=\"http: "\t xsi:schemaLocation=\"http: "\t project-version=\"10\">\n" + "\t<map name=\"testSaveLoaded4\"/>\n" + "</domain>").getBytes(Charset.forName("UTF-8"))); assertTrue(projectFile.isFile()); when(params.getCayenneProject()).thenReturn(projectFile); DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class); URL dataMapURL = new URL(getPackageURL(), "dbimport/testSaveLoaded4.map.xml"); File dataMapFile = new File(dataMapURL.toURI()); dataMapFile.delete(); assertFalse(dataMapFile.exists()); Files.write(dataMapFile.toPath(), ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<data-map xmlns=\"http: "\t xmlns:xsi=\"http: "\t xsi:schemaLocation=\"http: "\t project-version=\"10\">\n" + "\t<db-entity name=\"test\">\n" + "\t\t<db-attribute name=\"test\" type=\"INT\"/>\n" + "\t</db-entity>\n" + "</data-map>").getBytes(Charset.forName("UTF-8"))); assertTrue(dataMapFile.isFile()); DataMap map = new DataMap("testSaveLoaded4"); map.setConfigurationSource(new URLResource(dataMapURL)); action.saveLoaded(map, params); assertTrue(dataMapFile.isFile()); assertTrue(projectFile.isFile()); String dataMapContents = Util.stringFromFile(dataMapFile); assertTrue("Has no project version saved", dataMapContents.contains("project-version=\"")); assertFalse(dataMapContents.contains("<db-entity")); String projectContents = Util.stringFromFile(projectFile); assertTrue("Has no project version saved", projectContents.contains("project-version=\"")); assertEquals("Has no or too many datamaps in project", 1, Util.countMatches(projectContents, "<map name=\"testSaveLoaded4\"/>")); }
protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException { ConfigurationTree<ConfigurationNode> projectRoot; if(config.getCayenneProject() == null) { projectRoot = new ConfigurationTree<>(dataMap); } else { DataChannelDescriptor dataChannelDescriptor; if(config.getCayenneProject().exists()) { URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL()); ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource); if(!configurationTree.getLoadFailures().isEmpty()) { throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription()); } dataChannelDescriptor = configurationTree.getRootNode(); DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName()); if(oldDataMap != null) { dataChannelDescriptor.getDataMaps().remove(oldDataMap); } } else { dataChannelDescriptor = new DataChannelDescriptor(); dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName())); dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL())); logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath()); } dataChannelDescriptor.getDataMaps().add(dataMap); projectRoot = new ConfigurationTree<>(dataChannelDescriptor); } Project project = new Project(projectRoot); projectSaver.save(project); logger.info(""); logger.info("All changes saved."); }
DefaultDbImportAction implements DbImportAction { protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException { ConfigurationTree<ConfigurationNode> projectRoot; if(config.getCayenneProject() == null) { projectRoot = new ConfigurationTree<>(dataMap); } else { DataChannelDescriptor dataChannelDescriptor; if(config.getCayenneProject().exists()) { URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL()); ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource); if(!configurationTree.getLoadFailures().isEmpty()) { throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription()); } dataChannelDescriptor = configurationTree.getRootNode(); DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName()); if(oldDataMap != null) { dataChannelDescriptor.getDataMaps().remove(oldDataMap); } } else { dataChannelDescriptor = new DataChannelDescriptor(); dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName())); dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL())); logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath()); } dataChannelDescriptor.getDataMaps().add(dataMap); projectRoot = new ConfigurationTree<>(dataChannelDescriptor); } Project project = new Project(projectRoot); projectSaver.save(project); logger.info(""); logger.info("All changes saved."); } }
DefaultDbImportAction implements DbImportAction { protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException { ConfigurationTree<ConfigurationNode> projectRoot; if(config.getCayenneProject() == null) { projectRoot = new ConfigurationTree<>(dataMap); } else { DataChannelDescriptor dataChannelDescriptor; if(config.getCayenneProject().exists()) { URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL()); ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource); if(!configurationTree.getLoadFailures().isEmpty()) { throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription()); } dataChannelDescriptor = configurationTree.getRootNode(); DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName()); if(oldDataMap != null) { dataChannelDescriptor.getDataMaps().remove(oldDataMap); } } else { dataChannelDescriptor = new DataChannelDescriptor(); dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName())); dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL())); logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath()); } dataChannelDescriptor.getDataMaps().add(dataMap); projectRoot = new ConfigurationTree<>(dataChannelDescriptor); } Project project = new Project(projectRoot); projectSaver.save(project); logger.info(""); logger.info("All changes saved."); } DefaultDbImportAction(@Inject Logger logger,
                                 @Inject ProjectSaver projectSaver,
                                 @Inject DataSourceFactory dataSourceFactory,
                                 @Inject DbAdapterFactory adapterFactory,
                                 @Inject DataMapLoader mapLoader,
                                 @Inject MergerTokenFactoryProvider mergerTokenFactoryProvider,
                                 @Inject DataChannelDescriptorLoader dataChannelDescriptorLoader,
                                 @Inject DataChannelMetaData metaData); }
DefaultDbImportAction implements DbImportAction { protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException { ConfigurationTree<ConfigurationNode> projectRoot; if(config.getCayenneProject() == null) { projectRoot = new ConfigurationTree<>(dataMap); } else { DataChannelDescriptor dataChannelDescriptor; if(config.getCayenneProject().exists()) { URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL()); ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource); if(!configurationTree.getLoadFailures().isEmpty()) { throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription()); } dataChannelDescriptor = configurationTree.getRootNode(); DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName()); if(oldDataMap != null) { dataChannelDescriptor.getDataMaps().remove(oldDataMap); } } else { dataChannelDescriptor = new DataChannelDescriptor(); dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName())); dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL())); logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath()); } dataChannelDescriptor.getDataMaps().add(dataMap); projectRoot = new ConfigurationTree<>(dataChannelDescriptor); } Project project = new Project(projectRoot); projectSaver.save(project); logger.info(""); logger.info("All changes saved."); } DefaultDbImportAction(@Inject Logger logger,
                                 @Inject ProjectSaver projectSaver,
                                 @Inject DataSourceFactory dataSourceFactory,
                                 @Inject DbAdapterFactory adapterFactory,
                                 @Inject DataMapLoader mapLoader,
                                 @Inject MergerTokenFactoryProvider mergerTokenFactoryProvider,
                                 @Inject DataChannelDescriptorLoader dataChannelDescriptorLoader,
                                 @Inject DataChannelMetaData metaData); static void flattenManyToManyRelationships(DataMap map, Collection<ObjEntity> loadedObjEntities,
                                                         ObjectNameGenerator objectNameGenerator); @Override void execute(DbImportConfiguration config); boolean hasTokensToImport(Collection<MergerToken> tokens); }
DefaultDbImportAction implements DbImportAction { protected void saveLoaded(DataMap dataMap, DbImportConfiguration config) throws MalformedURLException { ConfigurationTree<ConfigurationNode> projectRoot; if(config.getCayenneProject() == null) { projectRoot = new ConfigurationTree<>(dataMap); } else { DataChannelDescriptor dataChannelDescriptor; if(config.getCayenneProject().exists()) { URLResource configurationResource = new URLResource(config.getCayenneProject().toURI().toURL()); ConfigurationTree<DataChannelDescriptor> configurationTree = dataChannelDescriptorLoader.load(configurationResource); if(!configurationTree.getLoadFailures().isEmpty()) { throw new CayenneRuntimeException("Unable to load cayenne project %s, %s", config.getCayenneProject(), configurationTree.getLoadFailures().iterator().next().getDescription()); } dataChannelDescriptor = configurationTree.getRootNode(); DataMap oldDataMap = dataChannelDescriptor.getDataMap(dataMap.getName()); if(oldDataMap != null) { dataChannelDescriptor.getDataMaps().remove(oldDataMap); } } else { dataChannelDescriptor = new DataChannelDescriptor(); dataChannelDescriptor.setName(getProjectNameFromFileName(config.getCayenneProject().getName())); dataChannelDescriptor.setConfigurationSource(new URLResource(config.getCayenneProject().toURI().toURL())); logger.info("Project file does not exist. New project will be saved into '" + config.getCayenneProject().getAbsolutePath()); } dataChannelDescriptor.getDataMaps().add(dataMap); projectRoot = new ConfigurationTree<>(dataChannelDescriptor); } Project project = new Project(projectRoot); projectSaver.save(project); logger.info(""); logger.info("All changes saved."); } DefaultDbImportAction(@Inject Logger logger,
                                 @Inject ProjectSaver projectSaver,
                                 @Inject DataSourceFactory dataSourceFactory,
                                 @Inject DbAdapterFactory adapterFactory,
                                 @Inject DataMapLoader mapLoader,
                                 @Inject MergerTokenFactoryProvider mergerTokenFactoryProvider,
                                 @Inject DataChannelDescriptorLoader dataChannelDescriptorLoader,
                                 @Inject DataChannelMetaData metaData); static void flattenManyToManyRelationships(DataMap map, Collection<ObjEntity> loadedObjEntities,
                                                         ObjectNameGenerator objectNameGenerator); @Override void execute(DbImportConfiguration config); boolean hasTokensToImport(Collection<MergerToken> tokens);  }