]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest18.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / support / undoRedoSupport / UndoTest18.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.support.undoRedoSupport;
13
14 import java.util.Arrays;
15
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;
29
30 /**
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)
37  */
38 public class UndoTest18 extends ExistingDatabaseTest {
39     private void initValue(byte[] value) {
40         for (int i=0; i<value.length; ++i)
41             value[i] = (byte)i;
42     }
43
44     @Test
45     public void testUndo1() throws Exception {
46
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];
55
56         try {
57             initValue(value10);
58             initValue(value11);
59             initValue(value20);
60             initValue(value21);
61             session.syncRequest(new WriteRequest() {
62                 @Override
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);
69                     subject1.set(s1);
70                 }
71             });
72         } catch (Throwable e) {
73             fail("Write transaction threw an unknown exception ", e);
74         }
75
76         try {
77             session.syncRequest(new WriteRequest() {
78                 @Override
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);
85                     subject2.set(s2);
86                 }
87             });
88         } catch (Throwable e) {
89             fail("Write transaction threw an unknown exception ", e);
90         }
91
92         try {
93             session.syncRequest(new ReadRequest() {
94                 @Override
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.");
104                 }
105             });
106         } catch (Throwable e) {
107             fail("Read transaction threw an unexpected exception ", e);
108         }
109
110         try {
111             session.syncRequest(new WriteRequest() {
112                 @Override
113                 public void perform(WriteGraph graph) throws DatabaseException {
114                     graph.markUndoPoint();
115                     graph.newResource();
116                 }
117             });
118         } catch (Throwable e) {
119             fail("Write transaction threw an unknown exception ", e);
120         }
121
122         try {
123             session.syncRequest(new WriteRequest() {
124                 @Override
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);
131                     subject1.set(s1);
132                     Resource s2 = subject2.get();
133                     graph.claim(s2, b.InstanceOf, b.Entity);
134                     graph.claimValue(s2, value21);
135                     subject2.set(s2);
136                 }
137             });
138         } catch (Throwable e) {
139             fail("Write transaction threw an unknown exception ", e);
140         }
141
142         try {
143             session.syncRequest(new ReadRequest() {
144                 @Override
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.");
154                 }
155             });
156         } catch (Throwable e) {
157             fail("Read transaction threw an unexpected exception ", e);
158         }
159
160         try {
161             support.undo(session, 2);
162         } catch (Throwable e) {
163             fail("Undo operation threw an unexpected exception.", e);
164         }
165
166         try {
167             session.syncRequest(new ReadRequest() {
168                 @Override
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.");
178                 }
179             });
180         } catch (Throwable e) {
181             fail("Read transaction threw an unexpected exception ", e);
182         }
183
184     }
185
186 }
187