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;
}
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);
}
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());
}
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());
}
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=\""));
}
Aggregations