]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/delayedWrite/DelayedWriteRequestCancelHandling.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / delayedWrite / DelayedWriteRequestCancelHandling.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/delayedWrite/DelayedWriteRequestCancelHandling.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/delayedWrite/DelayedWriteRequestCancelHandling.java
new file mode 100644 (file)
index 0000000..12cfb91
--- /dev/null
@@ -0,0 +1,62 @@
+package org.simantics.db.tests.api.delayedWrite;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.DelayedWriteRequest;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.CancelTransactionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.testing.annotation.Fails;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+
+/**
+ * Checks that throwing CancelTransactionException from within
+ * {@link DelayedWriteRequest#perform(WriteGraph)} will
+ * <ol>
+ * <li>Cancel the transaction correctly without getting stuck</li>
+ * <li>Write no data into the database</li>
+ * </ol>
+ * 
+ * @author Tuukka Lehtonen
+ */
+public class DelayedWriteRequestCancelHandling extends ExistingDatabaseTest {
+
+    @Test
+    @Fails
+    public void test() throws Exception {
+        final AtomicReference<Resource> ref = new AtomicReference<Resource>();
+        try {
+            DelayedWriteRequest r = new DelayedWriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    // Should throw an exception / error
+                    ref.set(graph.newResource());
+                    graph.claim(ref.get(), L0.InstanceOf, null, L0.String);
+                    throw new CancelTransactionException("intentional cancel");
+                }
+            };
+            getSession().sync(r);
+        } catch (CancelTransactionException e) {
+            // Should happen.
+        } catch (DatabaseException e) {
+            // Shouldn't happen.
+            fail("Got DatabaseException " + e + ", should've received CancelTransactionException");
+        }
+
+        // Just to check the database session is still alive.
+        Resource r = getSession().sync(new org.simantics.db.common.primitiverequest.Resource("http:/"));
+
+        fail("No data should've been written", getSession().sync(new UniqueRead<Boolean>() {
+            @Override
+            public Boolean perform(ReadGraph graph) throws DatabaseException {
+                return graph.hasStatement( ref.get() );
+            }
+        }));
+    }
+
+}