]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest18.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / support / undoRedoSupport / UndoTest18.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest18.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest18.java
new file mode 100644 (file)
index 0000000..63a2f42
--- /dev/null
@@ -0,0 +1,187 @@
+/*******************************************************************************
+ * 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.api.support.undoRedoSupport;
+
+import java.util.Arrays;
+
+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.service.UndoRedoSupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.layer0.Layer0;
+import org.simantics.utils.DataContainer;
+
+/**
+ * Tests undo of two changes sets with values modified in multiple cluster change sets.
+ * The binary values will cause two cluster change sets per modification.
+ * (By the way the above-mentioned behavior will cause serious fragmentation in server
+ * because the size of cluster change sets are ridiculously small and server does not
+ * process cluster change sets but stores them as is. The payload is just about the same
+ * as the bookkeeping structures. TODO: fix the aforementioned fragmentation)
+ */
+public class UndoTest18 extends ExistingDatabaseTest {
+    private void initValue(byte[] value) {
+        for (int i=0; i<value.length; ++i)
+            value[i] = (byte)i;
+    }
+
+    @Test
+    public void testUndo1() throws Exception {
+
+        Session session = getSession();
+        final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
+        final DataContainer<Resource> subject1 = new DataContainer<Resource>();
+        final DataContainer<Resource> subject2 = new DataContainer<Resource>();
+        final byte[] value10 = new byte[100000];
+        final byte[] value20 = new byte[100001];
+        final byte[] value11 = new byte[100002];
+        final byte[] value21 = new byte[100003];
+
+        try {
+            initValue(value10);
+            initValue(value11);
+            initValue(value20);
+            initValue(value21);
+            session.syncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    graph.markUndoPoint();
+                    Layer0 b = Layer0.getInstance(graph);
+                    Resource s1 = graph.newResource();
+                    graph.claim(s1, b.InstanceOf, b.Entity);
+                    graph.claimValue(s1, value10);
+                    subject1.set(s1);
+                }
+            });
+        } catch (Throwable e) {
+            fail("Write transaction threw an unknown exception ", e);
+        }
+
+        try {
+            session.syncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    graph.markUndoPoint();
+                    Layer0 b = Layer0.getInstance(graph);
+                    Resource s2 = graph.newResource();
+                    graph.claim(s2, b.InstanceOf, b.Entity);
+                    graph.claimValue(s2, value20);
+                    subject2.set(s2);
+                }
+            });
+        } catch (Throwable e) {
+            fail("Write transaction threw an unknown exception ", e);
+        }
+
+        try {
+            session.syncRequest(new ReadRequest() {
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    Resource s1 = subject1.get();
+                    byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
+                    if (!Arrays.equals(t1, value10))
+                        fail("Failed to verify value1.");
+                    Resource s2 = subject2.get();
+                    byte[] t2 = graph.getValue(s2, Bindings.BYTE_ARRAY);
+                    if (!Arrays.equals(t2, value20))
+                        fail("Failed to verify value2.");
+                }
+            });
+        } catch (Throwable e) {
+            fail("Read transaction threw an unexpected exception ", e);
+        }
+
+        try {
+            session.syncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    graph.markUndoPoint();
+                    graph.newResource();
+                }
+            });
+        } catch (Throwable e) {
+            fail("Write transaction threw an unknown exception ", e);
+        }
+
+        try {
+            session.syncRequest(new WriteRequest() {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    graph.markUndoPoint();
+                    Layer0 b = Layer0.getInstance(graph);
+                    Resource s1 = subject1.get();
+                    graph.claim(s1, b.InstanceOf, b.Entity);
+                    graph.claimValue(s1, value11);
+                    subject1.set(s1);
+                    Resource s2 = subject2.get();
+                    graph.claim(s2, b.InstanceOf, b.Entity);
+                    graph.claimValue(s2, value21);
+                    subject2.set(s2);
+                }
+            });
+        } catch (Throwable e) {
+            fail("Write transaction threw an unknown exception ", e);
+        }
+
+        try {
+            session.syncRequest(new ReadRequest() {
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    Resource s1 = subject1.get();
+                    byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
+                    if (!Arrays.equals(t1, value11))
+                        fail("Failed to verify value11.");
+                    Resource s2 = subject2.get();
+                    byte[] t2 = graph.getValue(s2, Bindings.BYTE_ARRAY);
+                    if (!Arrays.equals(t2, value21))
+                        fail("Failed to verify value21.");
+                }
+            });
+        } catch (Throwable e) {
+            fail("Read transaction threw an unexpected exception ", e);
+        }
+
+        try {
+            support.undo(session, 2);
+        } catch (Throwable e) {
+            fail("Undo operation threw an unexpected exception.", e);
+        }
+
+        try {
+            session.syncRequest(new ReadRequest() {
+                @Override
+                public void run(ReadGraph graph) throws DatabaseException {
+                    Resource s1 = subject1.get();
+                    byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
+                    if (!Arrays.equals(t1, value10))
+                        fail("Failed to verify value1.");
+                    Resource s2 = subject2.get();
+                    byte[] t2 = graph.getValue(s2, Bindings.BYTE_ARRAY);
+                    if (!Arrays.equals(t2, value20))
+                        fail("Failed to verify value2.");
+                }
+            });
+        } catch (Throwable e) {
+            fail("Read transaction threw an unexpected exception ", e);
+        }
+
+    }
+
+}
+