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.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
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;
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;
45 public void test5() throws DatabaseException {
47 System.out.println("This test is not valid for remote server.");
50 // Try to load and register the Driver instance with the driver manager
52 Class.forName("fi.vtt.simantics.procore.ProCoreDriver");
53 } catch (ClassNotFoundException e) {
54 throw new DatabaseException("Test failed.", e);
56 for (int i=0; i<RECONNECT_COUNT; ++i) {
60 void connectionTest() throws DatabaseException {
62 List<String> names = new ArrayList<String>();
63 for (int i=0; i<SAVE_COUNT; ++i) {
64 String name = saveTest(i+1);
69 void initTest() throws DatabaseException {
71 session = Tests.getTestHandler().getSession();
72 session.syncRequest(new WriteRequest() {
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);
83 session.syncRequest(new ReadRequest() {
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)) {
97 if (newResource == null) {
98 throw new ValidationException("Could not find created resource");
102 LifecycleSupport ls = session.getService(LifecycleSupport.class);
105 Tests.closeSession(session);
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)) {
123 String saveTest(int id) throws DatabaseException {
125 session = Tests.getTestHandler().getSession();
126 final String subLibraryName = "" + id + random;
127 session.syncRequest(new WriteRequest() {
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);
148 session.syncRequest(new ReadRequest() {
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);
167 LifecycleSupport ls = session.getService(LifecycleSupport.class);
169 return subLibraryName;
171 Tests.closeSession(session);
174 void checkTest(final List<String> names) throws DatabaseException {
176 session = Tests.getTestHandler().getSession();
177 session.syncRequest(new ReadRequest() {
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);
198 LifecycleSupport ls = session.getService(LifecycleSupport.class);
200 // } catch (ClassNotFoundException e) {
201 // throw new DatabaseException("Test failed.", e);
203 Tests.closeSession(session);