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 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);
}
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);
}
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;
}
Aggregations