]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/scalability/LargeImportTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / scalability / LargeImportTest.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.scalability;
13
14 import java.util.Collection;
15
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.WriteOnlyGraph;
22 import org.simantics.db.testing.base.ExistingDatabaseTest;
23 import org.simantics.db.tests.common.Configuration;
24 import org.simantics.layer0.Layer0;
25
26 public class LargeImportTest extends ExistingDatabaseTest {
27
28            private static final int OUTER_INSTANCE_COUNT = Configuration.get().importOuterCount; // these go to the root library
29            private static final int INNER_INSTANCE_COUNT = Configuration.get().importInnerCount; // these are for each outer instance
30        private static Layer0 l0;
31
32             /**
33              * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct. 
34              * @throws Exception
35              */
36         @Test
37             public void testImport() throws Exception{
38                 Session session = getSession();
39                 final Resource rootLib = getSession().getRootLibrary();
40                 final String random = getRandomString();
41                 final String newTypeName = "New Type " + random; 
42                 session.syncRequest(new TestReadRequest() {
43                     @Override
44                     public void run(ReadGraph g) throws Exception  {
45                         l0 = Layer0.getInstance(g);
46                         assert(null != l0.InstanceOf);
47                         assert(null != l0.Type);
48                         assert(null != l0.Inherits);
49                         assert(null != l0.SupertypeOf);
50                         assert(null != l0.HasName);
51                         assert(null != l0.ConsistsOf);
52                     }
53                 });
54                 session.syncRequest(new WriteOnlyQuery() {
55                                 @Override
56                                 public void run(WriteOnlyGraph g) throws Throwable {
57                                         Resource newType = g.newResource();
58                         g.claim(newType, l0.InstanceOf, null, l0.Type);
59                         g.claim(newType, l0.Inherits, l0.SupertypeOf, l0.Type);
60                         g.claimValue(newType, newTypeName, Bindings.STRING);
61                         g.claim(rootLib, l0.ConsistsOf, l0.PartOf, newType);
62                         for (int i=0; i<OUTER_INSTANCE_COUNT; ++i) {
63                             //System.out.println("Write outer=" + i);
64                             Resource outerInstance = g.newResource();
65                             g.claim(outerInstance, l0.InstanceOf, null, newType);
66                             String outerInstanceName = random+(i+1);
67                             g.claimValue(outerInstance, outerInstanceName, Bindings.STRING);
68                             g.claim(rootLib, l0.ConsistsOf, l0.PartOf, outerInstance);
69                             for (int j=0; j<INNER_INSTANCE_COUNT; ++j) {
70                                 Resource innerInstance = g.newResource();
71                                 g.claim(innerInstance, l0.InstanceOf, null, newType);
72                                 String innerInstanceName = ""+(j+1);
73                                 g.claimValue(innerInstance, innerInstanceName, Bindings.STRING);
74                                 g.claim(outerInstance, l0.ConsistsOf, l0.PartOf, innerInstance);
75                             }
76 //                          if (((flush++)%10000)==0)
77 //                              g.flushCluster();
78                         }
79                     }
80                 });
81                 
82                 checkException();
83                 
84                 session.syncRequest(new TestReadRequest() {
85                     @Override
86                     public void run(ReadGraph g) throws Exception  {
87                         Layer0 l0 =Layer0.getInstance(g);
88                         Collection<Resource> resources = g.getObjects(rootLib, l0.ConsistsOf);
89                         Resource newType = null;
90                         Resource newInstance[] = new Resource[OUTER_INSTANCE_COUNT];
91                         for (Resource r : resources) {
92                                 String name = g.getPossibleValue(r, Bindings.STRING);
93                             if (null == name)
94                                 continue;
95                             if (name.equals(newTypeName)) {
96                                 newType = r;
97                             }
98                             if (!name.startsWith(random, 0))
99                                 continue;
100                             name = name.substring(random.length());
101                             int index = 0;
102                             try {
103                                 index = Integer.parseInt(name);
104                             } catch (NumberFormatException e) {
105                                 continue; // not a number
106                             }
107                             if (index > 0 && index < OUTER_INSTANCE_COUNT+1) {
108                                 newInstance[index-1] = r;
109                             }
110                         }
111                         if (newType == null) 
112                             throw new Exception("Could not find created type");
113                         for (int i=0; i<OUTER_INSTANCE_COUNT; ++i) {
114                             //System.out.println("Read outer=" + i);
115                             if (newInstance[i] == null) 
116                                 throw new Exception("Could not find created instance");
117                             if (!g.isInstanceOf(newInstance[i], l0.Type))
118                                 throw new Exception("Created resource is not an instance of Type");
119                             if (!g.isInstanceOf(newInstance[i], newType))
120                                 throw new Exception("Created resource is not an instance of created type");
121                             resources = g.getObjects(newInstance[i], l0.ConsistsOf);
122                             if (resources.size() != INNER_INSTANCE_COUNT)
123                                 fail("Inner instance count does not match." + 
124                                         " Read count=" + resources.size() +
125                                         ", expected=" + INNER_INSTANCE_COUNT);
126                             for (Resource r : resources) {
127                                 if (!g.isInstanceOf(r, l0.Type))
128                                     throw new Exception("Created resource is not an instance of Type");
129                                 if (!g.isInstanceOf(r, newType))
130                                     throw new Exception("Created resource is not an instance of created type");
131                                 String name = g.getPossibleValue(r, Bindings.STRING);
132                                 if (null == name)
133                                     fail("Inner instance name is not correct.");
134                             Integer.parseInt(name);
135                             }
136                         }
137                     }
138                 });
139                 
140                 checkException();
141                 
142             }
143 }