Search in sources :

Example 36 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class XMLDataMapLoaderTest method setUp.

@Before
public void setUp() throws Exception {
    Module testModule = binder -> {
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(DataMapLoader.class).to(XMLDataMapLoader.class);
        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
        binder.bind(HandlerFactory.class).to(DefaultHandlerFactory.class);
        binder.bind(DataChannelMetaData.class).to(NoopDataChannelMetaData.class);
        binder.bind(XMLReader.class).toProviderInstance(new XMLReaderProvider(false)).withoutScope();
    };
    injector = DIBootstrap.createInjector(testModule);
    loader = injector.getInstance(DataMapLoader.class);
}
Also used : Injector(org.apache.cayenne.di.Injector) URLResource(org.apache.cayenne.resource.URLResource) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) SQLTemplateDescriptor(org.apache.cayenne.map.SQLTemplateDescriptor) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DbAttribute(org.apache.cayenne.map.DbAttribute) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLReader(org.xml.sax.XMLReader) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) DataMap(org.apache.cayenne.map.DataMap) Before(org.junit.Before) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) XMLReader(org.xml.sax.XMLReader) Before(org.junit.Before)

Example 37 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class ToolsModuleTest method testModuleContents.

@Test
public void testModuleContents() {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new ToolsModule(log));
    assertSame(log, i.getInstance(Logger.class));
    assertTrue(i.getInstance(DataSourceFactory.class) instanceof DriverDataSourceFactory);
    assertTrue(i.getInstance(AdhocObjectFactory.class) instanceof DefaultAdhocObjectFactory);
    assertTrue(i.getInstance(DbAdapterFactory.class) instanceof DefaultDbAdapterFactory);
}
Also used : DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Injector(org.apache.cayenne.di.Injector) DefaultDbAdapterFactory(org.apache.cayenne.configuration.server.DefaultDbAdapterFactory) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 38 with Injector

use of org.apache.cayenne.di.Injector in project cayenne by apache.

the class ToolsModuleTest method testDbApdater.

@Test
public void testDbApdater() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new ToolsModule(log));
    DbAdapterFactory factory = i.getInstance(DbAdapterFactory.class);
    DataNodeDescriptor nodeDescriptor = mock(DataNodeDescriptor.class);
    DataSource dataSource = mock(DataSource.class);
    assertTrue(factory.createAdapter(nodeDescriptor, dataSource) instanceof AutoAdapter);
}
Also used : DefaultDbAdapterFactory(org.apache.cayenne.configuration.server.DefaultDbAdapterFactory) DbAdapterFactory(org.apache.cayenne.configuration.server.DbAdapterFactory) Injector(org.apache.cayenne.di.Injector) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Logger(org.slf4j.Logger) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 39 with Injector

use of org.apache.cayenne.di.Injector 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 40 with Injector

use of org.apache.cayenne.di.Injector 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)

Aggregations

Injector (org.apache.cayenne.di.Injector)64 Test (org.junit.Test)52 Module (org.apache.cayenne.di.Module)27 DIBootstrap (org.apache.cayenne.di.DIBootstrap)23 URL (java.net.URL)15 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)15 DataMap (org.apache.cayenne.map.DataMap)15 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)14 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)14 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)14 Slf4jJdbcEventLogger (org.apache.cayenne.log.Slf4jJdbcEventLogger)14 URLResource (org.apache.cayenne.resource.URLResource)14 Assert (org.junit.Assert)14 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)13 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)12 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)12 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)12 Logger (org.slf4j.Logger)11 Constants (org.apache.cayenne.configuration.Constants)10