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: * *
    *
  1. Perform a write request (NOP) which inserts a comment into the metadata * but does no changes to the database
  2. *
  3. Perform another request (W) that writes both comment metadata and * database changes
  4. *
  5. The database revision history will contain a single new revision with the * both comments from the NOP and W
  6. *
* * 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() { @Override public Boolean perform(ReadGraph graph) throws DatabaseException { ManagementSupport ms = graph.getService(ManagementSupport.class); long cid = ms.getHeadRevisionId(); Collection 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; } })); } }