]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest2.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 / VirtualGraphTest2.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 VirtualGraphTest2 extends TestCleanDb {
31         
32         private Error exception; 
33         
34         @Test
35         public void test() throws DatabaseException {
36                 
37                 Session session = getSession();
38         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> strings = new HashSet<String>();
49
50                 for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) {
51                     String name = g.getPossibleRelatedValue(r, b.HasName); 
52                     strings.add(name);
53                 }
54                 
55                             g.syncRequest(new WriteRequest(virtual) {
56
57                                         @Override
58                                         public void perform(WriteGraph g) throws DatabaseException {
59                                                 
60                                                 Layer0 b = Layer0.getInstance(g);
61                                                 for(int i=0;i<10;i++) {
62                                                     Resource r = g.newResource();
63                                                     String name = "Virtual" + i;
64                                                     g.claimLiteral(r, b.HasName, name, Bindings.STRING);
65                                                     strings.add(name);
66                                                     g.claim(g.getRootLibrary(), b.ConsistsOf, r);
67                                                 }
68                                                 
69                                         }
70                                         
71                                 });
72                                 
73                 for(Resource r : g.getObjects(g.getRootLibrary() , b.ConsistsOf)) {
74                     String name = g.getPossibleRelatedValue(r, b.HasName);
75                     if (DEBUG)
76                         System.out.println("found " + name);
77                     boolean success = strings.remove(name);
78                     if(!success) {
79                         exception = new AssertionError("encountered unknown string " + name);
80                         throw exception;
81                     }
82                 }
83
84                                 if(!strings.isEmpty()) {
85                                         exception = new AssertionError("some strings were left over " + Arrays.toString(strings.toArray()));
86                                         throw exception;
87                                 }
88
89                         }
90                         
91                 });
92                 
93                 if(exception != null) {
94                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
95                 }
96                 
97         }
98
99 }