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.scalability;
14 import java.util.Collection;
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;
26 public class LargeImportTest extends ExistingDatabaseTest {
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;
33 * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct.
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() {
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);
54 session.syncRequest(new WriteOnlyQuery() {
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);
76 // if (((flush++)%10000)==0)
84 session.syncRequest(new TestReadRequest() {
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);
95 if (name.equals(newTypeName)) {
98 if (!name.startsWith(random, 0))
100 name = name.substring(random.length());
103 index = Integer.parseInt(name);
104 } catch (NumberFormatException e) {
105 continue; // not a number
107 if (index > 0 && index < OUTER_INSTANCE_COUNT+1) {
108 newInstance[index-1] = r;
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);
133 fail("Inner instance name is not correct.");
134 Integer.parseInt(name);