]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest6.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest6.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.File;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
18
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;
35
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";
49     @Test
50     @Fails
51         public void test6() throws Exception {
52             if (SKIP) {
53                 System.out.println("This test is not valid for remote server.");
54                 return;
55             }
56         for (int i=0; i<RECONNECT_COUNT; ++i) {
57             connectionTest();
58         }
59         }
60     void connectionTest() throws Exception {
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 Exception {
70         session = Tests.getTestHandler().getSession();
71         try {
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             session.syncRequest(new ReadRequest() {
83                 @Override
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)) {
91                             newResource = r;
92                             break;
93                         }
94                     }
95                     if (newResource == null) {
96                         throw new ValidationException("Could not find created resource");
97                     }
98                 }
99             });
100             LifecycleSupport ls = session.getService(LifecycleSupport.class);
101             ls.close();
102         } catch (DatabaseException e) {
103             throw e;
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 b = Layer0.getInstance(g);
114         for (Resource r : resources) {
115             String value = g.getPossibleRelatedValue(r, b.HasName);
116             if (null != value && value.equals(name)) {
117                 newResource = r;
118                 break;
119             }
120         }
121         return newResource;
122     }
123     private static Resource newResource;
124     String saveTest(final int id) throws Exception {
125         try {
126             session = Tests.getTestHandler().getSession();
127                         final String newInstanceName = "" + id + newInstanceSuffix;
128                         final String desc = "" + id;
129
130                         session.syncRequest(new WriteRequest() {
131                     @Override
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);
142                                 }
143                         });
144
145             LifecycleSupport ls = session.getService(LifecycleSupport.class);
146                 session.syncRequest(new ReadRequest() {
147                     @Override
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);
157                                         if (!s.equals(desc))
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");
161                                 }
162                 });
163             ls.close();
164             return newInstanceName;
165         } catch (DatabaseException e) {
166             throw e;
167         } finally {
168             Tests.closeSession(session);
169         }
170     }
171     void checkTest(final List<String> names) throws Exception {
172         try {
173             session = Tests.getTestHandler().getSession();
174             session.syncRequest(new ReadRequest() {
175                 @Override
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);
185                     }
186                 }
187             });
188             LifecycleSupport ls = session.getService(LifecycleSupport.class);
189             ls.close(-1, false);
190             if (DEBUG)
191                 System.out.println("close done");
192         } catch (DatabaseException e) {
193             throw e;
194         } finally {
195             Tests.closeSession(session);
196         }
197     }
198 }