--- /dev/null
+/*******************************************************************************
+ * 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 deletion of large literal when done twice in sequence.
+ * This was added because of Bug #4591.
+ */
+public class UndoTest20 extends ExistingDatabaseTest {
+ private void initValue(byte[] value) {
+ for (int i=0; i<value.length; ++i)
+ value[i] = (byte)i;
+ }
+
+ @Test
+ public void testUndo20() throws Exception {
+ Session session = getSession();
+ final byte[] value = new byte[100000];
+ initValue(value);
+ final DataContainer<Resource> subject1 = new DataContainer<Resource>();
+ try {
+ 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, value);
+ subject1.set(s1);
+ }
+ });
+ } catch (Throwable e) {
+ fail("Write transaction threw an unknown exception ", e);
+ }
+ testUndo(session, value, subject1);
+ testUndo(session, value, subject1);
+ }
+ public void testUndo(final Session session, final byte[] value, final DataContainer<Resource> subject1)
+ throws Exception {
+ final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
+ 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, value))
+ fail("Failed to verify value.");
+ }
+ });
+ } 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();
+ Resource s1 = subject1.get();
+ graph.denyValue(s1);
+ }
+ });
+ } 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.getPossibleValue(s1, Bindings.BYTE_ARRAY);
+ if (null != t1)
+ fail("Failed to verify delettion of value.");
+ }
+ });
+ } catch (Throwable e) {
+ fail("Read transaction threw an unexpected exception ", e);
+ }
+ try {
+ support.undo(session, 1);
+ } 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, value))
+ fail("Failed to verify value1.");
+ }
+ });
+ } catch (Throwable e) {
+ fail("Read transaction threw an unexpected exception ", e);
+ }
+ }
+}