use of com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent in project oracle-db-examples by oracle.
the class SessionTest method testTryWithResources.
/**
* Verify that declaring a Session in try-with-resources closes the session
* when the try block exits.
* @throws Exception
*/
@Test
public void testTryWithResources() throws Exception {
Builder sessionBuilder = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).build().builder();
TestLifecycleListener listener = new TestLifecycleListener();
LifecycleEvent[] expectedEvents;
try (Session se = sessionBuilder.build()) {
se.registerLifecycleListener(listener);
se.submit();
se.attachOperation().timeout(getTimeout()).submit();
expectedEvents = new LifecycleEvent[] { new LifecycleEvent(se, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se, Lifecycle.ATTACHED, Lifecycle.CLOSING), new LifecycleEvent(se, Lifecycle.CLOSING, Lifecycle.CLOSED) };
}
for (LifecycleEvent expected : expectedEvents) {
LifecycleEvent actual = listener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
if (actual == null)
fail("Timeout waiting for lifecycle event: " + expected);
assertEquals(expected, actual);
}
}
Aggregations