]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/WriteAfterClusterCollectTest2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / support / clusterControl / WriteAfterClusterCollectTest2.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.support.clusterControl;
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.WriteGraph;
21 import org.simantics.db.WriteOnlyGraph;
22 import org.simantics.db.common.request.WriteOnlyResultRequest;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.service.ClusterControl;
25 import org.simantics.db.testing.base.ExistingDatabaseTest;
26 import org.simantics.db.testing.common.WriteQuery;
27 import org.simantics.db.tests.common.Configuration;
28 import org.simantics.layer0.Layer0;
29
30 /**
31  * Creates large amount of instances in multiple transactions
32  * and the tries to verify that instances are written into the database.
33  * 
34  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35  *
36  */
37 public class WriteAfterClusterCollectTest2 extends ExistingDatabaseTest {
38         private int LOOP = Configuration.get().collectedLoopCount;
39         @Test
40         public void test() throws Exception{
41
42             final String random = getRandomString();
43                 
44                 final Resource rootLib = getSession().syncRequest(new WriteOnlyResultRequest<Resource>() {
45                     @Override
46                     public Resource perform(WriteOnlyGraph g) throws DatabaseException {
47                         Resource rootLib = g.newResource();
48                 for (int i = 0; i < 10; i++) {
49                         for (int j = 0; j < LOOP; j++) {
50                             Layer0 b = Layer0.getInstance(getSession());
51                             String newInstanceName = random + i;
52                             Resource instance = g.newResource();
53                             g.claim(instance, b.InstanceOf, null, b.Type);
54                             g.addLiteral(instance, b.HasName, b.NameOf, b.String, newInstanceName, Bindings.STRING);
55                             g.claim(rootLib, b.ConsistsOf, b.PartOf, instance);
56                             g.flushCluster();
57                         }
58                 }
59                 return rootLib;
60                     }
61                 });
62                 
63                 checkException();
64                 
65                 getSession().syncRequest(new TestReadRequest() {
66                         @Override
67                         public void run(ReadGraph g) throws Throwable {
68                                 Layer0 b = Layer0.getInstance(g);
69                                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
70                                 for (Resource r : resources) {
71                                         String name = g.getPossibleRelatedValue(r, b.HasName);
72                                         if (DEBUG)
73                                             System.out.println("Name is " + name);
74                                 }
75                         }
76                 });
77                 
78                 ClusterControl support = getSession().getService(ClusterControl.class);
79                 support.collectClusters(Integer.MAX_VALUE);
80                 
81                 checkException();
82                 
83         getSession().syncRequest(new WriteQuery(this) {
84             @Override
85             public void run(WriteGraph g) throws Throwable {
86                 
87                 Layer0 b = Layer0.getInstance(g);
88                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
89                 for (Resource r : resources) {
90                     String name = g.getPossibleRelatedValue(r, b.HasName);
91                     if (DEBUG)
92                         System.out.println("Name is " + name);
93                     String newInstanceName = random;
94                     Resource instance = g.newResource();
95                     g.claimLiteral(instance, b.HasName, newInstanceName);
96                     g.claim(r, b.ConsistsOf, b.PartOf, instance);
97                 }
98                 
99             }
100             
101         });
102         
103         checkException();
104
105         getSession().syncRequest(new TestReadRequest() {
106             @Override
107             public void run(ReadGraph g) throws Throwable {
108                 Layer0 b = Layer0.getInstance(g);
109                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
110                 for (Resource r : resources) {
111                     String name = g.getPossibleRelatedValue(r, b.HasName);
112                     if (DEBUG)
113                         System.out.println("Name is " + name);
114                 }
115             }
116         });
117         
118         checkException();
119
120         support.collectClusters(Integer.MAX_VALUE);
121                 
122         }
123
124 }