Search in sources :

Example 6 with Injector

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

the class CompatibilityDataChannelDescriptorLoaderIT method testLoad.

@Test
public void testLoad() throws Exception {
    Injector injector = getInjector();
    DataChannelDescriptorLoader loader = injector.getInstance(DataChannelDescriptorLoader.class);
    assertTrue(loader instanceof CompatibilityDataChannelDescriptorLoader);
    URL resourceUrl = getClass().getResource("../../project/compatibility/cayenne-project-v6.xml");
    Resource resource = new URLResource(resourceUrl);
    ConfigurationTree<DataChannelDescriptor> configurationTree = loader.load(resource);
    assertNotNull(configurationTree.getRootNode());
    assertTrue(configurationTree.getLoadFailures().isEmpty());
    assertEquals(1, configurationTree.getRootNode().getDataMaps().size());
    DataMap dataMap = configurationTree.getRootNode().getDataMaps().iterator().next();
    assertEquals(1, dataMap.getDbEntities().size());
    assertEquals(1, dataMap.getObjEntities().size());
    assertNotNull(dataMap.getObjEntity("Artist"));
    assertNotNull(dataMap.getDbEntity("Artist"));
    assertEquals(2, dataMap.getDbEntity("Artist").getAttributes().size());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) Injector(org.apache.cayenne.di.Injector) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 7 with Injector

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

the class CompatibilityUpgradeServiceIT method testUpgradeFullProjectDom.

@Test
public void testUpgradeFullProjectDom() throws Exception {
    Injector injector = getInjector();
    CompatibilityUpgradeService upgradeService = (CompatibilityUpgradeService) injector.getInstance(UpgradeService.class);
    DocumentProvider documentProvider = injector.getInstance(DocumentProvider.class);
    URL resourceUrl = getClass().getResource("cayenne-project-v6.xml");
    Resource resource = new URLResource(resourceUrl);
    upgradeService.upgradeProject(resource);
    Document domainDocument = documentProvider.getDocument(resourceUrl);
    assertNotNull(domainDocument);
    assertEquals("10", domainDocument.getDocumentElement().getAttribute("project-version"));
    URL dataMapUrl = getClass().getResource("test-map-v6.map.xml");
    Document dataMapDocument = documentProvider.getDocument(dataMapUrl);
    assertNotNull(dataMapDocument);
    assertEquals("10", dataMapDocument.getDocumentElement().getAttribute("project-version"));
    assertEquals(1, dataMapDocument.getElementsByTagName("obj-entity").getLength());
    assertEquals(1, dataMapDocument.getElementsByTagName("db-entity").getLength());
    assertEquals(2, dataMapDocument.getElementsByTagName("db-attribute").getLength());
}
Also used : URLResource(org.apache.cayenne.resource.URLResource) UpgradeService(org.apache.cayenne.project.upgrade.UpgradeService) Injector(org.apache.cayenne.di.Injector) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) Document(org.w3c.dom.Document) URL(java.net.URL) Test(org.junit.Test)

Example 8 with Injector

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

the class DataChannelProjectLoaderTest method testLoad.

@Test
public void testLoad() {
    DataChannelProjectLoader loader = new DataChannelProjectLoader();
    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(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.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 injector = DIBootstrap.createInjector(testModule);
    injector.injectMembers(loader);
    String testConfigName = "PROJECT1";
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
    Resource rootSource = new URLResource(url);
    Project project = loader.loadProject(rootSource);
    assertNotNull(project);
    DataChannelDescriptor rootNode = (DataChannelDescriptor) project.getRootNode();
    assertNotNull(rootNode);
    assertSame(rootSource, rootNode.getConfigurationSource());
    assertNotNull(project.getConfigurationResource());
    assertEquals(project.getConfigurationResource(), rootSource);
}
Also used : Injector(org.apache.cayenne.di.Injector) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) XMLReader(org.xml.sax.XMLReader) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) DIBootstrap(org.apache.cayenne.di.DIBootstrap) URLResource(org.apache.cayenne.resource.URLResource) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) Resource(org.apache.cayenne.resource.Resource) Assert(org.junit.Assert) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DataChannelDescriptor(org.apache.cayenne.configuration.DataChannelDescriptor) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) URLResource(org.apache.cayenne.resource.URLResource) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 9 with Injector

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

the class DataChannelProjectSaverTest method testSaveAs.

