]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest06.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 / UndoTest06.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 org.junit.Test;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.UndoContext;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.UndoContextEx;
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
29 /**
30  * Test case description missing!
31  */
32 public class UndoTest06 extends ExistingDatabaseTest {
33
34         @Test
35     public void testUndo6() throws Exception {
36
37         Session session = getSession();
38         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
39         try {
40             Context c1 = new Context(session, support);
41             c1.create();
42             c1.check();
43             c1.add();
44             c1.check();
45             c1.add();
46             c1.check();
47
48             c1.undo();
49             c1.check();
50             c1.undo();
51             c1.check();
52
53             c1.add();
54             c1.check();
55             c1.add();
56             c1.check();
57
58             Context c2 = new Context(session, support);
59             c2.create();
60             c2.check();
61             c2.add();
62             c2.check();
63             c2.add();
64             c2.check();
65             
66 //            c1.undo();
67 //            c1.check();
68 //            c1.undo();
69 //            c1.check();
70             
71             c2.undo();
72             c2.check();
73             c2.undo();
74             c2.check();
75         } catch (Throwable t) {
76             fail("Test failed with exception " + t);
77         }
78     }
79
80     class Context {
81         private final Session session;
82         private final UndoRedoSupport support;
83         final UndoContext uctx = new UndoContextEx();
84         Resource r;
85         Integer value = 0;
86         Context(Session session, UndoRedoSupport support) {
87             this.session = session;
88             this.support = support;
89         }
90         void create() throws DatabaseException {
91             session.syncRequest(new WriteRequest() {
92                 @Override
93                 public void perform(WriteGraph graph) throws DatabaseException {
94                     graph.markUndoPoint();
95                     Layer0 b = Layer0.getInstance(graph);
96                     r = graph.newResource();
97                     graph.claim(r, b.InstanceOf, b.Entity);
98                 }
99             });
100         }
101         void add() throws DatabaseException {
102             try {
103                 session.syncRequest(new WriteRequest() {
104                     @Override
105                     public void perform(WriteGraph graph) throws DatabaseException {
106                         graph.markUndoPoint();
107                         graph.claimValue(r, ++value, Bindings.INTEGER);
108                     }
109                 });
110             } catch (Throwable e) {
111                 fail("Write transaction threw an unknown exception " + e);
112             }
113         }
114         void check() throws DatabaseException {
115             session.syncRequest(new ReadRequest() {
116                 @Override
117                 public void run(ReadGraph graph) throws DatabaseException {
118                     Integer i = graph.getPossibleValue(r, Bindings.INTEGER);
119                     if (null == i) {
120                         if (0 != value)
121                             fail("Failed to verify value.");
122                     } else if (value != i)
123                         fail("Failed to verify value expected " + value + " got " + i);
124                 }
125             });
126         }
127         void undo() throws DatabaseException {
128             support.undo(session, 1);
129             value -= 1;
130         }
131     }
132 }