]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/virtualGraphSupport/VirtualGraphTest.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 / VirtualGraphTest.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.UUID;
15
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.VirtualGraph;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.common.request.WriteResultRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.service.VirtualGraphSupport;
26 import org.simantics.db.testing.base.TestCleanDb;
27 import org.simantics.layer0.Layer0;
28
29 public class VirtualGraphTest extends TestCleanDb {
30         
31         private Error exception; 
32         
33         @Test
34         public void test() throws DatabaseException {
35                 
36                 Session session = getSession();
37                 
38                 session.syncRequest(new WriteRequest() {
39
40                         @Override
41                         public void perform(WriteGraph graph) throws DatabaseException {
42                                 
43                                 Layer0 L0 = Layer0.getInstance(graph);
44
45                                 final Resource newResource = graph.syncRequest(new WriteResultRequest<Resource>() {
46
47                                         @Override
48                                         public Resource perform(WriteGraph graph) throws DatabaseException {
49                                                 
50                                                 Layer0 L0 = Layer0.getInstance(graph);
51                                                 Resource result = graph.newResource();
52                                                 graph.addLiteral(result, L0.HasName, L0.NameOf, L0.String, "RootLibrary", Bindings.STRING);
53                                                 return result;
54                                                 
55                                         }
56                                         
57                                 });
58                                 
59                                 String rootName = graph.getRelatedValue(newResource, L0.HasName);
60                                 if(!rootName.equals("RootLibrary")) {
61                                         exception = new AssertionError("rootName != 'RootLibrary'");
62                                         throw exception;
63                                 }
64
65                                 VirtualGraphSupport support = graph.getSession().getService(VirtualGraphSupport.class);
66                                 final VirtualGraph virtual = support.getMemoryPersistent(UUID.randomUUID().toString());
67
68                                 graph.syncRequest(new WriteRequest(virtual) {
69
70                                         @Override
71                                         public void perform(WriteGraph graph) throws DatabaseException {
72                                                 
73                                                 Layer0 L0 = Layer0.getInstance(graph);
74                                                 graph.addLiteral(newResource, L0.HasName, L0.NameOf, L0.String, "RootLibrary2", Bindings.STRING);
75                                                 
76                                         }
77                                         
78                                 });
79                                 
80                                 rootName = graph.getRelatedValue(newResource, L0.HasName);
81                                 if(!rootName.equals("RootLibrary2")) {
82                                         exception = new AssertionError("rootName != 'RootLibrary2'");
83                                         throw exception;
84                                 }
85                                 
86                                 support.discard(virtual);
87                                 
88                                 rootName = graph.getRelatedValue(newResource, L0.HasName);
89                                 if(!rootName.equals("RootLibrary")) {
90                                         exception = new AssertionError("rootName != 'RootLibrary'");
91                                         throw exception;
92                                 }
93
94                         }
95                         
96                 });
97                 
98                 if(exception != null) {
99                         fail("Write transaction threw and exception (" + exception.getMessage() + ") which was not passed through ");
100                 }
101                 
102         }
103
104 }