@Test
public void testSaveAs() throws Exception {
    FileProjectSaver saver = new FileProjectSaver(Collections.<ProjectExtension>emptyList());
    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(DataChannelDescriptorLoader.class).to(XMLDataChannelDescriptorLoader.class);
        binder.bind(ProjectLoader.class).to(DataChannelProjectLoader.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 injector = DIBootstrap.createInjector(testModule);
    injector.injectMembers(saver);
    String testConfigName = "PROJECT2";
    String baseUrl = getClass().getPackage().getName().replace('.', '/');
    URL url = getClass().getClassLoader().getResource(baseUrl + "/cayenne-" + testConfigName + ".xml");
    Resource source = new URLResource(url);
    Project project = injector.getInstance(ProjectLoader.class).loadProject(source);
    File outFile = setupTestDirectory("testSave");
    saver.saveAs(project, new URLResource(outFile.toURI().toURL()));
    File rootFile = new File(outFile, "cayenne-PROJECT2.xml");
    assertTrue(rootFile.exists());
    assertTrue(rootFile.length() > 0);
    File map1File = new File(outFile, "testProjectMap2_1.map.xml");
    assertTrue(map1File.exists());
    assertTrue(map1File.length() > 0);
    File map2File = new File(outFile, "testProjectMap2_2.map.xml");
    assertTrue(map2File.exists());
    assertTrue(map2File.length() > 0);
}
Also used : Injector(org.apache.cayenne.di.Injector) HandlerFactory(org.apache.cayenne.configuration.xml.HandlerFactory) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) Project2Case(org.apache.cayenne.project.unit.Project2Case) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) URL(java.net.URL) ConfigurationNameMapper(org.apache.cayenne.configuration.ConfigurationNameMapper) Module(org.apache.cayenne.di.Module) DataChannelDescriptorLoader(org.apache.cayenne.configuration.DataChannelDescriptorLoader) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DataMapLoader(org.apache.cayenne.configuration.DataMapLoader) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) XMLReader(org.xml.sax.XMLReader) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) DIBootstrap(org.apache.cayenne.di.DIBootstrap) ProjectExtension(org.apache.cayenne.project.extension.ProjectExtension) PrintWriter(java.io.PrintWriter) URLResource(org.apache.cayenne.resource.URLResource) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) DataChannelMetaData(org.apache.cayenne.configuration.xml.DataChannelMetaData) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) File(java.io.File) Resource(org.apache.cayenne.resource.Resource) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DefaultConfigurationNameMapper(org.apache.cayenne.configuration.DefaultConfigurationNameMapper) XMLDataMapLoader(org.apache.cayenne.configuration.xml.XMLDataMapLoader) URLResource(org.apache.cayenne.resource.URLResource) Resource(org.apache.cayenne.resource.Resource) URL(java.net.URL) XMLDataChannelDescriptorLoader(org.apache.cayenne.configuration.xml.XMLDataChannelDescriptorLoader) NoopDataChannelMetaData(org.apache.cayenne.configuration.xml.NoopDataChannelMetaData) XMLReaderProvider(org.apache.cayenne.configuration.xml.XMLReaderProvider) URLResource(org.apache.cayenne.resource.URLResource) DefaultHandlerFactory(org.apache.cayenne.configuration.xml.DefaultHandlerFactory) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) File(java.io.File) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 10 with Injector

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

the class BaseContext method attachToRuntimeIfNeeded.

/**
 * Checks whether this context is attached to Cayenne runtime stack and if
 * not, attempts to attach itself to the runtime using Injector returned
 * from the call to {@link CayenneRuntime#getThreadInjector()}. If thread
 * Injector is not available and the context is not attached, throws
 * CayenneRuntimeException.
 * <p>
 * This method is called internally by the context before access to
 * transient variables to allow the context to attach to the stack lazily
 * following deserialization.
 *
 * @return true if the context successfully attached to the thread runtime,
 *         false - if it was already attached.
 * @since 3.1
 */
protected boolean attachToRuntimeIfNeeded() {
    if (channel != null) {
        return false;
    }
    Injector injector = CayenneRuntime.getThreadInjector();
    if (injector == null) {
        throw new CayenneRuntimeException("Can't attach to Cayenne runtime. " + "Null injector returned from CayenneRuntime.getThreadInjector()");
    }
    attachToRuntime(injector);
    return true;
}
Also used : Injector(org.apache.cayenne.di.Injector) ValueInjector(org.apache.cayenne.exp.ValueInjector)

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