]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3420Test2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / Issue3420Test2.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.regression.bugs;
13
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.TreeSet;
18 import java.util.Vector;
19
20 import org.junit.Test;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.WriteOnlyGraph;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.common.request.WriteOnlyRequest;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.exception.ClusterSetExistException;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.impl.ClusterTraitsBase;
32 import org.simantics.db.service.ClusteringSupport;
33 import org.simantics.db.testing.base.ExistingDatabaseTest;
34 import org.simantics.layer0.Layer0;
35
36 /**
37  * Tests that cluster set can be used to explicitly control clustering.
38  * Must be called more than once from runner to test that cluster set is persistent.
39  */
40 public class Issue3420Test2 extends ExistingDatabaseTest {
41 //    final boolean DEBUG = true;
42     final boolean VERBOSE = false;
43     final int CLUSTER_FILL_SIZE = ClusterTraitsBase.getMaxNumberOfResources();
44     final Vector<Resource> clusterSets = new Vector<Resource>();
45     final Vector<Resource> resources = new Vector<Resource>();
46     @Test
47     public void test() throws DatabaseException {
48         Session session = getSession();
49         session.syncRequest(new InitClusterSet());
50         session.syncRequest(new Init());
51         session.syncRequest(new Modi());
52         session.syncRequest(new Check());
53         session.syncRequest(new ModiWO());
54         session.syncRequest(new Check());
55     }
56     class InitClusterSet extends WriteRequest {
57         @Override
58         public void perform(WriteGraph g) throws DatabaseException {
59             Layer0 l0 = Layer0.getInstance(g);
60             String base = "http://Projects/Development%20Project";
61             String tail = "Issue3420Test2";
62             Resource pl = g.getResource(base);
63             if (DEBUG)
64                 System.out.println("projects=" + pl);
65             Resource il = g.getPossibleResource(base + "/" + tail);
66             try {
67                 g.newClusterSet(pl);
68                 // New cluster set.
69                 assertTrue("Old cluster set data lost.", null == il);
70                 il = g.newResource();
71                 g.claim(il, l0.InstanceOf, null, l0.Library);
72                 g.claim(il, l0.PartOf, pl);
73                 g.claimLiteral(il, l0.HasName, tail);
74             } catch (ClusterSetExistException e) {
75                 // Old cluster set.
76                 assertTrue("Missing library resource.", null != il);
77             }
78             clusterSets.setSize(2);
79             clusterSets.set(0, pl);
80             clusterSets.set(1, il);
81         }
82     }
83     class Init extends ReadRequest {
84         @Override
85         public void run(ReadGraph g) throws DatabaseException {
86             resources.clear();
87             Layer0 l0 = Layer0.getInstance(g);
88             for (Resource r : g.getObjects(clusterSets.get(1), l0.ConsistsOf)) {
89                 if (DEBUG)
90                     if (VERBOSE)
91                         System.out.println("Resource " + r);
92                 resources.add(r);
93             }
94             if (DEBUG)
95                 System.out.println("Resource count=" + resources.size());
96         }
97     }
98     class Modi extends WriteRequest {
99         @Override
100         public void perform(WriteGraph g) throws DatabaseException {
101             for (int i=0; i<CLUSTER_FILL_SIZE * 2 + 33; ++i) {
102                 Resource r = g.newResource(clusterSets.get(0));
103                 g.claim(r, L0.InstanceOf, null, L0.Entity);
104                 g.claim(r, L0.PartOf, clusterSets.get(1));
105                 resources.add(r);
106             }
107         }
108     }
109     class ModiWO extends WriteOnlyRequest {
110         @Override
111         public void perform(WriteOnlyGraph g) throws DatabaseException {
112             for (int i=0; i<CLUSTER_FILL_SIZE * 2 + 33; ++i) {
113                 Resource r = g.newResource(clusterSets.get(0));
114                 g.claim(r, L0.InstanceOf, null, L0.Entity);
115                 g.claim(r, L0.PartOf, L0.ConsistsOf, clusterSets.get(1));
116                 resources.add(r);
117             }
118         }
119     }
120     class Check extends ReadRequest {
121         @Override
122         public void run(ReadGraph g) throws DatabaseException {
123             ClusteringSupport cs = g.getService(ClusteringSupport.class);
124             Map<Long, Integer> clusters = new HashMap<Long, Integer>();
125             for (int i=0; i<resources.size(); ++i){
126                 long cluster = cs.getCluster(resources.get(i));
127                 Integer count = clusters.get(cluster);
128                 if (null == count)
129                     count = 0;
130                 clusters.put(cluster, ++count);
131             }
132             Set<Long> sortedKeys = new TreeSet<Long>();
133             for (Map.Entry<Long, Integer> i : clusters.entrySet())
134                 sortedKeys.add(i.getKey());
135             long sum = 0;
136             for (long key : sortedKeys) {
137                 int count = clusters.get(key);
138                 if (DEBUG)
139                     System.out.println("cluster=" + key + " count=" + count);
140                 sum += count;
141             }
142             if (resources.size() != sum) {
143                 String msg = "Resource count mismatch. expected=" + resources.size() + " realized=" + sum;
144                 if (DEBUG)
145                     System.out.println(msg);
146                 fail(msg);
147             }
148             int n = resources.size() / CLUSTER_FILL_SIZE + 1;
149             if (n < clusters.size()) {
150                 String msg = "Cluster count mismatch. expected=" + n + " realized=" + clusters.size();
151                 if (DEBUG)
152                     System.out.println(msg);
153                 fail(msg);
154             }
155         }
156     }
157 }