]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest4.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest4.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.util.Collection;
15
16 import org.junit.Test;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.ReadRequest;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.exception.ValidationException;
25 import org.simantics.db.testing.annotation.Fails;
26 import org.simantics.db.testing.common.TestBase;
27 import org.simantics.db.testing.common.Tests;
28 import org.simantics.db.testing.impl.Configuration;
29 import org.simantics.layer0.Layer0;
30
31 public class ConnectionTest4 {
32         private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
33         private static final boolean SKIP = Configuration.get().skipServerCreation;
34         private Session session = null;
35     @Test
36     @Fails
37         public void test4() throws DatabaseException {
38             if (SKIP) {
39                 System.out.println("This test is not valid for remote server.");
40                 return;
41             }
42         for (int i=0; i<RECONNECT_COUNT; ++i) {
43             connectionTest();
44         }
45         }
46         void connectionTest() throws DatabaseException {
47         try {
48             session = Tests.getTestHandler().getSession();
49             final String newInstanceName ="ConnectionTest4InstanceName-" +
50                 TestBase.getRandomString();
51
52             session.syncRequest(new WriteRequest() {
53                 @Override
54                 public void perform(WriteGraph g) throws DatabaseException {
55                     Layer0 b = Layer0.getInstance(g);
56                     Resource newResource = g.newResource();
57                     g.claim(newResource, b.InstanceOf, null, b.Type);
58                     g.claimLiteral(newResource, b.HasName, newInstanceName);
59                     g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
60                 }
61             });
62
63             session.syncRequest(new ReadRequest() {
64                 @Override
65                 public void run(ReadGraph g) throws DatabaseException {
66                     Layer0 b = Layer0.getInstance(g);
67                     Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
68                     Resource newResource = null;
69                     for (Resource r : resources) {
70                         String value = g.getPossibleRelatedValue(r, b.HasName);
71                         if (null != value && value.equals(newInstanceName)) {
72                             newResource = r;
73                             break;
74                         }
75                     }
76                     if (newResource == null) {
77                         throw new ValidationException("Could not find created resource");
78                     }
79                     if (!g.isInstanceOf(newResource, b.Type))
80                         throw new ValidationException("Created resource is not an instance of Type");
81                 }
82             });
83             Tests.closeSession(session);
84             session = Tests.getTestHandler().getSession();
85             session.syncRequest(new ReadRequest() {
86                 @Override
87                 public void run(ReadGraph g) throws DatabaseException {
88                     Layer0 b = Layer0.getInstance(g);
89                     Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
90                     Resource newResource = null;
91                     for (Resource r : resources) {
92                         String value = g.getPossibleRelatedValue(r, b.HasName);
93                         if (null != value && value.equals(newInstanceName)) {
94                             newResource = r;
95                             break;
96                         }
97                     }
98                     if (newResource == null) {
99                         throw new ValidationException("Could not find created resource");
100                     }
101                     if (!g.isInstanceOf(newResource, b.Type))
102                         throw new ValidationException("Created resource is not an instance of Type");
103                 }
104             });
105             Tests.closeSession(session);
106 //        } catch (ClassNotFoundException e) {
107 //            throw new DatabaseException("Test failed.", e);
108         } finally {
109             Tests.closeSession(session);
110         }
111         }
112 }