X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fsupport%2FundoRedoSupport%2FUndoTest01.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fsupport%2FundoRedoSupport%2FUndoTest01.java;h=730bb942d65d6271887320425bb41a5519635796;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest01.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest01.java new file mode 100644 index 000000000..730bb942d --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest01.java @@ -0,0 +1,202 @@ +/******************************************************************************* + * 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.Map; + +import org.junit.Test; +import org.simantics.databoard.Bindings; +import org.simantics.db.ChangeSetIdentifier; +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 add statement. + */ +public class UndoTest01 extends ExistingDatabaseTest { + + @Test + public void testUndo1() throws Exception { + + Session session = getSession(); + final UndoRedoSupport support = session.getService(UndoRedoSupport.class); + final DataContainer subject = new DataContainer(); + + try { + session.syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + // Use any non-functional relation + Layer0 b = Layer0.getInstance(graph); + Resource s = graph.newResource(); + graph.claim(s, b.InstanceOf, b.Entity); + subject.set(s); + } + }); + } catch (Throwable e) { + fail("Write transaction threw an unknown exception ", e); + } + + try { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + assert(graph.hasStatement(subject.get())); + } + }); + } 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 { + assert(!graph.hasStatement(subject.get())); + } + }); + } catch (Throwable e) { + e.printStackTrace(); + fail("Read transaction threw an unexpected exception ", e); + } + + } + +} + +class Context { + private final Session session; + private final UndoRedoSupport support; + Resource r; + Integer value = 0; + Integer oldValue = 0; + Context(Session session, UndoRedoSupport support) { + this.session = session; + this.support = support; + } + void create() throws DatabaseException { + session.syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + Layer0 b = Layer0.getInstance(graph); + r = graph.newResource(); + graph.claim(r, b.InstanceOf, b.Entity); + } + }); + } + void createAndSet() throws DatabaseException { + session.syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + Layer0 b = Layer0.getInstance(graph); + r = graph.newResource(); + graph.claim(r, b.InstanceOf, b.Entity); + graph.claimValue(r, ++value, Bindings.INTEGER); + oldValue = value; + } + }); + } + void add() throws DatabaseException { + try { + session.syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + graph.claimValue(r, ++value, Bindings.INTEGER); + oldValue = value; + } + }); + } catch (Throwable e) { + throw new DatabaseException("Write transaction threw an unknown exception:", e); + } + } + void check() throws DatabaseException { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + Integer i = graph.getPossibleValue(r, Bindings.INTEGER); + if (null == i) { + if (0 != value) + throw new DatabaseException("Failed to verify value."); + } else if (value != i) + throw new DatabaseException("Failed to verify value. Expected " + value + " got " + i + "."); + } + }); + } + void checkStatements() throws DatabaseException { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + if (!graph.hasStatement(r)) + throw new DatabaseException("Failed to verify statments. Expected statements."); + } + }); + } + void checkNoStatements() throws DatabaseException { + session.syncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + if (graph.hasStatement(r)) + throw new DatabaseException("Failed to verify statments. Expected no statements."); + } + }); + } + void addAppend() throws DatabaseException { + try { + session.syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + graph.claimValue(r, ++value, Bindings.INTEGER); + } + }); + } catch (Throwable e) { + throw new DatabaseException("Write transaction threw an unknown exception:", e); + } + } + class ChangeSetIdentifierImpl implements ChangeSetIdentifier { + private final long id; + ChangeSetIdentifierImpl(long id) { + this.id = id; + } + @Override + public long getId() { + return id; + } + + @Override + public Map getMetadata() { + return null; + } + + } + void undo() throws DatabaseException { + support.undo(session, 1); + value -= 1; + } +}