1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.support.undoRedoSupport;
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ChangeSetIdentifier;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.service.UndoRedoSupport;
27 import org.simantics.db.testing.base.ExistingDatabaseTest;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.utils.DataContainer;
32 * Tests undo of add statement.
34 public class UndoTest01 extends ExistingDatabaseTest {
37 public void testUndo1() throws Exception {
39 Session session = getSession();
40 final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
41 final DataContainer<Resource> subject = new DataContainer<Resource>();
44 session.syncRequest(new WriteRequest() {
46 public void perform(WriteGraph graph) throws DatabaseException {
47 // Use any non-functional relation
48 Layer0 b = Layer0.getInstance(graph);
49 Resource s = graph.newResource();
50 graph.claim(s, b.InstanceOf, b.Entity);
54 } catch (Throwable e) {
55 fail("Write transaction threw an unknown exception ", e);
59 session.syncRequest(new ReadRequest() {
61 public void run(ReadGraph graph) throws DatabaseException {
62 assert(graph.hasStatement(subject.get()));
65 } catch (Throwable e) {
66 fail("Read transaction threw an unexpected exception ", e);
69 support.undo(session, 1);
70 } catch (Throwable e) {
71 fail("Undo operation threw an unexpected exception.", e);
75 session.syncRequest(new ReadRequest() {
77 public void run(ReadGraph graph) throws DatabaseException {
78 assert(!graph.hasStatement(subject.get()));
81 } catch (Throwable e) {
83 fail("Read transaction threw an unexpected exception ", e);
91 private final Session session;
92 private final UndoRedoSupport support;
96 Context(Session session, UndoRedoSupport support) {
97 this.session = session;
98 this.support = support;
100 void create() throws DatabaseException {
101 session.syncRequest(new WriteRequest() {
103 public void perform(WriteGraph graph) throws DatabaseException {
104 graph.markUndoPoint();
105 Layer0 b = Layer0.getInstance(graph);
106 r = graph.newResource();
107 graph.claim(r, b.InstanceOf, b.Entity);
111 void createAndSet() throws DatabaseException {
112 session.syncRequest(new WriteRequest() {
114 public void perform(WriteGraph graph) throws DatabaseException {
115 graph.markUndoPoint();
116 Layer0 b = Layer0.getInstance(graph);
117 r = graph.newResource();
118 graph.claim(r, b.InstanceOf, b.Entity);
119 graph.claimValue(r, ++value, Bindings.INTEGER);
124 void add() throws DatabaseException {
126 session.syncRequest(new WriteRequest() {
128 public void perform(WriteGraph graph) throws DatabaseException {
129 graph.markUndoPoint();
130 graph.claimValue(r, ++value, Bindings.INTEGER);
134 } catch (Throwable e) {
135 throw new DatabaseException("Write transaction threw an unknown exception:", e);
138 void check() throws DatabaseException {
139 session.syncRequest(new ReadRequest() {
141 public void run(ReadGraph graph) throws DatabaseException {
142 Integer i = graph.getPossibleValue(r, Bindings.INTEGER);
145 throw new DatabaseException("Failed to verify value.");
146 } else if (value != i)
147 throw new DatabaseException("Failed to verify value. Expected " + value + " got " + i + ".");
151 void checkStatements() throws DatabaseException {
152 session.syncRequest(new ReadRequest() {
154 public void run(ReadGraph graph) throws DatabaseException {
155 if (!graph.hasStatement(r))
156 throw new DatabaseException("Failed to verify statments. Expected statements.");
160 void checkNoStatements() throws DatabaseException {
161 session.syncRequest(new ReadRequest() {
163 public void run(ReadGraph graph) throws DatabaseException {
164 if (graph.hasStatement(r))
165 throw new DatabaseException("Failed to verify statments. Expected no statements.");
169 void addAppend() throws DatabaseException {
171 session.syncRequest(new WriteRequest() {
173 public void perform(WriteGraph graph) throws DatabaseException {
174 graph.markUndoPoint();
175 graph.claimValue(r, ++value, Bindings.INTEGER);
178 } catch (Throwable e) {
179 throw new DatabaseException("Write transaction threw an unknown exception:", e);
182 class ChangeSetIdentifierImpl implements ChangeSetIdentifier {
183 private final long id;
184 ChangeSetIdentifierImpl(long id) {
188 public long getId() {
193 public Map<String, byte[]> getMetadata() {
198 void undo() throws DatabaseException {
199 support.undo(session, 1);