Search in sources :

Example 1 with SessionLifecycleListener

use of jdk.incubator.sql2.Session.SessionLifecycleListener in project oracle-db-examples by oracle.

the class SessionTest method testSessionBuilder.

/**
 * Verifies the following behavior:
 * <br>
 * (1) Session.Builder.property(SessionProperty, Object) will specify a
 * property and its value for the built Session.
 * [Spec: {@link Session.Builder#property(SessionProperty, Object)}]
 * Session.getProperties() will return the set of properties configured on
 * this Session excepting any sensitive properties.
 * [Spec: {@link Session#getProperties()}]
 * <br>
 * (2) A Session is initially in the Session.Lifecycle.NEW lifecycle state.
 * [Spec: {@link Session.Builder}]
 * <br>
 * (3) If an attach operation completes successfully and the lifecycle is
 * Session.Lifecycle.NEW -> Session.Lifecycle.OPEN.
 * [Spec: {@link Session#attachOperation()}]
 * <br>
 * (4) Session.getSessionLifecycle() returns the current lifecycle of this
 * Session. [Spec: {@link Session#getSessionLifecycle()}]
 * <br>
 * (5) Session.registerLifcycleListener(SessionLifecycleListener) registers
 * a listener that will be called whenever there is a change in the
 * lifecycle of this Session. If the listener is already registered this
 * is a no-op. [Spec:
 * {@link Session#registerLifecycleListener(SessionLifecycleListener)}]
 * (6) Session.closeOperation() returns an operation which, upon execution,
 * transitions the session's lifecyle from OPEN to CLOSING. When the
 * operation completes, and no other operations are enqueued, the lifecycle
 * transistions from CLOSING to CLOSED. If the operation is submitted when
 * the lifecycle is closed, the execution of the operation is a no-op.
 * [Spec: {@link Session#closeOperation()}]
 */
@Test
public void testSessionBuilder() throws Exception {
    TestLifecycleListener testListener = new TestLifecycleListener();
    final String url = getUrl();
    final String user = getUser();
    final String password = getPassword();
    Session se = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().build().builder().property(AdbaSessionProperty.URL, url).property(AdbaSessionProperty.USER, user).property(AdbaSessionProperty.PASSWORD, password).build();
    // (1)
    assertEquals(url, se.getProperties().get(AdbaSessionProperty.URL));
    assertEquals(user, se.getProperties().get(AdbaSessionProperty.USER));
    assertNull(se.getProperties().get(AdbaSessionProperty.PASSWORD));
    // (2, 4)
    assertEquals(Lifecycle.NEW, se.getSessionLifecycle());
    // (5)
    // The duplicate call verifies the no-op behavior.
    se.registerLifecycleListener(testListener);
    se.registerLifecycleListener(testListener);
    // (3, 4)
    se.submit();
    se.attachOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.ATTACHED, se.getSessionLifecycle());
    // (4, 6)
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.CLOSED, se.getSessionLifecycle());
    // (6)
    // The duplicate close operation verifies the no-op behavior.
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // (2, 3, 5, 6)
    LifecycleEvent[] expected = { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
    LifecycleEvent[] actual = testListener.record.toArray(new LifecycleEvent[0]);
    assertArrayEquals(expected, actual);
}
Also used : LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with SessionLifecycleListener

use of jdk.incubator.sql2.Session.SessionLifecycleListener in project oracle-db-examples by oracle-samples.

the class SessionTest method testSessionBuilder.

/**
 * Verifies the following behavior:
 * <br>
 * (1) Session.Builder.property(SessionProperty, Object) will specify a
 * property and its value for the built Session.
 * [Spec: {@link Session.Builder#property(SessionProperty, Object)}]
 * Session.getProperties() will return the set of properties configured on
 * this Session excepting any sensitive properties.
 * [Spec: {@link Session#getProperties()}]
 * <br>
 * (2) A Session is initially in the Session.Lifecycle.NEW lifecycle state.
 * [Spec: {@link Session.Builder}]
 * <br>
 * (3) If an attach operation completes successfully and the lifecycle is
 * Session.Lifecycle.NEW -> Session.Lifecycle.OPEN.
 * [Spec: {@link Session#attachOperation()}]
 * <br>
 * (4) Session.getSessionLifecycle() returns the current lifecycle of this
 * Session. [Spec: {@link Session#getSessionLifecycle()}]
 * <br>
 * (5) Session.registerLifcycleListener(SessionLifecycleListener) registers
 * a listener that will be called whenever there is a change in the
 * lifecycle of this Session. If the listener is already registered this
 * is a no-op. [Spec:
 * {@link Session#registerLifecycleListener(SessionLifecycleListener)}]
 * (6) Session.closeOperation() returns an operation which, upon execution,
 * transitions the session's lifecyle from OPEN to CLOSING. When the
 * operation completes, and no other operations are enqueued, the lifecycle
 * transistions from CLOSING to CLOSED. If the operation is submitted when
 * the lifecycle is closed, the execution of the operation is a no-op.
 * [Spec: {@link Session#closeOperation()}]
 */
@Test
public void testSessionBuilder() throws Exception {
    TestLifecycleListener testListener = new TestLifecycleListener();
    final String url = getUrl();
    final String user = getUser();
    final String password = getPassword();
    Session se = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().build().builder().property(AdbaSessionProperty.URL, url).property(AdbaSessionProperty.USER, user).property(AdbaSessionProperty.PASSWORD, password).build();
    // (1)
    assertEquals(url, se.getProperties().get(AdbaSessionProperty.URL));
    assertEquals(user, se.getProperties().get(AdbaSessionProperty.USER));
    assertNull(se.getProperties().get(AdbaSessionProperty.PASSWORD));
    // (2, 4)
    assertEquals(Lifecycle.NEW, se.getSessionLifecycle());
    // (5)
    // The duplicate call verifies the no-op behavior.
    se.registerLifecycleListener(testListener);
    se.registerLifecycleListener(testListener);
    // (3, 4)
    se.submit();
    se.attachOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.ATTACHED, se.getSessionLifecycle());
    // (4, 6)
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    assertEquals(Lifecycle.CLOSED, se.getSessionLifecycle());
    // (6)
    // The duplicate close operation verifies the no-op behavior.
    se.closeOperation().timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // (2, 3, 5, 6)
    LifecycleEvent[] expected = { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
    LifecycleEvent[] actual = testListener.record.toArray(new LifecycleEvent[0]);
    assertArrayEquals(expected, actual);
}
Also used : LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Aggregations

LifecycleEvent (com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent)2 Session (jdk.incubator.sql2.Session)2 Test (org.junit.Test)2