]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest5.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest5.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.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.ValidationException;
27 import org.simantics.db.service.LifecycleSupport;
28 import org.simantics.db.testing.annotation.Fails;
29 import org.simantics.db.testing.common.TestBase;
30 import org.simantics.db.testing.common.Tests;
31 import org.simantics.db.testing.impl.Configuration;
32 import org.simantics.layer0.Layer0;
33
34 public class ConnectionTest5 extends TestBase {
35         private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
36     private static final int SAVE_COUNT = Configuration.get().connectionSaveCount;
37     private static final int INSTANCE_COUNT = Configuration.get().connectionInstanceCount;
38         private static final boolean SKIP = Configuration.get().skipServerCreation;
39         private Session session = null;
40         private final String random = "ConnectionTest5" + TestBase.getRandomString();
41     private final String libraryName = "Library" + random;
42
43     @Test
44     @Fails
45         public void test5() throws DatabaseException {
46             if (SKIP) {
47                 System.out.println("This test is not valid for remote server.");
48                 return;
49             }
50         // Try to load and register the Driver instance with the driver manager
51         try {
52             Class.forName("fi.vtt.simantics.procore.ProCoreDriver");
53         } catch (ClassNotFoundException e) {
54             throw new DatabaseException("Test failed.", e);
55         }
56         for (int i=0; i<RECONNECT_COUNT; ++i) {
57             connectionTest();
58         }
59         }
60     void connectionTest() throws DatabaseException {
61         initTest();
62         List<String> names = new ArrayList<String>();
63         for (int i=0; i<SAVE_COUNT; ++i) {
64              String name = saveTest(i+1);
65              names.add(name);
66         }
67         checkTest(names);
68     }
69     void initTest() throws DatabaseException {
70         try {
71             session = Tests.getTestHandler().getSession();
72             session.syncRequest(new WriteRequest() {
73                 @Override
74                 public void perform(WriteGraph g) throws DatabaseException {
75                     Layer0 b = Layer0.getInstance(g);
76                     Resource newResource = g.newResource();
77                     g.claim(newResource, b.InstanceOf, null, b.Library);
78                     g.claimLiteral(newResource, b.HasName, libraryName);
79                     g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
80                 }
81             });
82
83             session.syncRequest(new ReadRequest() {
84                 @Override
85                 public void run(ReadGraph g) throws DatabaseException {
86                     Layer0 l0 = Layer0.getInstance(g);
87                     Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
88                     Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
89                     Resource newResource = null;
90                     for (Resource r : resources) {
91                         String value = g.getPossibleRelatedValue(r, l0.HasName);
92                         if (null != value && value.equals(libraryName)) {
93                             newResource = r;
94                             break;
95                         }
96                     }
97                     if (newResource == null) {
98                         throw new ValidationException("Could not find created resource");
99                     }
100                 }
101             });
102             LifecycleSupport ls = session.getService(LifecycleSupport.class);
103             ls.close();
104         } finally {
105             Tests.closeSession(session);
106         }
107         }
108     private Resource getResourceByNameAndRelation(ReadGraph g,
109             Resource parent, Resource relation, String name)
110     throws DatabaseException {
111         Collection<Resource> resources = g.getObjects(parent, relation);
112         Resource newResource = null;
113         Layer0 l0 = Layer0.getInstance(g);
114         for (Resource r : resources) {
115             String value = g.getPossibleRelatedValue(r, l0.HasName);
116             if (null != value && value.equals(name)) {
117                 newResource = r;
118                 break;
119             }
120         }
121         return newResource;
122     }
123     String saveTest(int id) throws DatabaseException {
124         try {
125             session = Tests.getTestHandler().getSession();
126             final String subLibraryName = "" + id + random;
127             session.syncRequest(new WriteRequest() {
128                 @Override
129                 public void perform(WriteGraph g) throws DatabaseException {
130                     Layer0 b = Layer0.getInstance(g);
131                     Resource subLibrary = g.newResource();
132                     g.claim(subLibrary, b.InstanceOf, null, b.Library);
133                     g.claimLiteral(subLibrary, b.HasName, subLibraryName);
134                     Resource library = getResourceByNameAndRelation(g,
135                             g.getRootLibrary(), b.ConsistsOf, libraryName);
136                     assertTrue(null != library);
137                     g.claim(library, b.ConsistsOf, b.PartOf, subLibrary);
138                     for (int j=0; j<INSTANCE_COUNT; ++j) {
139                         Resource instance = g.newResource();
140                         g.claim(instance, b.InstanceOf, null, b.Library);
141                         String instanceName = ""+(j+1);
142                         g.claimLiteral(instance, b.HasName, instanceName);
143                         g.claim(subLibrary, b.ConsistsOf, b.PartOf, instance);
144                     }
145                 }
146             });
147
148             session.syncRequest(new ReadRequest() {
149                 @Override
150                 public void run(ReadGraph g) throws DatabaseException {
151                     Layer0 l0 = Layer0.getInstance(g);
152                     Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
153                     Resource library = getResourceByNameAndRelation(g,
154                             rl, l0.ConsistsOf, libraryName);
155                     assertTrue(null != library);
156                     Resource subLibrary = getResourceByNameAndRelation(g,
157                             library, l0.ConsistsOf, subLibraryName);
158                     assertTrue(null != subLibrary);
159                     for (int j=0; j<INSTANCE_COUNT; ++j) {
160                         String name = ""+(j+1);
161                         Resource instance = getResourceByNameAndRelation(g,
162                                 subLibrary, l0.ConsistsOf, name);
163                         assertTrue(instance != null);
164                     }
165                 }
166             });
167             LifecycleSupport ls = session.getService(LifecycleSupport.class);
168             ls.close();
169             return subLibraryName;
170         } finally {
171             Tests.closeSession(session);
172         }
173     }
174     void checkTest(final List<String> names) throws DatabaseException {
175         try {
176             session = Tests.getTestHandler().getSession();
177             session.syncRequest(new ReadRequest() {
178                 @Override
179                 public void run(ReadGraph g) throws DatabaseException {
180                     Layer0 l0 = Layer0.getInstance(g);
181                     Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
182                     Resource library = getResourceByNameAndRelation(g,
183                             rl, l0.ConsistsOf, libraryName);
184                     assertTrue(null != library);
185                     for (String libraryName : names) {
186                         Resource subLibrary = getResourceByNameAndRelation(g,
187                                 library, l0.ConsistsOf, libraryName);
188                         assertTrue(null != subLibrary);
189                         for (int j=0; j<INSTANCE_COUNT; ++j) {
190                             String name = ""+(j+1);
191                             Resource instance = getResourceByNameAndRelation(g,
192                                     subLibrary, l0.ConsistsOf, name);
193                             assertTrue(instance != null);
194                         }
195                     }
196                 }
197             });
198             LifecycleSupport ls = session.getService(LifecycleSupport.class);
199             ls.close();
200 //        } catch (ClassNotFoundException e) {
201 //            throw new DatabaseException("Test failed.", e);
202         } finally {
203             Tests.closeSession(session);
204         }
205     }
206 }