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;
14 import java.util.Arrays;
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.service.UndoRedoSupport;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.DataContainer;
31 * Tests undo of two changes sets with values modified in multiple cluster change sets.
32 * The binary values will cause two cluster change sets per modification.
33 * (By the way the above-mentioned behavior will cause serious fragmentation in server
34 * because the size of cluster change sets are ridiculously small and server does not
35 * process cluster change sets but stores them as is. The payload is just about the same
36 * as the bookkeeping structures. TODO: fix the aforementioned fragmentation)
38 public class UndoTest18 extends ExistingDatabaseTest {
39 private void initValue(byte[] value) {
40 for (int i=0; i<value.length; ++i)
45 public void testUndo1() throws Exception {
47 Session session = getSession();
48 final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
49 final DataContainer<Resource> subject1 = new DataContainer<Resource>();
50 final DataContainer<Resource> subject2 = new DataContainer<Resource>();
51 final byte[] value10 = new byte[100000];
52 final byte[] value20 = new byte[100001];
53 final byte[] value11 = new byte[100002];
54 final byte[] value21 = new byte[100003];
61 session.syncRequest(new WriteRequest() {
63 public void perform(WriteGraph graph) throws DatabaseException {
64 graph.markUndoPoint();
65 Layer0 b = Layer0.getInstance(graph);
66 Resource s1 = graph.newResource();
67 graph.claim(s1, b.InstanceOf, b.Entity);
68 graph.claimValue(s1, value10);
72 } catch (Throwable e) {
73 fail("Write transaction threw an unknown exception ", e);
77 session.syncRequest(new WriteRequest() {
79 public void perform(WriteGraph graph) throws DatabaseException {
80 graph.markUndoPoint();
81 Layer0 b = Layer0.getInstance(graph);
82 Resource s2 = graph.newResource();
83 graph.claim(s2, b.InstanceOf, b.Entity);
84 graph.claimValue(s2, value20);
88 } catch (Throwable e) {
89 fail("Write transaction threw an unknown exception ", e);
93 session.syncRequest(new ReadRequest() {
95 public void run(ReadGraph graph) throws DatabaseException {
96 Resource s1 = subject1.get();
97 byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
98 if (!Arrays.equals(t1, value10))
99 fail("Failed to verify value1.");
100 Resource s2 = subject2.get();
101 byte[] t2 = graph.getValue(s2, Bindings.BYTE_ARRAY);
102 if (!Arrays.equals(t2, value20))
103 fail("Failed to verify value2.");
106 } catch (Throwable e) {
107 fail("Read transaction threw an unexpected exception ", e);
111 session.syncRequest(new WriteRequest() {
113 public void perform(WriteGraph graph) throws DatabaseException {
114 graph.markUndoPoint();
118 } catch (Throwable e) {
119 fail("Write transaction threw an unknown exception ", e);
123 session.syncRequest(new WriteRequest() {
125 public void perform(WriteGraph graph) throws DatabaseException {
126 graph.markUndoPoint();
127 Layer0 b = Layer0.getInstance(graph);
128 Resource s1 = subject1.get();
129 graph.claim(s1, b.InstanceOf, b.Entity);
130 graph.claimValue(s1, value11);
132 Resource s2 = subject2.get();
133 graph.claim(s2, b.InstanceOf, b.Entity);
134 graph.claimValue(s2, value21);
138 } catch (Throwable e) {
139 fail("Write transaction threw an unknown exception ", e);
143 session.syncRequest(new ReadRequest() {
145 public void run(ReadGraph graph) throws DatabaseException {
146 Resource s1 = subject1.get();
147 byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
148 if (!Arrays.equals(t1, value11))
149 fail("Failed to verify value11.");
150 Resource s2 = subject2.get();
151 byte[] t2 = graph.getValue(s2, Bindings.BYTE_ARRAY);
152 if (!Arrays.equals(t2, value21))
153 fail("Failed to verify value21.");
156 } catch (Throwable e) {
157 fail("Read transaction threw an unexpected exception ", e);
161 support.undo(session, 2);
162 } catch (Throwable e) {
163 fail("Undo operation threw an unexpected exception.", e);
167 session.syncRequest(new ReadRequest() {
169 public void run(ReadGraph graph) throws DatabaseException {
170 Resource s1 = subject1.get();
171 byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
172 if (!Arrays.equals(t1, value10))
173 fail("Failed to verify value1.");
174 Resource s2 = subject2.get();
175 byte[] t2 = graph.getValue(s2, Bindings.BYTE_ARRAY);
176 if (!Arrays.equals(t2, value20))
177 fail("Failed to verify value2.");
180 } catch (Throwable e) {
181 fail("Read transaction threw an unexpected exception ", e);