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());
}
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());
}
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"));
}
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());
}
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);
}
Aggregations