]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/CommentMetadataWriteTest.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 / CommentMetadataWriteTest.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/CommentMetadataWriteTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/CommentMetadataWriteTest.java
new file mode 100644 (file)
index 0000000..a072fce
--- /dev/null
@@ -0,0 +1,84 @@
+package org.simantics.db.tests.regression.bugs;
+
+import java.util.Collection;
+
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.UndoMetadata;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.ManagementSupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+
+/**
+ * I found a bug in the database client write requests that caused the following
+ * to happen:
+ *
+ * <ol>
+ * <li>Perform a write request (NOP) which inserts a comment into the metadata
+ * but does no changes to the database</li>
+ * <li>Perform another request (W) that writes both comment metadata and
+ * database changes</li>
+ * <li>The database revision history will contain a single new revision with the
+ * both comments from the NOP and W</li>
+ * </ol>
+ *
+ * The expected result is that the database only contains the comment metadata
+ * written in request W.
+ *
+ * @author Tuukka Lehtonen
+ */
+public class CommentMetadataWriteTest extends ExistingDatabaseTest {
+    // Behavior of commit has been changed. Commits with only CommentMetadata are dropped.
+    private void addMetadata(WriteGraph wg, String s) throws DatabaseException {
+        CommentMetadata cm = wg.getMetadata(CommentMetadata.class);
+        wg.addMetadata(cm.add(s));
+        wg.addMetadata(new UndoMetadata(null, false, 0, 0));
+    }
+    @Test
+    public void test() throws Exception {
+        // Perform empty request, write comment
+        WriteRequest nop = new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                addMetadata(graph, "NOP");
+            }
+        };
+        getSession().sync(nop);
+
+        // Perform non-empty request, write comment
+        WriteRequest w = new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                Resource r = graph.newResource();
+                graph.claim(r, L0.InstanceOf, L0.String);
+                graph.claimValue(r, "FOO");
+                CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+                graph.addMetadata(cm.add("W"));
+            }
+        };
+        getSession().sync(w);
+        assertTrue("Metadata from empty requests should not accumulate to following requests", getSession().sync(new UniqueRead<Boolean>() {
+            @Override
+            public Boolean perform(ReadGraph graph) throws DatabaseException {
+                ManagementSupport ms = graph.getService(ManagementSupport.class);
+                long cid = ms.getHeadRevisionId();
+                Collection<CommentMetadata> comments = ms.getMetadata(cid, cid, CommentMetadata.class);
+                if (comments.size() != 1)
+                    return false;
+                CommentMetadata cm = comments.iterator().next();
+                String s = cm.toString();
+                // The line feed is added by the add operation.
+                if (!s.matches("W\n"))
+                    return false;
+                else
+                    return true;
+            }
+        }));
+    }
+
+}