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;
15 import java.util.UUID;
17 import org.junit.Test;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.testing.base.ExistingDatabaseTest;
23 import org.simantics.db.testing.common.WriteQuery;
24 import org.simantics.layer0.Layer0;
26 public class LargeCommitTest extends ExistingDatabaseTest {
28 private static final int OUTER_INSTANCE_COUNT = 10; // these go to the root library
29 private static final int INNER_INSTANCE_COUNT = 100; // these are for each outer instance
32 * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct.
36 public void testCommit() throws Exception{
37 Session session = getSession();
38 final Resource rootLib = getSession().getRootLibrary();
39 final String random = UUID.randomUUID().toString();
40 final String newTypeName = "New Type" + random;
41 session.syncRequest(new WriteQuery(this) {
43 public void run(WriteGraph g) throws Throwable {
44 Layer0 b = Layer0.getInstance(g);
45 Resource newType = g.newResource();
46 g.claim(newType, b.InstanceOf, null, b.Type);
47 g.claim(newType, b.Inherits, b.SupertypeOf, b.Type);
48 g.claimLiteral(newType, b.HasName, newTypeName);
49 g.claim(rootLib, b.ConsistsOf, newType);
51 for (int i=0; i<OUTER_INSTANCE_COUNT; ++i) {
52 //System.out.println("Write outer=" + i);
53 Resource outerInstance = g.newResource();
54 g.claim(outerInstance, b.InstanceOf, null, newType);
55 String outerInstanceName = random+(i+1);
56 g.claimLiteral(outerInstance, b.HasName, outerInstanceName);
57 g.claim(rootLib, b.ConsistsOf, outerInstance);
58 for (int j=0; j<INNER_INSTANCE_COUNT; ++j) {
59 Resource innerInstance = g.newResource();
60 g.claim(innerInstance, b.InstanceOf, null, newType);
61 String innerInstanceName = ""+(j+1);
62 g.claimLiteral(innerInstance, b.HasName, innerInstanceName);
63 g.claim(outerInstance, b.ConsistsOf, innerInstance);
71 session.syncRequest(new TestReadRequest() {
73 public void run(ReadGraph g) throws Exception {
74 Layer0 b = Layer0.getInstance(g);
75 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
76 Resource newType = null;
77 Resource newInstance[] = new Resource[OUTER_INSTANCE_COUNT];
78 for (Resource r : resources) {
79 String name = g.getPossibleRelatedValue(r, b.HasName);
82 if (name.equals(newTypeName)) {
85 if (!name.startsWith(random, 0))
87 name = name.substring(random.length());
90 index = Integer.parseInt(name);
91 } catch (NumberFormatException e) {
92 continue; // not a number
94 if (index > 0 && index < OUTER_INSTANCE_COUNT+1) {
95 newInstance[index-1] = r;
99 throw new Exception("Could not find created type");
100 for (int i=0; i<OUTER_INSTANCE_COUNT; ++i) {
101 //System.out.println("Read outer=" + i);
102 if (newInstance[i] == null)
103 throw new Exception("Could not find created instance");
104 if (!g.isInstanceOf(newInstance[i], b.Type))
105 throw new Exception("Created resource is not an instance of Type");
106 if (!g.isInstanceOf(newInstance[i], newType))
107 throw new Exception("Created resource is not an instance of created type");
108 // TODO: check inner instances