]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/story/AddStatementTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / story / AddStatementTest.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.api.write.story;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
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.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.request.Read;
26 import org.simantics.db.service.LifecycleSupport;
27 import org.simantics.db.testing.base.SimpleBase;
28 import org.simantics.db.testing.common.TestBase;
29 import org.simantics.db.testing.common.Tests;
30 import org.simantics.layer0.Layer0;
31 public class AddStatementTest extends SimpleBase {
32     private static int NR = 1; //1<<14;
33     private static int NS = 1<<16; // 4;
34     private final String stms = "Statements" + getRandomString();
35     private final String types = "Types" + getRandomString();
36     public AddStatementTest() {
37     }
38         @Test
39     public void testAddStatement() throws DatabaseException {
40         final Session s = getSession();
41         addStatements(s);
42         checkStatements(s);
43         final Session s2 = Tests.getTestHandler().getSession();
44         try {
45             checkStatements(s2);
46         } finally {
47             LifecycleSupport ls = s2.getService(LifecycleSupport.class);
48             ls.close();
49         }
50     }
51     private void addStatements(Session s) throws DatabaseException {
52         s.syncRequest(new WriteRequest() {
53             @Override
54             public void perform(WriteGraph g) throws DatabaseException {
55                 Layer0 b = Layer0.getInstance(g);
56
57                 Resource rs = g.newResource();
58                 g.claim(rs, b.InstanceOf, null, b.Library);
59                 g.claimLiteral(rs, b.HasName, stms);
60                 g.claim(rl, b.ConsistsOf, rs);
61
62                 Resource rt = g.newResource();
63                 g.claim(rt, b.InstanceOf, null, b.Library);
64                 g.claimLiteral(rt, b.HasName, types);
65                 g.claim(rl, b.ConsistsOf, rt);
66
67                 g.flushCluster();
68
69                 Resource[] typesR = new Resource[NS];
70                 for (int i=0; i<NS; ++i) {
71                     typesR[i] = g.newResource();
72                     g.claim(typesR[i], b.InstanceOf, b.Type);
73                     g.claim(rt, b.ConsistsOf, typesR[i]);
74                 }
75
76                 for (int i=0; i<NR; ++i) {
77                     Resource r = g.newResource();
78                     g.claim(rs, b.ConsistsOf, r);
79                     for (int j=0; j<NS; ++j) {
80                         g.claim(r, b.InstanceOf, typesR[j]);
81                     }
82                 }
83             }
84         });
85     }
86     private void checkStatements(Session s) throws DatabaseException {
87         s.syncRequest(new Read<Resource>() {
88             @Override
89             public Resource perform(ReadGraph g) throws DatabaseException {
90                 Layer0 l0 = Layer0.getInstance(g);
91
92                 Resource rs = g.getResource(TestBase.ROOT_LIBRARY_URI + "/" + stms);
93                 assertTrue(null != rs);
94                 Collection<Resource> stmsR = g.getObjects(rs, l0.ConsistsOf);
95                 if (stmsR.size() != NR)
96                     fail("Resource count does not match. count=" + stmsR.size());
97
98                 Resource rt = g.getResource(TestBase.ROOT_LIBRARY_URI + "/" + types);
99                 assertTrue(null != rs);
100                 Collection<Resource> typesR = g.getObjects(rt, l0.ConsistsOf);
101                 if (typesR.size() != NS)
102                     fail("Types count does not match. count=" + typesR.size());
103
104                 Set<Resource> typesS = new HashSet<Resource>();
105                 for (Resource r : typesR) {
106                     typesS.add(r);
107                 }
108                 for (Resource r : stmsR) {
109                     Collection<Resource> instances = g.getObjects(r, l0.InstanceOf);
110                     if (instances.size() != NS)
111                         fail("Instance count does not match. count=" + instances.size());
112                     for (Resource ri : instances)
113                         if (!typesS.contains(ri))
114                             fail("Instance typet does not match. instance=" + ri);
115                 }
116                 return null;
117             }
118         });
119     }
120 }