Search in sources :

Example 6 with MockConnection

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")));
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) Module(org.apache.cayenne.di.Module) DatabaseMetaData(java.sql.DatabaseMetaData) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) List(java.util.List) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) Assert(org.junit.Assert) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Mockito.mock(org.mockito.Mockito.mock) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DatabaseMetaData(java.sql.DatabaseMetaData) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Test(org.junit.Test)

Example 7 with MockConnection

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")));
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) Module(org.apache.cayenne.di.Module) DatabaseMetaData(java.sql.DatabaseMetaData) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) List(java.util.List) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) Assert(org.junit.Assert) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Mockito.mock(org.mockito.Mockito.mock) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DatabaseMetaData(java.sql.DatabaseMetaData) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) Test(org.junit.Test)

Example 8 with MockConnection

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());
}
Also used : Connection(java.sql.Connection) MockConnection(com.mockrunner.mock.jdbc.MockConnection) MockStatement(com.mockrunner.mock.jdbc.MockStatement) MockResultSet(com.mockrunner.mock.jdbc.MockResultSet) MockConnection(com.mockrunner.mock.jdbc.MockConnection) Test(org.junit.Test)

Example 9 with MockConnection

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);
}
Also used : PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.mockrunner.mock.jdbc.MockPreparedStatement) MockPreparedStatement(com.mockrunner.mock.jdbc.MockPreparedStatement) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DateTime(org.joda.time.DateTime)

Example 10 with MockConnection

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);
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.mockrunner.mock.jdbc.MockPreparedStatement) MockPreparedStatement(com.mockrunner.mock.jdbc.MockPreparedStatement) MockConnection(com.mockrunner.mock.jdbc.MockConnection)

Aggregations

MockConnection (com.mockrunner.mock.jdbc.MockConnection)10 Test (org.junit.Test)6 MockPreparedStatement (com.mockrunner.mock.jdbc.MockPreparedStatement)4 PreparedStatement (java.sql.PreparedStatement)4 DbEntity (org.apache.cayenne.map.DbEntity)4 DbAdapter (org.apache.cayenne.dba.DbAdapter)3 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)2 MockDataSource (com.mockrunner.mock.jdbc.MockDataSource)2 MockResultSet (com.mockrunner.mock.jdbc.MockResultSet)2 MockStatement (com.mockrunner.mock.jdbc.MockStatement)2 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DataNode (org.apache.cayenne.access.DataNode)2 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)2 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)2 BatchTranslatorFactory (org.apache.cayenne.access.translator.batch.BatchTranslatorFactory)2