]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/ConnectionTest1.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / ConnectionTest1.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.WriteRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.exception.ValidationException;
24 import org.simantics.db.service.LifecycleSupport;
25 import org.simantics.db.testing.base.ExistingDatabaseTest;
26 import org.simantics.db.testing.common.Tests;
27 import org.simantics.db.testing.impl.Configuration;
28 import org.simantics.layer0.Layer0;
29
30 public class ConnectionTest1 extends ExistingDatabaseTest {
31 //      private static final  String USERNAME = Configuration.get().username;
32 //      private static final String PASSWORD = Configuration.get().password;
33         private static final int RECONNECT_COUNT = Configuration.get().connectionReconnectCount;
34
35     @Test
36         public void test1() throws DatabaseException {
37             Session session = null;
38         for (int i=0; i<RECONNECT_COUNT; ++i) {
39             connectionTest(session);
40         }
41         }
42         void connectionTest(Session session) throws DatabaseException {
43         try {
44             session = Tests.getTestHandler().getSession();
45             boolean TEST_QUERY = true;
46             if (TEST_QUERY) {
47                 final String newInstanceName ="ConnectionTest1InstanceName-" +
48                     getRandomString();
49                 session.syncRequest(new WriteRequest() {
50                     @Override
51                     public void perform(WriteGraph g) throws DatabaseException {
52                         Layer0 b = Layer0.getInstance(g);
53                         Resource newResource = g.newResource();
54                         g.claim(newResource, b.InstanceOf, null, b.Type);
55                         g.claimLiteral(newResource, b.HasName, newInstanceName);
56                         g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
57                     }
58                 });
59
60                 session.syncRequest(new TestReadRequest() {
61                     @Override
62                     public void run(ReadGraph g) throws DatabaseException {
63                         Layer0 b = Layer0.getInstance(g);
64                         Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
65                         Resource newResource = null;
66                         for (Resource r : resources) {
67                             String value = g.getPossibleRelatedValue(r, b.HasName);
68                             if (null != value && value.equals(newInstanceName)) {
69                                 newResource = r;
70                                 break;
71                             }
72                         }
73                         if (newResource == null) {
74                             throw new ValidationException("Could not find created resource");
75                         }
76                         if (!g.isInstanceOf(newResource, b.Type))
77                             throw new ValidationException("Created resource is not an instance of Type");
78                     }
79                 });
80             }
81             LifecycleSupport ls = session.getService(LifecycleSupport.class);
82             ls.close();
83 //        } catch (ClassNotFoundException e) {
84 //            throw new DatabaseException("Test failed.", e);
85         } finally {
86         }
87         }
88 }