Search in sources :

Example 11 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DefaultDbImportActionTest method testSaveLoadedWithNonEmptyProject.

@Test
public void testSaveLoadedWithNonEmptyProject() 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-testProject3.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://cayenne.apache.org/schema/10/domain\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/domain http://cayenne.apache.org/schema/10/domain.xsd\"\n" + "\t project-version=\"10\">\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/testSaveLoaded3.map.xml");
    File dataMapFile = new File(dataMapURL.toURI());
    dataMapFile.delete();
    assertFalse(dataMapFile.exists());
    DataMap map = new DataMap("testSaveLoaded3");
    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=\""));
    String projectContents = Util.stringFromFile(projectFile);
    assertTrue("Has no project version saved", projectContents.contains("project-version=\""));
    assertTrue("Has no datamap in project", projectContents.contains("<map name=\"testSaveLoaded3\"/>"));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) Test(org.junit.Test)

Example 12 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DefaultDbImportActionTest method testSaveLoadedWithNonEmptyProjectAndNonEmptyDataMap.

@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://cayenne.apache.org/schema/10/domain\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/domain http://cayenne.apache.org/schema/10/domain.xsd\"\n" + "\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://cayenne.apache.org/schema/10/modelMap\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd\"\n" + "\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\"/>"));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) Test(org.junit.Test)

Example 13 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DefaultDbImportActionTest method testSaveLoadedWithEmptyProject.

@Test
public void testSaveLoadedWithEmptyProject() 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-testProject2.map.xml");
    File projectFile = new File(projectURL.toURI());
    projectFile.delete();
    assertFalse(projectFile.exists());
    when(params.getCayenneProject()).thenReturn(projectFile);
    DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
    URL dataMapURL = new URL(getPackageURL(), "dbimport/testSaveLoaded2.map.xml");
    File dataMapFile = new File(dataMapURL.toURI());
    dataMapFile.delete();
    assertFalse(dataMapFile.exists());
    DataMap map = new DataMap("testSaveLoaded2");
    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=\""));
    String projectContents = Util.stringFromFile(projectFile);
    assertTrue("Has no project version saved", projectContents.contains("project-version=\""));
    assertTrue("Has no datamap in project", projectContents.contains("<map name=\"testSaveLoaded2\"/>"));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) Test(org.junit.Test)

Example 14 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class CgenTask method generate.

@TaskAction
public void generate() {
    File dataMapFile = getDataMapFile();
    Injector injector = DIBootstrap.createInjector(new ToolsModule(LoggerFactory.getLogger(CgenTask.class)));
    CayenneGeneratorMapLoaderAction loaderAction = new CayenneGeneratorMapLoaderAction(injector);
    loaderAction.setMainDataMapFile(dataMapFile);
    CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction();
    filterAction.setClient(client);
    filterAction.setNameFilter(NamePatternMatcher.build(getLogger(), includeEntities, excludeEntities));
    try {
        loaderAction.setAdditionalDataMapFiles(convertAdditionalDataMaps());
        ClassGenerationAction generator = this.createGenerator();
        DataMap dataMap = loaderAction.getMainDataMap();
        generator.setLogger(getLogger());
        if (this.force || getProject().hasProperty("force")) {
            generator.setForce(true);
        }
        generator.setTimestamp(dataMapFile.lastModified());
        generator.setDataMap(dataMap);
        generator.addEntities(filterAction.getFilteredEntities(dataMap));
        generator.addEmbeddables(dataMap.getEmbeddables());
        generator.addQueries(dataMap.getQueryDescriptors());
        generator.execute();
    } catch (Exception exception) {
        throw new GradleException("Error generating classes: ", exception);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) GradleException(org.gradle.api.GradleException) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) GradleException(org.gradle.api.GradleException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction) DataMap(org.apache.cayenne.map.DataMap) TaskAction(org.gradle.api.tasks.TaskAction)

Example 15 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DbGenerateTask method createDbAdapter.

DbAdapter createDbAdapter() {
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(getLogger()));
    AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
    return (adapter == null) ? objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : objectFactory.newInstance(DbAdapter.class, adapter);
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)

Aggregations

ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)16 Injector (org.apache.cayenne.di.Injector)15 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 Logger (org.slf4j.Logger)10 DataMap (org.apache.cayenne.map.DataMap)9 Test (org.junit.Test)6 File (java.io.File)5 URL (java.net.URL)4 DbAdapter (org.apache.cayenne.dba.DbAdapter)4 URLResource (org.apache.cayenne.resource.URLResource)4 DbGenerator (org.apache.cayenne.access.DbGenerator)3 DbImportAction (org.apache.cayenne.dbsync.reverse.dbimport.DbImportAction)3 DbImportConfigurationValidator (org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator)3 DbImportModule (org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule)3 ClassGenerationAction (org.apache.cayenne.gen.ClassGenerationAction)3 ClientClassGenerationAction (org.apache.cayenne.gen.ClientClassGenerationAction)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 BuildException (org.apache.tools.ant.BuildException)3 TaskAction (org.gradle.api.tasks.TaskAction)3