]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest01.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 / UndoTest01.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.Map;
15
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;
30
31 /**
32  * Tests undo of add statement.
33  */
34 public class UndoTest01 extends ExistingDatabaseTest {
35
36         @Test
37     public void testUndo1() throws Exception {
38
39         Session session = getSession();
40         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
41         final DataContainer<Resource> subject = new DataContainer<Resource>();
42         
43         try {
44             session.syncRequest(new WriteRequest() {
45                 @Override
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);
51                     subject.set(s);
52                 }
53             });
54         } catch (Throwable e) {
55             fail("Write transaction threw an unknown exception ", e);
56         }
57
58         try {
59             session.syncRequest(new ReadRequest() {
60                 @Override
61                 public void run(ReadGraph graph) throws DatabaseException {
62                     assert(graph.hasStatement(subject.get()));
63                 }
64             });
65         } catch (Throwable e) {
66             fail("Read transaction threw an unexpected exception ", e);
67         }
68         try {
69             support.undo(session, 1);
70         } catch (Throwable e) {
71             fail("Undo operation threw an unexpected exception.", e);
72         }
73         
74         try {
75             session.syncRequest(new ReadRequest() {
76                 @Override
77                 public void run(ReadGraph graph) throws DatabaseException {
78                         assert(!graph.hasStatement(subject.get()));
79                 }
80             });
81         } catch (Throwable e) {
82             e.printStackTrace();
83             fail("Read transaction threw an unexpected exception ", e);
84         }
85         
86     }
87
88 }
89
90 class Context {
91     private final Session session;
92     private final UndoRedoSupport support;
93     Resource r;
94     Integer value = 0;
95     Integer oldValue = 0;
96     Context(Session session, UndoRedoSupport support) {
97         this.session = session;
98         this.support = support;
99     }
100     void create() throws DatabaseException {
101         session.syncRequest(new WriteRequest() {
102             @Override
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);
108             }
109         });
110     }
111     void createAndSet() throws DatabaseException {
112         session.syncRequest(new WriteRequest() {
113             @Override
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);
120                 oldValue = value;
121             }
122         });
123     }
124     void add() throws DatabaseException {
125         try {
126             session.syncRequest(new WriteRequest() {
127                 @Override
128                 public void perform(WriteGraph graph) throws DatabaseException {
129                     graph.markUndoPoint();
130                     graph.claimValue(r, ++value, Bindings.INTEGER);
131                     oldValue = value;
132                 }
133             });
134         } catch (Throwable e) {
135             throw new DatabaseException("Write transaction threw an unknown exception:", e);
136         }
137     }
138     void check() throws DatabaseException {
139         session.syncRequest(new ReadRequest() {
140             @Override
141             public void run(ReadGraph graph) throws DatabaseException {
142                 Integer i = graph.getPossibleValue(r, Bindings.INTEGER);
143                 if (null == i) {
144                     if (0 != value)
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 + ".");
148             }
149         });
150     }
151     void checkStatements() throws DatabaseException {
152         session.syncRequest(new ReadRequest() {
153             @Override
154             public void run(ReadGraph graph) throws DatabaseException {
155                if (!graph.hasStatement(r))
156                    throw new DatabaseException("Failed to verify statments. Expected statements.");
157             }
158         });
159     }
160     void checkNoStatements() throws DatabaseException {
161         session.syncRequest(new ReadRequest() {
162             @Override
163             public void run(ReadGraph graph) throws DatabaseException {
164                if (graph.hasStatement(r))
165                    throw new DatabaseException("Failed to verify statments. Expected no statements.");
166             }
167         });
168     }
169     void addAppend() throws DatabaseException {
170         try {
171             session.syncRequest(new WriteRequest() {
172                 @Override
173                 public void perform(WriteGraph graph) throws DatabaseException {
174                     graph.markUndoPoint();
175                     graph.claimValue(r, ++value, Bindings.INTEGER);
176                 }
177             });
178         } catch (Throwable e) {
179             throw new DatabaseException("Write transaction threw an unknown exception:", e);
180         }
181     }
182     class ChangeSetIdentifierImpl implements ChangeSetIdentifier {
183         private final long id;
184         ChangeSetIdentifierImpl(long id) {
185             this.id = id;
186         }
187         @Override
188         public long getId() {
189             return id;
190         }
191
192         @Override
193         public Map<String, byte[]> getMetadata() {
194             return null;
195         }
196         
197     }
198     void undo() throws DatabaseException {
199         support.undo(session, 1);
200         value -= 1;
201     }
202 }