]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug639Test.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/SimanticsBug639Test.java
new file mode 100644 (file)
index 0000000..f6a3dd1
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.regression.bugs;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.NoSingleResultException;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.DataContainer;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class SimanticsBug639Test extends ExistingDatabaseTest {
+
+    public Resource createAssertion(WriteGraph graph, Resource subject, Resource relation, String object) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        Resource assertion = graph.newResource();
+        graph.claim(assertion, L0.InstanceOf, null, L0.Assertion);
+        graph.claim(subject, L0.Asserts, assertion);
+        graph.claim(assertion, L0.HasPredicate, relation);
+        Resource value = graph.newResource();
+        graph.claim(value, L0.InstanceOf, null, L0.String);
+        graph.claimValue(value, object);
+        graph.claim(assertion, L0.HasObject, value);
+        return assertion;
+    }
+
+    @Test
+    public void testTicket646() throws Exception {
+
+        Session session = getSession();
+        final DataContainer<Resource> subject = new DataContainer<Resource>();
+        try {
+            session.syncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    // Use any non-functional relation
+                    Layer0 b = Layer0.getInstance(graph);
+                    Resource s = graph.newResource();
+                    graph.claim(s, b.InstanceOf, b.Entity);
+
+                    createAssertion(graph, s, L0.HasName, "Foo1");
+                    createAssertion(graph, s, L0.HasName, "Foo2");
+                    createAssertion(graph, s, L0.HasComment, "Comment1");
+                    createAssertion(graph, s, L0.HasComment, "Comment2");
+
+                    subject.set(s);
+                }
+            });
+        } catch (Throwable e) {
+            fail("Write transaction threw an unknown exception " + e);
+        }
+
+        try {
+            session.syncRequest(new ReadRequest() {
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    Layer0 L0 = Layer0.getInstance(graph);
+                    Resource s = subject.get();
+                    // Just make sure that these don't crash.
+                    assertEquals(graph.getObjects(s, L0.HasComment).size(), 0);
+                    assertEquals(graph.getObjects(s, L0.HasName).size(), 0);
+                }
+            });
+        } catch (Throwable e) {
+            fail("Read transaction threw an unexpected exception " + e);
+        }
+
+        try {
+            session.syncRequest(new ReadRequest() {
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    Layer0 L0 = Layer0.getInstance(graph);
+                    Resource s = subject.get();
+                    graph.getRelatedValue(s, L0.HasName, Bindings.STRING);
+                }
+            });
+        } catch (NoSingleResultException e) {
+            // Expected
+        } catch (Throwable e) {
+            fail("Read transaction threw an unexpected exception " + e);
+        }
+
+    }
+}
\ No newline at end of file