/******************************************************************************* * 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; } }