Search in sources :

Example 1 with MockConnection

use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.

the class LocalDateTypeTest method testSetJdbcObject.

public void testSetJdbcObject() throws Exception {
    PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?");
    LocalDate savedObject = new LocalDate(0);
    type.setJdbcObject(statement, savedObject, 1, Types.DATE, 0);
    Object object = ((MockPreparedStatement) statement).getParameter(1);
    assertEquals(Date.class, object.getClass());
    assertEquals(savedObject.toDate().getTime(), ((Date) object).getTime());
    type.setJdbcObject(statement, savedObject, 1, Types.TIMESTAMP, 0);
    object = ((MockPreparedStatement) statement).getParameter(1);
    assertEquals(Timestamp.class, object.getClass());
    assertEquals(savedObject.toDate().getTime(), ((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) LocalDate(org.joda.time.LocalDate)

Example 2 with MockConnection

use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.

the class LocalTimeTypeTest method testSetJdbcObject.

public void testSetJdbcObject() throws Exception {
    PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?");
    LocalTime date = new LocalTime(0);
    type.setJdbcObject(statement, date, 1, Types.TIME, 0);
    Object object = ((MockPreparedStatement) statement).getParameter(1);
    assertEquals(Time.class, object.getClass());
    assertEquals(new LocalDate(0, DateTimeZone.UTC).toDateTime(date).getMillis(), ((Time) object).getTime());
    type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0);
    object = ((MockPreparedStatement) statement).getParameter(1);
    assertEquals(Timestamp.class, object.getClass());
    assertEquals(new LocalDate(0, DateTimeZone.UTC).toDateTime(date).getMillis(), ((Timestamp) object).getTime());
    type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0);
    object = ((MockPreparedStatement) statement).getParameter(1);
    assertNull(object);
}
Also used : LocalTime(org.joda.time.LocalTime) PreparedStatement(java.sql.PreparedStatement) MockPreparedStatement(com.mockrunner.mock.jdbc.MockPreparedStatement) MockPreparedStatement(com.mockrunner.mock.jdbc.MockPreparedStatement) MockConnection(com.mockrunner.mock.jdbc.MockConnection) LocalDate(org.joda.time.LocalDate)

Example 3 with MockConnection

use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.

the class BatchActionLockingIT method testRunAsIndividualQueriesOptimisticLockingFailure.

@Test
public void testRunAsIndividualQueriesOptimisticLockingFailure() throws Exception {
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    // test with adapter that supports keys...
    JdbcAdapter adapter = buildAdapter(true);
    DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
    Collection<String> nullAttributeNames = Collections.singleton("NAME");
    Map<String, Object> qualifierSnapshot = new HashMap<>();
    qualifierSnapshot.put("LOCKING_TEST_ID", 1);
    DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
    batchQuery.setUsingOptimisticLocking(true);
    batchQuery.add(qualifierSnapshot);
    DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
    MockConnection mockConnection = new MockConnection();
    PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
    preparedStatementResultSetHandler.setExactMatch(false);
    preparedStatementResultSetHandler.setCaseSensitive(false);
    preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 0);
    boolean generatesKeys = false;
    DataNode node = new DataNode();
    node.setAdapter(adapter);
    node.setEntityResolver(resolver);
    node.setRowReaderFactory(mock(RowReaderFactory.class));
    BatchAction action = new BatchAction(batchQuery, node, false);
    try {
        action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
        fail("No OptimisticLockingFailureException thrown.");
    } catch (OptimisticLockException e) {
    }
    assertEquals(0, mockConnection.getNumberCommits());
    assertEquals(0, mockConnection.getNumberRollbacks());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) HashMap(java.util.HashMap) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) EntityResolver(org.apache.cayenne.map.EntityResolver) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbEntity(org.apache.cayenne.map.DbEntity) PreparedStatementResultSetHandler(com.mockrunner.jdbc.PreparedStatementResultSetHandler) DataNode(org.apache.cayenne.access.DataNode) DeleteBatchTranslator(org.apache.cayenne.access.translator.batch.DeleteBatchTranslator) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 4 with MockConnection

use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.

the class BatchActionLockingIT method testRunAsIndividualQueriesSuccess.

@Test
public void testRunAsIndividualQueriesSuccess() throws Exception {
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    // test with adapter that supports keys...
    JdbcAdapter adapter = buildAdapter(true);
    DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
    Collection<String> nullAttributeNames = Collections.singleton("NAME");
    Map<String, Object> qualifierSnapshot = new HashMap<>();
    qualifierSnapshot.put("LOCKING_TEST_ID", new Integer(1));
    DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
    batchQuery.setUsingOptimisticLocking(true);
    batchQuery.add(qualifierSnapshot);
    DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
    MockConnection mockConnection = new MockConnection();
    PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
    preparedStatementResultSetHandler.setExactMatch(false);
    preparedStatementResultSetHandler.setCaseSensitive(false);
    preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 1);
    boolean generatesKeys = false;
    DataNode node = new DataNode();
    node.setAdapter(adapter);
    node.setEntityResolver(resolver);
    node.setRowReaderFactory(mock(RowReaderFactory.class));
    BatchAction action = new BatchAction(batchQuery, node, false);
    action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
    assertEquals(0, mockConnection.getNumberCommits());
    assertEquals(0, mockConnection.getNumberRollbacks());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) HashMap(java.util.HashMap) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbEntity(org.apache.cayenne.map.DbEntity) PreparedStatementResultSetHandler(com.mockrunner.jdbc.PreparedStatementResultSetHandler) DataNode(org.apache.cayenne.access.DataNode) DeleteBatchTranslator(org.apache.cayenne.access.translator.batch.DeleteBatchTranslator) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 5 with MockConnection

use of com.mockrunner.mock.jdbc.MockConnection in project cayenne by apache.

the class JDBCResultIteratorTest method testNextDataRow.

@Test
public void testNextDataRow() throws Exception {
    Connection c = new MockConnection();
    Statement s = new MockStatement(c);
    MockResultSet rs = new MockResultSet("rs");
    rs.addColumn("a", new Object[] { "1", "2", "3" });
    RowDescriptor descriptor = new RowDescriptorBuilder().setResultSet(rs).getDescriptor(new ExtendedTypeMap());
    RowReader<?> rowReader = new DefaultRowReaderFactory().rowReader(descriptor, new MockQueryMetadata(), mock(DbAdapter.class), Collections.<ObjAttribute, ColumnDescriptor>emptyMap());
    JDBCResultIterator it = new JDBCResultIterator(s, rs, rowReader);
    DataRow row = (DataRow) it.nextRow();
    assertNotNull(row);
    assertEquals(1, row.size());
    assertEquals("1", row.get("a"));
}
Also used : MockStatement(com.mockrunner.mock.jdbc.MockStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) MockConnection(com.mockrunner.mock.jdbc.MockConnection) MockResultSet(com.mockrunner.mock.jdbc.MockResultSet) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DataRow(org.apache.cayenne.DataRow) ExtendedTypeMap(org.apache.cayenne.access.types.ExtendedTypeMap) DefaultRowReaderFactory(org.apache.cayenne.access.jdbc.reader.DefaultRowReaderFactory) MockQueryMetadata(org.apache.cayenne.query.MockQueryMetadata) DbAdapter(org.apache.cayenne.dba.DbAdapter) MockStatement(com.mockrunner.mock.jdbc.MockStatement) Test(org.junit.Test)

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