]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest16.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 / UndoTest16.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  * Test case description missing!
32  */
33 public class UndoTest16 extends ExistingDatabaseTest {
34     private void initValue(byte[] value) {
35         for (int i=0; i<value.length; ++i)
36             value[i] = (byte)i;
37     }
38         @Test
39     public void testUndo16() throws Exception {
40
41         Session session = getSession();
42         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
43         final DataContainer<Resource> subject = new DataContainer<Resource>();
44         final byte[] value = new byte[100000];
45         final byte[] newValue = new byte[200000];
46         
47         try {
48             initValue(value);
49             initValue(newValue);
50             session.syncRequest(new WriteRequest() {
51                 @Override
52                 public void perform(WriteGraph graph) throws DatabaseException {
53                     graph.markUndoPoint();
54                     // Use any non-functional relation
55                     Layer0 b = Layer0.getInstance(graph);
56                     Resource s = graph.newResource();
57                     graph.claim(s, b.InstanceOf, b.Entity);
58                     graph.claimValue(s, value);
59                     subject.set(s);
60                 }
61             });
62         } catch (Throwable e) {
63             fail("Write transaction threw an unknown exception " + e);
64         }
65
66         try {
67             session.syncRequest(new ReadRequest() {
68                 @Override
69                 public void run(ReadGraph graph) throws DatabaseException {
70                     Resource s = subject.get();
71                     if (!graph.hasStatement(s))
72                         fail("Failed to verify modifications (stm).");
73                     byte[] t = graph.getValue(s, Bindings.BYTE_ARRAY);
74                     if (!Arrays.equals(t, value))
75                         fail("Failed to verify modifications (value).");
76                 }
77             });
78         } catch (Throwable e) {
79             fail("Read transaction threw an unexpected exception " + e);
80         }
81
82         try {
83             session.syncRequest(new WriteRequest() {
84                 @Override
85                 public void perform(WriteGraph graph) throws DatabaseException {
86                     graph.markUndoPoint();
87                     Resource s = subject.get();
88                     graph.claimValue(s, newValue);
89                 }
90             });
91         } catch (Throwable e) {
92             fail("Write transaction threw an unknown exception " + e);
93         }
94
95         support.undo(session, 1);
96
97         try {
98             session.syncRequest(new ReadRequest() {
99                 @Override
100                 public void run(ReadGraph graph) throws DatabaseException {
101                     Resource s = subject.get();
102                     byte[] t = graph.getValue(s, Bindings.BYTE_ARRAY);
103                     if (!Arrays.equals(t, value))
104                         fail("Failed to verify modifications (value).");
105                 }
106             });
107         } catch (Throwable e) {
108             fail("Read transaction threw an unexpected exception " + e);
109         }
110
111         support.undo(session, 1);
112
113         try {
114             session.syncRequest(new ReadRequest() {
115                 @Override
116                 public void run(ReadGraph graph) throws DatabaseException {
117                     Resource s = subject.get();
118                     byte[] t = graph.getPossibleValue(s, Bindings.BYTE_ARRAY);
119                     if (null != t)
120                         fail("Failed to verify modifications (value).");
121                 }
122             });
123         } catch (Throwable e) {
124             fail("Read transaction threw an unexpected exception " + e);
125         }
126     }
127 }