]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.tests.regression.bugs;
2
3 import java.util.Collection;
4
5 import org.junit.Test;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.CommentMetadata;
10 import org.simantics.db.common.UndoMetadata;
11 import org.simantics.db.common.request.UniqueRead;
12 import org.simantics.db.common.request.WriteRequest;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.service.ManagementSupport;
15 import org.simantics.db.testing.base.ExistingDatabaseTest;
16
17 /**
18  * I found a bug in the database client write requests that caused the following
19  * to happen:
20  *
21  * <ol>
22  * <li>Perform a write request (NOP) which inserts a comment into the metadata
23  * but does no changes to the database</li>
24  * <li>Perform another request (W) that writes both comment metadata and
25  * database changes</li>
26  * <li>The database revision history will contain a single new revision with the
27  * both comments from the NOP and W</li>
28  * </ol>
29  *
30  * The expected result is that the database only contains the comment metadata
31  * written in request W.
32  *
33  * @author Tuukka Lehtonen
34  */
35 public class CommentMetadataWriteTest extends ExistingDatabaseTest {
36     // Behavior of commit has been changed. Commits with only CommentMetadata are dropped.
37     private void addMetadata(WriteGraph wg, String s) throws DatabaseException {
38         CommentMetadata cm = wg.getMetadata(CommentMetadata.class);
39         wg.addMetadata(cm.add(s));
40         wg.addMetadata(new UndoMetadata(null, false, 0, 0));
41     }
42     @Test
43     public void test() throws Exception {
44         // Perform empty request, write comment
45         WriteRequest nop = new WriteRequest() {
46             @Override
47             public void perform(WriteGraph graph) throws DatabaseException {
48                 addMetadata(graph, "NOP");
49             }
50         };
51         getSession().sync(nop);
52
53         // Perform non-empty request, write comment
54         WriteRequest w = new WriteRequest() {
55             @Override
56             public void perform(WriteGraph graph) throws DatabaseException {
57                 Resource r = graph.newResource();
58                 graph.claim(r, L0.InstanceOf, L0.String);
59                 graph.claimValue(r, "FOO");
60                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
61                 graph.addMetadata(cm.add("W"));
62             }
63         };
64         getSession().sync(w);
65         assertTrue("Metadata from empty requests should not accumulate to following requests", getSession().sync(new UniqueRead<Boolean>() {
66             @Override
67             public Boolean perform(ReadGraph graph) throws DatabaseException {
68                 ManagementSupport ms = graph.getService(ManagementSupport.class);
69                 long cid = ms.getHeadRevisionId();
70                 Collection<CommentMetadata> comments = ms.getMetadata(cid, cid, CommentMetadata.class);
71                 if (comments.size() != 1)
72                     return false;
73                 CommentMetadata cm = comments.iterator().next();
74                 String s = cm.toString();
75                 // The line feed is added by the add operation.
76                 if (!s.matches("W\n"))
77                     return false;
78                 else
79                     return true;
80             }
81         }));
82     }
83
84 }