]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest3.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest3.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.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;
28
29 import fi.vtt.simantics.procore.SessionManagerSource;
30
31 public class ConnectionTest3 extends TestBase {
32
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() {
45         ++sessionOpened;
46     }
47     private synchronized void incExcept() {
48         ++sessionExcept;
49     }
50     private synchronized void incClosed() {
51         ++sessionClosed;
52     }
53     @Test
54     @Fails
55     public void test3() throws DatabaseException {
56         if (SKIP) {
57             System.out.println("This test is not valid for remote server.");
58             return;
59         }
60         SessionListener sl = new SessionListener() {
61             @Override
62             public void sessionClosed(SessionEvent e) {
63                 if (null == e.getCause())
64                     fail("Session closed without exception!");
65                 else
66                     incClosed();
67             }
68
69             @Override
70             public void sessionOpened(SessionEvent e) {
71                 if (null != e.getCause())
72                     fail("Session opened with exception: " + e.getCause().getMessage());
73                 else
74                     incOpened();
75             }
76
77             @Override
78             public void sessionException(SessionEvent e) {
79                 if (null == e.getCause())
80                     fail("Session threw null exception");
81                 else
82                     incExcept();
83             }
84         };
85         try {
86             SessionManagerSource.getSessionManager().addSessionListener(sl);
87
88             for (int i = 0; i < RECONNECT_COUNT; ++i) {
89                 connectionTest();
90             }
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);
95         } finally {
96             // Not totally correct because we do this even if add failed, but...
97             try {
98                 SessionManagerSource.getSessionManager().removeSessionListener(sl);
99             } catch (IOException e1) {
100                 throw new DatabaseException("Test failed.", e1);
101             }
102         }
103     }
104
105     void connectionTest() throws DatabaseException {
106         try {
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);
112         }
113         Tests.closeSession(session);
114         try {
115             session.syncRequest(new ReadRequest() {
116                 @Override
117                 public void run(ReadGraph g) throws DatabaseException {
118                     Layer0 b = Layer0.getInstance(g);
119                     if (null == b)
120                         throw new DatabaseException("Could not get builtins.");
121                 }
122             });
123         } catch (DatabaseException e) {
124             // This is what was expected.
125             return;
126         }
127         fail("Session.syncRequest exception handling failed.");
128     }
129 }