]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.client;
13
14 import java.io.IOException;
15
16 import org.junit.Test;
17 import org.simantics.db.Session;
18 import org.simantics.db.event.SessionEvent;
19 import org.simantics.db.event.SessionListener;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.service.LifecycleSupport;
22 import org.simantics.db.testing.base.ExistingDatabaseTest;
23 import org.simantics.db.testing.common.Tests;
24 import org.simantics.db.testing.impl.Configuration;
25
26 import fi.vtt.simantics.procore.SessionManagerSource;
27
28 public class ConnectionTest2 extends ExistingDatabaseTest {
29 //      private static final  String USERNAME = Configuration.get().username;
30 //      private static final String PASSWORD = Configuration.get().password;
31         private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
32
33     private int sessionOpened = 0;
34     private int sessionClosed = 0;
35     private synchronized void incOpened() {
36         ++sessionOpened;
37     }
38     private synchronized void incClosed() {
39         ++sessionClosed;
40     }
41     @Test
42         public void test2() throws DatabaseException {
43             SessionListener sl = new SessionListener() {
44             @Override
45             public void sessionClosed(SessionEvent e) {
46                 if (null != e.getCause())
47                     fail("Session closed with exception: " + e.getCause().getMessage());
48                 else
49                     incClosed();
50             }
51
52             @Override
53             public void sessionOpened(SessionEvent e) {
54                 if (null != e.getCause())
55                     fail("Session opened with exception: " + e.getCause().getMessage());
56                 else
57                     incOpened();
58             }
59
60             @Override
61             public void sessionException(SessionEvent e) {
62                 if (null == e.getCause())
63                     fail("Session threw null exception");
64                 else
65                     fail("Session threw exception: " + e.getCause().getMessage());
66             }
67         };
68         try {
69             SessionManagerSource.getSessionManager().addSessionListener(sl);
70             for (int i=0; i<RECONNECT_COUNT; ++i) {
71                 connectionTest();
72             }
73             assertTrue("Open/Closed count mismatch!", sessionOpened == sessionClosed);
74         } catch (IOException e) {
75             throw new DatabaseException("Test failed.", e);
76         } finally {
77             // Not totally correct because we do this even if add failed, but...
78             try {
79                 SessionManagerSource.getSessionManager().removeSessionListener(sl);
80             } catch (IOException e1) {
81                 throw new DatabaseException("Test failed.", e1);
82             }
83         }
84         }
85
86         void connectionTest() throws DatabaseException {
87         try {
88             Session session = Tests.getTestHandler().getSession();
89             LifecycleSupport ls = session.getService(LifecycleSupport.class);
90             ls.close();
91 //        } catch (ClassNotFoundException e) {
92 //            throw new DatabaseException("Test failed.", e);
93         } finally {
94         }
95         }
96 }