Search in sources :

Example 1 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)

Example 2 with Injector

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

the class DefaultDataRowStoreFactoryIT method testGetDataRowStoreWithBridge.

@Test
public void testGetDataRowStoreWithBridge() {
    final DataDomain DOMAIN = new DataDomain("test");
    final EventManager EVENT_MANAGER = new DefaultEventManager();
    Module testModule = binder -> {
        binder.bind(DataDomain.class).toInstance(DOMAIN);
        binder.bind(EventManager.class).toInstance(EVENT_MANAGER);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
        binder.bind(EventBridge.class).toProvider(MockEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(DataDomainFlushActionFactory.class).to(DefaultDataDomainFlushActionFactory.class);
        binder.bind(DbRowOpSorter.class).to(DefaultDbRowOpSorter.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        ServerModule.contributeProperties(binder);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataRowStore dataStore = injector.getInstance(DataRowStoreFactory.class).createDataRowStore("test");
    assertEquals(dataStore.getEventBridge().getClass(), MockEventBridge.class);
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) TransactionManager(org.apache.cayenne.tx.TransactionManager) ServerCase(org.apache.cayenne.unit.di.server.ServerCase) Module(org.apache.cayenne.di.Module) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) MockEventBridgeProvider(org.apache.cayenne.event.MockEventBridgeProvider) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) ServerModule(org.apache.cayenne.configuration.server.ServerModule) DIBootstrap(org.apache.cayenne.di.DIBootstrap) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) MockEventBridge(org.apache.cayenne.event.MockEventBridge) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) Assert.assertNotNull(org.junit.Assert.assertNotNull) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) CayenneProjects(org.apache.cayenne.unit.di.server.CayenneProjects) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) Assert.assertNull(org.junit.Assert.assertNull) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) DbRowOpSorter(org.apache.cayenne.access.flush.operation.DbRowOpSorter) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) UseServerRuntime(org.apache.cayenne.unit.di.server.UseServerRuntime) DataDomainFlushActionFactory(org.apache.cayenne.access.flush.DataDomainFlushActionFactory) Assert.assertEquals(org.junit.Assert.assertEquals) DefaultDataDomainFlushActionFactory(org.apache.cayenne.access.flush.DefaultDataDomainFlushActionFactory) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) MockEventBridgeProvider(org.apache.cayenne.event.MockEventBridgeProvider) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) DefaultDbRowOpSorter(org.apache.cayenne.access.flush.operation.DefaultDbRowOpSorter) Module(org.apache.cayenne.di.Module) ServerModule(org.apache.cayenne.configuration.server.ServerModule) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Test(org.junit.Test)

Example 3 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 4 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 5 with Injector

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

the class DefaultDbImportActionTest method testSaveLoadedNoProject.

@Test
public void testSaveLoadedNoProject() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
    DbImportConfiguration params = mock(DbImportConfiguration.class);
    when(params.getCayenneProject()).thenReturn(null);
    URL outUrl = new URL(getPackageURL(), "dbimport/testSaveLoaded1.map.xml");
    DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
    File out = new File(outUrl.toURI());
    out.delete();
    assertFalse(out.exists());
    DataMap map = new DataMap("testSaveLoaded1");
    map.setConfigurationSource(new URLResource(outUrl));
    action.saveLoaded(map, params);
    assertTrue(out.isFile());
    String contents = Util.stringFromFile(out);
    assertTrue("Has no project version saved", contents.contains("project-version=\""));
}
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)65 Test (org.junit.Test)55 Module (org.apache.cayenne.di.Module)30 DIBootstrap (org.apache.cayenne.di.DIBootstrap)23 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)21 DefaultAdhocObjectFactory (org.apache.cayenne.di.spi.DefaultAdhocObjectFactory)20 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)20 ClassLoaderManager (org.apache.cayenne.di.ClassLoaderManager)19 URL (java.net.URL)15 RuntimeProperties (org.apache.cayenne.configuration.RuntimeProperties)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 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)13 DataMap (org.apache.cayenne.map.DataMap)13 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)12 DefaultRuntimeProperties (org.apache.cayenne.configuration.DefaultRuntimeProperties)12 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 DbAdapter (org.apache.cayenne.dba.DbAdapter)11