+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.client;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.simantics.db.Session;
+import org.simantics.db.event.SessionEvent;
+import org.simantics.db.event.SessionListener;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.testing.common.Tests;
+import org.simantics.db.testing.impl.Configuration;
+
+import fi.vtt.simantics.procore.SessionManagerSource;
+
+public class ConnectionTest2 extends ExistingDatabaseTest {
+// private static final String USERNAME = Configuration.get().username;
+// private static final String PASSWORD = Configuration.get().password;
+ private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
+
+ private int sessionOpened = 0;
+ private int sessionClosed = 0;
+ private synchronized void incOpened() {
+ ++sessionOpened;
+ }
+ private synchronized void incClosed() {
+ ++sessionClosed;
+ }
+ @Test
+ public void test2() throws DatabaseException {
+ SessionListener sl = new SessionListener() {
+ @Override
+ public void sessionClosed(SessionEvent e) {
+ if (null != e.getCause())
+ fail("Session closed with exception: " + e.getCause().getMessage());
+ else
+ incClosed();
+ }
+
+ @Override
+ public void sessionOpened(SessionEvent e) {
+ if (null != e.getCause())
+ fail("Session opened with exception: " + e.getCause().getMessage());
+ else
+ incOpened();
+ }
+
+ @Override
+ public void sessionException(SessionEvent e) {
+ if (null == e.getCause())
+ fail("Session threw null exception");
+ else
+ fail("Session threw exception: " + e.getCause().getMessage());
+ }
+ };
+ try {
+ SessionManagerSource.getSessionManager().addSessionListener(sl);
+ for (int i=0; i<RECONNECT_COUNT; ++i) {
+ connectionTest();
+ }
+ assertTrue("Open/Closed count mismatch!", sessionOpened == sessionClosed);
+ } catch (IOException e) {
+ throw new DatabaseException("Test failed.", e);
+ } finally {
+ // Not totally correct because we do this even if add failed, but...
+ try {
+ SessionManagerSource.getSessionManager().removeSessionListener(sl);
+ } catch (IOException e1) {
+ throw new DatabaseException("Test failed.", e1);
+ }
+ }
+ }
+
+ void connectionTest() throws DatabaseException {
+ try {
+ Session session = Tests.getTestHandler().getSession();
+ LifecycleSupport ls = session.getService(LifecycleSupport.class);
+ ls.close();
+// } catch (ClassNotFoundException e) {
+// throw new DatabaseException("Test failed.", e);
+ } finally {
+ }
+ }
+}