/******************************************************************************* * 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.ReadGraph; import org.simantics.db.Session; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.event.SessionEvent; import org.simantics.db.event.SessionListener; import org.simantics.db.exception.DatabaseException; import org.simantics.db.testing.annotation.Fails; import org.simantics.db.testing.common.TestBase; import org.simantics.db.testing.common.Tests; import org.simantics.db.testing.impl.Configuration; import org.simantics.layer0.Layer0; import fi.vtt.simantics.procore.SessionManagerSource; public class ConnectionTest3 extends TestBase { // private static final String HOST = Configuration.get().host; // private static final Integer PORT = Configuration.get().port; // 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 static final boolean SKIP = Configuration.get().skipServerCreation; // private CoreHandler core = new CoreHandler(); private Session session = null; private int sessionOpened = 0; private int sessionClosed = 0; private int sessionExcept = 0; private synchronized void incOpened() { ++sessionOpened; } private synchronized void incExcept() { ++sessionExcept; } private synchronized void incClosed() { ++sessionClosed; } @Test @Fails public void test3() throws DatabaseException { if (SKIP) { System.out.println("This test is not valid for remote server."); return; } SessionListener sl = new SessionListener() { @Override public void sessionClosed(SessionEvent e) { if (null == e.getCause()) fail("Session closed without exception!"); 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 incExcept(); } }; try { SessionManagerSource.getSessionManager().addSessionListener(sl); for (int i = 0; i < RECONNECT_COUNT; ++i) { connectionTest(); } assertTrue("Open/Closed count mismatch!", sessionOpened == sessionClosed); assertTrue("Except/Closed count mismatch!", sessionExcept == 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 = Tests.getTestHandler().getSession(); // } catch (ClassNotFoundException e) { // throw new DatabaseException("Test failed.", e); } catch (Throwable t) { fail("ConnectionTest failed.", t); } Tests.closeSession(session); try { session.syncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); if (null == b) throw new DatabaseException("Could not get builtins."); } }); } catch (DatabaseException e) { // This is what was expected. return; } fail("Session.syncRequest exception handling failed."); } }