]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest3.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 / virtualGraphSupport / VirtualGraphTest3.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.virtualGraphSupport;
13
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.UUID;
17
18 import org.junit.Test;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.VirtualGraph;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.service.VirtualGraphSupport;
27 import org.simantics.db.testing.base.TestCleanDb;
28 import org.simantics.layer0.Layer0;
29
30 public class VirtualGraphTest3 extends TestCleanDb {
31         
32         private Error exception; 
33         
34         @Test
35         public void test() throws DatabaseException {
36                 
37                 Session session = getSession();
38         final VirtualGraphSupport support = session.getService(VirtualGraphSupport.class);
39         final VirtualGraph virtual = support.getMemoryPersistent(UUID.randomUUID().toString());
40                 
41                 session.syncRequest(new WriteRequest() {
42
43                         @Override
44                         public void perform(WriteGraph g) throws DatabaseException {
45
46                 Layer0 b = Layer0.getInstance(g);
47
48                 final HashSet<String> original = new HashSet<String>();
49                             final HashSet<String> strings = new HashSet<String>();
50
51                 for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) {
52                     String name = g.getPossibleRelatedValue(r, b.HasName);
53                     if (name != null) { 
54                         original.add(name);
55                         strings.add(name);
56                     } else if (DEBUG)
57                         System.out.println("Resource " + r + " has no name.");
58                 }
59                 
60                             g.syncRequest(new WriteRequest(virtual) {
61
62                                         @Override
63                                         public void perform(WriteGraph g) throws DatabaseException {
64                                                 
65                                                 Layer0 b = Layer0.getInstance(g);
66                                                 for(int i=0;i<10;i++) {
67                                                     Resource r = g.newResource();
68                                                     String name = "Virtual" + i;
69                                                     g.claimLiteral(r, b.HasName, name, Bindings.STRING);
70                                                     strings.add(name);
71                                                     g.claim(g.getRootLibrary(), b.ConsistsOf, r);
72                                                     if (DEBUG) 
73                                                         System.out.println("added " + name + " resource " + r);
74                                                 }
75                                                 
76                                         }
77                                         
78                                 });
79                                 
80                 for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) {
81                     String name = g.getPossibleRelatedValue(r, b.HasName);
82                     if (DEBUG)
83                         System.out.println("found " + name);
84                     boolean success = strings.remove(name);
85                     if(!success) {
86                         exception = new AssertionError("encountered unknown string " + name);
87                         throw exception;
88                     }
89                 }
90
91                 if(!strings.isEmpty()) {
92                     exception = new AssertionError("some strings were left over " + Arrays.toString(strings.toArray()));
93                     throw exception;
94                 }
95
96                 support.discard(virtual);
97                 
98                 for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) {
99                     String name = g.getPossibleRelatedValue(r, b.HasName);
100                     if (DEBUG)
101                         System.out.println("found " + name);
102                     boolean success = original.remove(name);
103                     if(!success) {
104                         exception = new AssertionError("encountered unknown string " + name + " resource " + r);
105                         throw exception;
106                     }
107                 }
108
109                 if(!original.isEmpty()) {
110                     exception = new AssertionError("some strings were left over " + Arrays.toString(strings.toArray()));
111                     throw exception;
112                 }
113
114                         }
115                         
116                 });
117                 
118                 if(exception != null) {
119                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
120                 }
121                 
122         }
123
124 }