use of jdk.incubator.sql2.DataSource in project oracle-db-examples by oracle.
the class RowOperationTest method testColumnForEach.
@Test
public void testColumnForEach() throws Exception {
try (DataSource ds = getDataSource();
Session se = ds.getSession()) {
AtomicInteger rowCount = new AtomicInteger();
se.rowOperation("SELECT * FROM " + TEST_TABLE).collect(ArrayList<String>::new, (ls, rc) -> {
int rowIndex = rowCount.getAndIncrement();
assertEquals(rowIndex, rc.rowNumber());
assertEquals(TEST_COLUMNS.length - 1, rc.numberOfValuesRemaining());
AtomicInteger coIndex = new AtomicInteger();
rc.forEach(co -> {
validateColumn(rowIndex, co, coIndex.incrementAndGet(), 0, TEST_COLUMNS.length);
});
}).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
}
}
use of jdk.incubator.sql2.DataSource in project oracle-db-examples by oracle.
the class RowOperationTest method testInvalidSlice.
@Test
public void testInvalidSlice() throws Exception {
try (DataSource ds = getDataSource();
Session se = ds.getSession()) {
AtomicInteger rowCount = new AtomicInteger();
se.rowOperation("SELECT * FROM " + TEST_TABLE).collect(ArrayList<String>::new, (ls, rc) -> {
int rowIndex = rowCount.getAndIncrement();
assertEquals(rowIndex, rc.rowNumber());
assertEquals(TEST_COLUMNS.length - 1, rc.numberOfValuesRemaining());
int coIndex = 0;
for (Column co : rc) {
coIndex++;
assertEquals(TEST_COLUMNS.length - coIndex, co.numberOfValuesRemaining());
// From the current column index, test the invalid ranges.
try {
co.slice(-coIndex);
fail("No exception thrown at call to slice(" + (-coIndex) + ")" + " from column index: " + coIndex);
} catch (NoSuchElementException expected) {
/* Expected */
}
try {
co.slice(1 + TEST_COLUMNS.length - (coIndex - 1));
fail("No exception thrown at call to slice(" + (-coIndex) + ")" + " from column index: " + coIndex);
} catch (NoSuchElementException expected) {
/* Expected */
}
try {
co.slice(0);
fail("No exception thrown at call to slice(0)" + " from column index: " + coIndex);
} catch (NoSuchElementException expected) {
/* Expected */
}
}
}).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
}
}
use of jdk.incubator.sql2.DataSource in project oracle-db-examples by oracle.
the class SessionOperationGroupTest method testCollect.
/**
* Verify basic functionality of
* {@link Session#collect(java.util.stream.Collector)}
* @throws java.lang.Exception
*/
@Test
public void testCollect() throws Exception {
Submission<Object> seSubmission = null;
try (DataSource ds = TestConfig.getDataSource();
Session se = ds.builder().build()) {
Collector<Object, ?, Object> co = new ObjectCollector();
se.attachOperation().timeout(getTimeout()).submit();
se.collect(co).localOperation().onExecution(() -> 99).submit();
se.rowOperation("SELECT COUNT(*) FROM " + TABLE).collect(Collectors.summingInt(row -> row.at(1).get(Integer.class))).timeout(getTimeout()).submit();
se.endTransactionOperation(se.transactionCompletion()).submit();
seSubmission = se.submit();
}
// se.closeOperation().submit() // 5
assertNotNull(seSubmission);
@SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) seSubmission.getCompletionStage().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
assertNotNull(result);
// Note the inline comments which count each submit (1, 2, 3...). This is
// the expected count of accumulated results.
assertEquals(5, result.size());
// Attach operation
assertNull(result.get(0));
// Local operation
assertEquals(99, result.get(1).intValue());
// Row operation
assertEquals(0, result.get(2).intValue());
assertEquals(TransactionOutcome.COMMIT, // Transaction operation
result.get(3));
// Close operation
assertNull(result.get(4));
}
use of jdk.incubator.sql2.DataSource in project oracle-db-examples by oracle.
the class SessionOperationGroupTest method arrayRowCountOperationTest.
/**
* Verify {@link Session#arrayRowCountOperation()} returns an instance of
* ArrayRowCountOperation. The returned instance is only verified
* for basic functionality.
* @throws Exception
*/
@Test
public void arrayRowCountOperationTest() throws Exception {
try (DataSource ds = TestConfig.getDataSource();
Session se = ds.getSession()) {
ArrayRowCountOperation<Long> arcOp = se.<Long>arrayRowCountOperation("INSERT INTO " + TABLE + " VALUES (?)");
assertNotNull(arcOp);
String[] values = { "x", "y" };
long count = arcOp.set("1", values, AdbaType.VARCHAR).collect(Collectors.summingLong(Result.RowCount::getCount)).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
assertEquals(2L, count);
se.rollback().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
}
use of jdk.incubator.sql2.DataSource in project oracle-db-examples by oracle.
the class SessionOperationGroupTest method testFalseConditonal.
@Test
public void testFalseConditonal() throws Exception {
try (DataSource ds = getDataSource();
Session se = ds.builder().build()) {
se.conditional(CompletableFuture.completedFuture(false));
se.attachOperation().submit();
AtomicBoolean opExecuted = new AtomicBoolean();
se.localOperation().onExecution(() -> {
opExecuted.set(true);
return null;
}).submit();
Object result = se.collect(new ObjectCollector()).submit().getCompletionStage().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
assertFalse(opExecuted.get());
assertNull(result);
}
}
Aggregations