use of com.mockrunner.mock.jdbc.MockConnection 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 com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.
the class DefaultDbAdapterFactoryTest method testCreatedAdapter_AutoExplicit.
@Test
public void testCreatedAdapter_AutoExplicit() 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(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.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);
DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
nodeDescriptor.setAdapterType(AutoAdapter.class.getName());
DbAdapter createdAdapter = factory.createAdapter(nodeDescriptor, dataSource);
assertTrue(createdAdapter instanceof AutoAdapter);
assertEquals("XXXXX", createdAdapter.createTable(new DbEntity("Test")));
}
use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.
the class JDBCResultIteratorTest method testClose.
@Test
public void testClose() throws Exception {
Connection c = new MockConnection();
MockStatement s = new MockStatement(c);
MockResultSet rs = new MockResultSet("rs");
rs.addColumn("a", new Object[] { "1", "2", "3" });
RowReader<?> rowReader = mock(RowReader.class);
try (JDBCResultIterator it = new JDBCResultIterator(s, rs, rowReader)) {
assertFalse(rs.isClosed());
assertFalse(s.isClosed());
assertFalse(c.isClosed());
}
assertTrue(rs.isClosed());
assertTrue(s.isClosed());
}
use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.
the class DateTimeTypeTest method testSetJdbcObject.
public void testSetJdbcObject() throws Exception {
PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?");
DateTime date = new DateTime(0);
type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0);
Object object = ((MockPreparedStatement) statement).getParameter(1);
assertEquals(Timestamp.class, object.getClass());
assertEquals(date.getMillis(), ((Timestamp) object).getTime());
type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0);
object = ((MockPreparedStatement) statement).getParameter(1);
assertNull(object);
}
use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.
the class LocalDateTimeTypeTest method testSetJdbcObject.
public void testSetJdbcObject() throws Exception {
PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?");
LocalDateTime date = new LocalDateTime(0);
type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0);
Object object = ((MockPreparedStatement) statement).getParameter(1);
assertEquals(Timestamp.class, object.getClass());
assertEquals(date.toDate().getTime(), ((Timestamp) object).getTime());
type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0);
object = ((MockPreparedStatement) statement).getParameter(1);
assertNull(object);
}
Aggregations