]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/clusterControl/WriteAfterClusterCollectTest.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 / WriteAfterClusterCollectTest.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.layer0.Layer0;
28
29 /**
30  * Creates large amount of instances in multiple transactions
31  * and the tries to verify that instances are written into the database.
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  *
35  */
36 public class WriteAfterClusterCollectTest extends ExistingDatabaseTest {
37     @Test
38     public void test() throws Exception{
39
40 //        final Resource rootLib = getRootLibrary();
41         final String random = getRandomString();
42         
43         final Resource rootLib = getSession().syncRequest(new WriteOnlyResultRequest<Resource>() {
44             @Override
45             public Resource perform(WriteOnlyGraph g) throws DatabaseException {
46                 Resource rootLib = g.newResource();
47                 for (int i = 0; i < 1000; i++) {
48                     Layer0 b = Layer0.getInstance(getSession());
49                     String newInstanceName = random + i;
50                     Resource instance = g.newResource();
51                     g.claim(instance, b.InstanceOf, null, b.Type);
52                     g.addLiteral(instance, b.HasName, b.NameOf, b.String, newInstanceName, Bindings.STRING);
53                     g.claim(rootLib, b.ConsistsOf, b.PartOf, instance);
54                 }
55                 return rootLib;
56             }
57         });
58         
59         checkException();
60         
61         getSession().syncRequest(new TestReadRequest() {
62             @Override
63             public void run(ReadGraph g) throws Throwable {
64                 Layer0 b = Layer0.getInstance(g);
65                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
66                 for (Resource r : resources) {
67                     String name = g.getPossibleRelatedValue(r, b.HasName);
68                     if (DEBUG)
69                         System.out.println("Resource=" + r + " name=" + name);
70                 }
71             }
72         });
73         
74         ClusterControl support = getSession().getService(ClusterControl.class);
75         support.collectClusters(Integer.MAX_VALUE);
76         
77         checkException();
78         
79         getSession().syncRequest(new WriteQuery(this) {
80             @Override
81             public void run(WriteGraph g) throws Throwable {
82                 
83                 Layer0 b = Layer0.getInstance(g);
84                 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
85                 for (Resource r : resources) {
86                     String name = g.getPossibleRelatedValue(r, b.HasName);
87                     if (DEBUG)
88                         System.out.println("Resource=" + r + " name=" + name);
89                     String newInstanceName = random;
90                     Resource instance = g.newResource();
91                     g.claimLiteral(instance, b.HasName, newInstanceName);
92                     g.claim(r, b.ConsistsOf, b.PartOf, instance);
93                 }
94             }
95         });
96     }
97
98 }