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;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
19 import org.junit.Test;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.common.request.WriteRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.exception.ValidationException;
29 import org.simantics.db.service.LifecycleSupport;
30 import org.simantics.db.testing.annotation.Fails;
31 import org.simantics.db.testing.common.TestBase;
32 import org.simantics.db.testing.common.Tests;
33 import org.simantics.db.testing.impl.Configuration;
34 import org.simantics.layer0.Layer0;
36 public class ConnectionTest6 extends TestBase {
37 private static final boolean DEBUG = Configuration.get().debug;
38 private static final String USERNAME = Configuration.get().username;
39 private static final String PASSWORD = Configuration.get().password;
40 private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
41 private static final int SAVE_COUNT = Configuration.get().connectionSaveCount;
42 private static final boolean SKIP = Configuration.get().skipServerCreation;
43 private static final File WS_DIR = new File(Configuration.get().workspace);
44 private Session session = null;
45 private final String random = "ConnectionTest6" + TestBase.getRandomString();
46 private final String libraryName = "Library" + random;
47 private final String newInstanceSuffix = "InstanceName" + random;
48 private final String WORK_DIR = "connection6";
51 public void test6() throws Exception {
53 System.out.println("This test is not valid for remote server.");
56 for (int i=0; i<RECONNECT_COUNT; ++i) {
60 void connectionTest() throws Exception {
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 Exception {
70 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);
82 session.syncRequest(new ReadRequest() {
84 public void run(ReadGraph g) throws DatabaseException {
85 Layer0 b = Layer0.getInstance(g);
86 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
87 Resource newResource = null;
88 for (Resource r : resources) {
89 String value = g.getPossibleRelatedValue(r, b.HasName);
90 if (null != value && value.equals(libraryName)) {
95 if (newResource == null) {
96 throw new ValidationException("Could not find created resource");
100 LifecycleSupport ls = session.getService(LifecycleSupport.class);
102 } catch (DatabaseException e) {
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 b = Layer0.getInstance(g);
114 for (Resource r : resources) {
115 String value = g.getPossibleRelatedValue(r, b.HasName);
116 if (null != value && value.equals(name)) {
123 private static Resource newResource;
124 String saveTest(final int id) throws Exception {
126 session = Tests.getTestHandler().getSession();
127 final String newInstanceName = "" + id + newInstanceSuffix;
128 final String desc = "" + id;
130 session.syncRequest(new WriteRequest() {
132 public void perform(WriteGraph g) throws DatabaseException {
133 Layer0 b = Layer0.getInstance(g);
134 newResource = g.newResource();
135 g.claim(newResource, b.InstanceOf, null, b.Type);
136 g.claimLiteral(newResource, b.HasName, newInstanceName);
137 Resource library = getResourceByNameAndRelation(g,
138 g.getRootLibrary(), b.ConsistsOf, libraryName);
139 assertTrue(null != library);
140 g.claim(library, b.ConsistsOf, newResource);
141 g.claimLiteral(newResource, b.HasDescription, desc);
145 LifecycleSupport ls = session.getService(LifecycleSupport.class);
146 session.syncRequest(new ReadRequest() {
148 public void run(ReadGraph g) throws DatabaseException {
149 Layer0 b = Layer0.getInstance(g);
150 Resource library = getResourceByNameAndRelation(g,
151 g.getRootLibrary(), b.ConsistsOf, libraryName);
152 assertTrue(null != library);
153 Resource newResource = getResourceByNameAndRelation(g,
154 library, b.ConsistsOf, newInstanceName);
155 assertTrue(null != newResource);
156 String s = g.getRelatedValue(newResource, b.HasDescription, Bindings.STRING);
158 throw new ValidationException("Value is not correct.");
159 if (!g.isInstanceOf(newResource, b.Type))
160 throw new ValidationException("Created resource is not an instance of Type");
164 return newInstanceName;
165 } catch (DatabaseException e) {
168 Tests.closeSession(session);
171 void checkTest(final List<String> names) throws Exception {
173 session = Tests.getTestHandler().getSession();
174 session.syncRequest(new ReadRequest() {
176 public void run(ReadGraph g) throws DatabaseException {
177 Layer0 b = Layer0.getInstance(g);
178 Resource library = getResourceByNameAndRelation(g,
179 g.getRootLibrary(), b.ConsistsOf, libraryName);
180 assertTrue(null != library);
181 for (String instanceName : names) {
182 Resource instance = getResourceByNameAndRelation(g,
183 library, b.ConsistsOf, instanceName);
184 assertTrue(instance != null);
188 LifecycleSupport ls = session.getService(LifecycleSupport.class);
191 System.out.println("close done");
192 } catch (DatabaseException e) {
195 Tests.closeSession(session);