]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug3114Test.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 / SimanticsBug3114Test.java
1 package org.simantics.db.tests.regression.bugs;
2
3 import org.junit.Test;
4 import org.simantics.databoard.Bindings;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.Session;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.WriteOnlyGraph;
10 import org.simantics.db.common.request.UniqueRead;
11 import org.simantics.db.common.request.WriteOnlyRequest;
12 import org.simantics.db.common.request.WriteRequest;
13 import org.simantics.db.event.ChangeEvent;
14 import org.simantics.db.event.ChangeListener;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.service.GraphChangeListenerSupport;
17 import org.simantics.db.service.VirtualGraphSupport;
18 import org.simantics.db.testing.base.ExistingDatabaseTest;
19
20 public class SimanticsBug3114Test extends ExistingDatabaseTest {
21
22         private Resource newResource;
23         
24     @Test
25     public void test() throws Exception {
26         
27         Session session = getSession();
28         GraphChangeListenerSupport support = session.getService(GraphChangeListenerSupport.class);
29
30         // This listener writes 2 versions of a functional property into the same resource. The resulting model is broken if queries are not updated correctly.
31         support.addMetadataListener(new ChangeListener() {
32
33                         @Override
34                         public void graphChanged(ChangeEvent e) throws DatabaseException {
35                                 WriteGraph graph = (WriteGraph)e.getGraph();
36                         graph.sync(new WriteRequest(graph.getService(VirtualGraphSupport.class).getMemoryPersistent("mem")) {
37
38                                         @Override
39                                         public void perform(WriteGraph graph) throws DatabaseException {
40                                                 graph.claimLiteral(newResource, L0.HasName, "name", Bindings.STRING);
41                                                 graph.claimLiteral(newResource, L0.HasName, "name2", Bindings.STRING);
42                                         }
43
44                                 });             
45                                 
46                         }
47                         
48                 });
49
50         // Perform host request as write only.
51         session.sync(new WriteOnlyRequest() {
52                         
53                         @Override
54                         public void perform(WriteOnlyGraph graph) throws DatabaseException {
55                                 newResource = graph.newResource();
56                                 graph.claim(newResource, L0.InstanceOf, null, L0.Entity);
57                         }
58                         
59                 });
60         
61         // Ensure correct results
62         assertEquals("name2", getSession().sync(new UniqueRead<String>() {
63             @Override
64             public String perform(ReadGraph graph) throws DatabaseException {
65                 return graph.getRelatedValue(newResource, L0.HasName, Bindings.STRING);
66             }
67         }));
68         
69     }
70
71 }