Search in sources :

Example 1 with MockOperationObserver

use of org.apache.cayenne.access.MockOperationObserver 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);
    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 ignore) {
    }
    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 2 with MockOperationObserver

use of org.apache.cayenne.access.MockOperationObserver 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", 1);
    DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
    batchQuery.setUsingOptimisticLocking(true);
    batchQuery.add(qualifierSnapshot);
    DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter);
    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 3 with MockOperationObserver

use of org.apache.cayenne.access.MockOperationObserver in project cayenne by apache.

the class SQLTemplateActionIT method testExecuteSelect.

@Test
public void testExecuteSelect() throws Exception {
    createFourArtists();
    String templateString = "SELECT * FROM ARTIST WHERE ARTIST_ID = #bind($id)";
    SQLTemplate template = new SQLTemplate(Object.class, templateString);
    sqlTemplateCustomizer.updateSQLTemplate(template);
    Map<String, Object> bindings = new HashMap<>();
    bindings.put("id", 201L);
    template.setParameters(bindings);
    // must ensure the right SQLTemplateAction is created
    SQLAction plan = adapter.getAction(template, node);
    assertTrue(plan instanceof SQLTemplateAction);
    MockOperationObserver observer = new MockOperationObserver();
    try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
        plan.performAction(c, observer);
    }
    List<DataRow> rows = observer.rowsForQuery(template);
    assertNotNull(rows);
    assertEquals(1, rows.size());
    DataRow row = rows.get(0);
    // In the absence of ObjEntity most DB's return a Long here, except for
    // Oracle
    // that has no BIGINT type and
    // returns BigDecimal, so do a Number comparison
    Number id = (Number) row.get("ARTIST_ID");
    assertNotNull(id);
    assertEquals(((Number) bindings.get("id")).longValue(), id.longValue());
    assertEquals("artist4", row.get("ARTIST_NAME"));
    assertTrue(row.containsKey("DATE_OF_BIRTH"));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) HashMap(java.util.HashMap) Connection(java.sql.Connection) SQLAction(org.apache.cayenne.query.SQLAction) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 4 with MockOperationObserver

use of org.apache.cayenne.access.MockOperationObserver in project cayenne by apache.

the class SchemaUpdateStrategyIT method testCreateIfNoSchemaStrategy.

@Test
public void testCreateIfNoSchemaStrategy() throws Exception {
    setStrategy(CreateIfNoSchemaStrategy.class);
    String template = "SELECT #result('id' 'int') FROM SUS1";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    OperationObserver observer = new MockOperationObserver();
    node.performQueries(Collections.singletonList((Query) query), observer);
    Map<String, Boolean> nameTables = tablesMap();
    assertTrue(nameTables.get("SUS1"));
    assertEquals(2, existingTables().size());
    node.performQueries(Collections.singletonList(query), observer);
    assertEquals(2, existingTables().size());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) OperationObserver(org.apache.cayenne.access.OperationObserver) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Example 5 with MockOperationObserver

use of org.apache.cayenne.access.MockOperationObserver in project cayenne by apache.

the class SchemaUpdateStrategyIT method testNoStandardSchema.

@Test
public void testNoStandardSchema() throws Exception {
    String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
    SQLTemplate query = new SQLTemplate(Object.class, template);
    MockOperationObserver observer = new MockOperationObserver();
    setStrategy(TstSchemaUpdateStrategy.class);
    node.performQueries(Collections.singletonList((Query) query), observer);
    assertTrue(node.getSchemaUpdateStrategy() instanceof TstSchemaUpdateStrategy);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) Test(org.junit.Test)

Aggregations

MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)16 SQLTemplate (org.apache.cayenne.query.SQLTemplate)14 Test (org.junit.Test)14 HashMap (java.util.HashMap)8 Connection (java.sql.Connection)7 SQLAction (org.apache.cayenne.query.SQLAction)7 DataRow (org.apache.cayenne.DataRow)5 Query (org.apache.cayenne.query.Query)3 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)2 Date (java.sql.Date)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 DataNode (org.apache.cayenne.access.DataNode)2 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)2 DeleteBatchTranslator (org.apache.cayenne.access.translator.batch.DeleteBatchTranslator)2 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)2 DbAttribute (org.apache.cayenne.map.DbAttribute)2 DbEntity (org.apache.cayenne.map.DbEntity)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)2