Search in sources :

Example 26 with DataSource

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();
    }
}
Also used : AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) Column(jdk.incubator.sql2.Result.Column) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Collector(java.util.stream.Collector) Assert(org.junit.Assert) NoSuchElementException(java.util.NoSuchElementException) Session(jdk.incubator.sql2.Session) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 27 with DataSource

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();
    }
}
Also used : AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) Column(jdk.incubator.sql2.Result.Column) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ForkJoinPool(java.util.concurrent.ForkJoinPool) Collector(java.util.stream.Collector) Assert(org.junit.Assert) NoSuchElementException(java.util.NoSuchElementException) Session(jdk.incubator.sql2.Session) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Column(jdk.incubator.sql2.Result.Column) NoSuchElementException(java.util.NoSuchElementException) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 28 with DataSource

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));
}
Also used : BeforeClass(org.junit.BeforeClass) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) SqlSkippedException(jdk.incubator.sql2.SqlSkippedException) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) AdbaType(jdk.incubator.sql2.AdbaType) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) BinaryOperator(java.util.function.BinaryOperator) ArrayRowCountOperation(jdk.incubator.sql2.ArrayRowCountOperation) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) Result(jdk.incubator.sql2.Result) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Collections(java.util.Collections) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 29 with DataSource

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);
    }
}
Also used : DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 30 with DataSource

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);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Aggregations

DataSource (jdk.incubator.sql2.DataSource)96 Session (jdk.incubator.sql2.Session)88 Test (org.junit.Test)85 DataSource (io.atlasmap.v2.DataSource)54 ArrayList (java.util.ArrayList)50 AfterClass (org.junit.AfterClass)44 BeforeClass (org.junit.BeforeClass)44 TimeUnit (java.util.concurrent.TimeUnit)42 Collector (java.util.stream.Collector)36 ForkJoinPool (java.util.concurrent.ForkJoinPool)32 AdbaType (jdk.incubator.sql2.AdbaType)32 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)30 DataSourceFactory (jdk.incubator.sql2.DataSourceFactory)28 Assert (org.junit.Assert)28 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 List (java.util.List)23 CompletableFuture (java.util.concurrent.CompletableFuture)22 NoSuchElementException (java.util.NoSuchElementException)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 Column (jdk.incubator.sql2.Result.Column)18