1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.client;
14 import java.io.IOException;
16 import org.junit.Test;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Session;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.event.SessionEvent;
21 import org.simantics.db.event.SessionListener;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.testing.annotation.Fails;
24 import org.simantics.db.testing.common.TestBase;
25 import org.simantics.db.testing.common.Tests;
26 import org.simantics.db.testing.impl.Configuration;
27 import org.simantics.layer0.Layer0;
29 import fi.vtt.simantics.procore.SessionManagerSource;
31 public class ConnectionTest3 extends TestBase {
33 // private static final String HOST = Configuration.get().host;
34 // private static final Integer PORT = Configuration.get().port;
35 // private static final String USERNAME = Configuration.get().username;
36 // private static final String PASSWORD = Configuration.get().password;
37 private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
38 private static final boolean SKIP = Configuration.get().skipServerCreation;
39 // private CoreHandler core = new CoreHandler();
40 private Session session = null;
41 private int sessionOpened = 0;
42 private int sessionClosed = 0;
43 private int sessionExcept = 0;
44 private synchronized void incOpened() {
47 private synchronized void incExcept() {
50 private synchronized void incClosed() {
55 public void test3() throws DatabaseException {
57 System.out.println("This test is not valid for remote server.");
60 SessionListener sl = new SessionListener() {
62 public void sessionClosed(SessionEvent e) {
63 if (null == e.getCause())
64 fail("Session closed without exception!");
70 public void sessionOpened(SessionEvent e) {
71 if (null != e.getCause())
72 fail("Session opened with exception: " + e.getCause().getMessage());
78 public void sessionException(SessionEvent e) {
79 if (null == e.getCause())
80 fail("Session threw null exception");
86 SessionManagerSource.getSessionManager().addSessionListener(sl);
88 for (int i = 0; i < RECONNECT_COUNT; ++i) {
91 assertTrue("Open/Closed count mismatch!", sessionOpened == sessionClosed);
92 assertTrue("Except/Closed count mismatch!", sessionExcept == sessionClosed);
93 } catch (IOException e) {
94 throw new DatabaseException("Test failed.", e);
96 // Not totally correct because we do this even if add failed, but...
98 SessionManagerSource.getSessionManager().removeSessionListener(sl);
99 } catch (IOException e1) {
100 throw new DatabaseException("Test failed.", e1);
105 void connectionTest() throws DatabaseException {
107 session = Tests.getTestHandler().getSession();
108 // } catch (ClassNotFoundException e) {
109 // throw new DatabaseException("Test failed.", e);
110 } catch (Throwable t) {
111 fail("ConnectionTest failed.", t);
113 Tests.closeSession(session);
115 session.syncRequest(new ReadRequest() {
117 public void run(ReadGraph g) throws DatabaseException {
118 Layer0 b = Layer0.getInstance(g);
120 throw new DatabaseException("Could not get builtins.");
123 } catch (DatabaseException e) {
124 // This is what was expected.
127 fail("Session.syncRequest exception handling failed.");