]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/scalability/LargeCommitTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / scalability / LargeCommitTest.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 import java.util.UUID;
16
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;
25
26 public class LargeCommitTest extends ExistingDatabaseTest {
27     
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
30
31     /**
32      * Creates new type and new instance of it. Then tries to find the instance and the type, and verifies that inheritance is correct. 
33      * @throws Exception
34      */
35     @Test
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) {
42                         @Override
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);
50                 
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);
64                     }
65                 }
66             }
67         });
68         
69         checkException();
70         
71         session.syncRequest(new TestReadRequest() {
72             @Override
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);
80                     if (null == name)
81                         continue;
82                     if (name.equals(newTypeName)) {
83                         newType = r;
84                     }
85                     if (!name.startsWith(random, 0))
86                         continue;
87                     name = name.substring(random.length());
88                     int index = 0;
89                     try {
90                         index = Integer.parseInt(name);
91                     } catch (NumberFormatException e) {
92                         continue; // not a number
93                     }
94                     if (index > 0 && index < OUTER_INSTANCE_COUNT+1) {
95                         newInstance[index-1] = r;
96                     }
97                 }
98                 if (newType == null) 
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
109                 }
110             }
111         });
112         
113         checkException();
114         
115     }
116
117 }