]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug639Test.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 / SimanticsBug639Test.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 org.junit.Test;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.exception.NoSingleResultException;
24 import org.simantics.db.testing.base.ExistingDatabaseTest;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.utils.DataContainer;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class SimanticsBug639Test extends ExistingDatabaseTest {
32
33     public Resource createAssertion(WriteGraph graph, Resource subject, Resource relation, String object) throws DatabaseException {
34         Layer0 L0 = Layer0.getInstance(graph);
35         Resource assertion = graph.newResource();
36         graph.claim(assertion, L0.InstanceOf, null, L0.Assertion);
37         graph.claim(subject, L0.Asserts, assertion);
38         graph.claim(assertion, L0.HasPredicate, relation);
39         Resource value = graph.newResource();
40         graph.claim(value, L0.InstanceOf, null, L0.String);
41         graph.claimValue(value, object);
42         graph.claim(assertion, L0.HasObject, value);
43         return assertion;
44     }
45
46     @Test
47     public void testTicket646() throws Exception {
48
49         Session session = getSession();
50         final DataContainer<Resource> subject = new DataContainer<Resource>();
51         try {
52             session.syncRequest(new WriteRequest() {
53                 @Override
54                 public void perform(WriteGraph graph) throws DatabaseException {
55                     // Use any non-functional relation
56                     Layer0 b = Layer0.getInstance(graph);
57                     Resource s = graph.newResource();
58                     graph.claim(s, b.InstanceOf, b.Entity);
59
60                     createAssertion(graph, s, L0.HasName, "Foo1");
61                     createAssertion(graph, s, L0.HasName, "Foo2");
62                     createAssertion(graph, s, L0.HasComment, "Comment1");
63                     createAssertion(graph, s, L0.HasComment, "Comment2");
64
65                     subject.set(s);
66                 }
67             });
68         } catch (Throwable e) {
69             fail("Write transaction threw an unknown exception " + e);
70         }
71
72         try {
73             session.syncRequest(new ReadRequest() {
74                 @Override
75                 public void run(ReadGraph graph) throws DatabaseException {
76                     Layer0 L0 = Layer0.getInstance(graph);
77                     Resource s = subject.get();
78                     // Just make sure that these don't crash.
79                     assertEquals(graph.getObjects(s, L0.HasComment).size(), 0);
80                     assertEquals(graph.getObjects(s, L0.HasName).size(), 0);
81                 }
82             });
83         } catch (Throwable e) {
84             fail("Read transaction threw an unexpected exception " + e);
85         }
86
87         try {
88             session.syncRequest(new ReadRequest() {
89                 @Override
90                 public void run(ReadGraph graph) throws DatabaseException {
91                     Layer0 L0 = Layer0.getInstance(graph);
92                     Resource s = subject.get();
93                     graph.getRelatedValue(s, L0.HasName, Bindings.STRING);
94                 }
95             });
96         } catch (NoSingleResultException e) {
97             // Expected
98         } catch (Throwable e) {
99             fail("Read transaction threw an unexpected exception " + e);
100         }
101
102     }
103 }