use of org.apache.cayenne.di.Injector in project cayenne by apache.
the class DefaultDataSourceFactoryLoaderTest method testGetDataSourceFactory_Property.
@Test
public void testGetDataSourceFactory_Property() throws Exception {
final RuntimeProperties properties = mock(RuntimeProperties.class);
when(properties.get(Constants.JDBC_DRIVER_PROPERTY)).thenReturn("x");
when(properties.get(Constants.JDBC_URL_PROPERTY)).thenReturn("y");
DataChannelDescriptor channelDescriptor = new DataChannelDescriptor();
channelDescriptor.setName("X");
DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
nodeDescriptor.setName("node1");
nodeDescriptor.setDataSourceFactoryType(MockDataSourceFactory1.class.getName());
nodeDescriptor.setDataChannelDescriptor(channelDescriptor);
Module testModule = binder -> {
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(MockResourceLocator.class);
binder.bind(RuntimeProperties.class).toInstance(properties);
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
};
Injector injector = DIBootstrap.createInjector(testModule);
DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory();
injector.injectMembers(factoryLoader);
DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertTrue(factory instanceof PropertyDataSourceFactory);
when(properties.get(Constants.JDBC_URL_PROPERTY)).thenReturn(null);
factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertFalse(factory instanceof PropertyDataSourceFactory);
when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node2")).thenReturn("y");
factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertFalse(factory instanceof PropertyDataSourceFactory);
when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node1")).thenReturn("y");
factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertTrue(factory instanceof PropertyDataSourceFactory);
}
use of org.apache.cayenne.di.Injector in project cayenne by apache.
the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Auto.
@Test
public void testCreatedAdapter_Auto() throws Exception {
final DbAdapter adapter = mock(DbAdapter.class);
when(adapter.createTable(any(DbEntity.class))).thenReturn("XXXXX");
List<DbAdapterDetector> detectors = new ArrayList<DbAdapterDetector>();
detectors.add(new DbAdapterDetector() {
public DbAdapter createAdapter(DatabaseMetaData md) throws SQLException {
return adapter;
}
});
MockConnection connection = new MockConnection();
MockDataSource dataSource = new MockDataSource();
dataSource.setupConnection(connection);
Module testModule = binder -> {
ServerModule.contributeProperties(binder);
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
};
Injector injector = DIBootstrap.createInjector(testModule);
DefaultDbAdapterFactory factory = new DefaultDbAdapterFactory(detectors);
injector.injectMembers(factory);
DbAdapter createdAdapter = factory.createAdapter(new DataNodeDescriptor(), dataSource);
assertTrue(createdAdapter instanceof AutoAdapter);
assertEquals("XXXXX", createdAdapter.createTable(new DbEntity("Test")));
}
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);
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 CayenneGeneratorTask method execute.
/**
* Executes the task. It will be called by ant framework.
*/
@Override
public void execute() throws BuildException {
validateAttributes();
Injector injector = DIBootstrap.createInjector(new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorTask.class)));
AntLogger logger = new AntLogger(this);
CayenneGeneratorMapLoaderAction loadAction = new CayenneGeneratorMapLoaderAction(injector);
loadAction.setMainDataMapFile(map);
loadAction.setAdditionalDataMapFiles(additionalMaps);
CayenneGeneratorEntityFilterAction filterAction = new CayenneGeneratorEntityFilterAction();
filterAction.setClient(client);
filterAction.setNameFilter(NamePatternMatcher.build(logger, includeEntitiesPattern, excludeEntitiesPattern));
try {
DataMap dataMap = loadAction.getMainDataMap();
ClassGenerationAction generatorAction = createGeneratorAction();
generatorAction.setLogger(logger);
generatorAction.setTimestamp(map.lastModified());
generatorAction.setDataMap(dataMap);
generatorAction.addEntities(filterAction.getFilteredEntities(dataMap));
generatorAction.addEmbeddables(filterAction.getFilteredEmbeddables(dataMap));
generatorAction.addQueries(dataMap.getQueryDescriptors());
generatorAction.execute();
} catch (Exception e) {
throw new BuildException(e);
}
}
use of org.apache.cayenne.di.Injector in project cayenne by apache.
the class DbGeneratorTask method execute.
@Override
public void execute() {
Logger logger = new AntLogger(this);
log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver, url, userName), Project.MSG_VERBOSE);
log(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
validateAttributes();
ClassLoader loader = null;
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
try {
loader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
// Load the data map and run the db generator.
DataMap dataMap = loadDataMap(injector);
// load driver taking custom CLASSPATH into account...
DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, userName, password);
DbAdapter adapter = getAdapter(injector, dataSource);
DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>emptyList(), null, NoopJdbcEventLogger.getInstance());
generator.setShouldCreateFKConstraints(createFK);
generator.setShouldCreatePKSupport(createPK);
generator.setShouldCreateTables(createTables);
generator.setShouldDropPKSupport(dropPK);
generator.setShouldDropTables(dropTables);
generator.runGenerator(dataSource);
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error generating database";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
log(message, Project.MSG_ERR);
throw new BuildException(message, th);
} finally {
Thread.currentThread().setContextClassLoader(loader);
injector.shutdown();
}
}
Aggregations