]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest17.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 / UndoTest17.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.WriteGraph;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.service.UndoRedoSupport;
24 import org.simantics.db.testing.base.ExistingDatabaseTest;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.utils.DataContainer;
27
28 /**
29  * Tests undo of added value with deleted value as previous operation.
30  */
31 public class UndoTest17 extends ExistingDatabaseTest {
32
33     @Test
34     public void testUndo1() throws Exception {
35
36         Session session = getSession();
37         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
38         final DataContainer<Resource> subject = new DataContainer<Resource>();
39         final String value = "value";
40
41         try {
42             session.syncRequest(new WriteRequest() {
43                 @Override
44                 public void perform(WriteGraph graph) throws DatabaseException {
45                     Layer0 b = Layer0.getInstance(graph);
46                     Resource s = graph.newResource();
47                     graph.claim(s, b.InstanceOf, b.Entity);
48                     graph.claimValue(s, value);
49                     subject.set(s);
50                 }
51             });
52         } catch (Throwable e) {
53             fail("Write transaction threw an unknown exception ", e);
54         }
55
56         try {
57             session.syncRequest(new ReadRequest() {
58                 @Override
59                 public void run(ReadGraph graph) throws DatabaseException {
60                     Resource s = subject.get();
61                     if (!graph.hasStatement(s))
62                         fail("Failed to verify modifications (stm).");
63                     String t = graph.getValue(s, Bindings.STRING);
64                     if (!t.equals(value))
65                         fail("Failed to verify modifications (value).");
66                     
67                 }
68             });
69         } catch (Throwable e) {
70             fail("Read transaction threw an unexpected exception ", e);
71         }
72
73         try {
74             session.syncRequest(new WriteRequest() {
75                 @Override
76                 public void perform(WriteGraph graph) throws DatabaseException {
77                     Resource s = subject.get();
78                     graph.denyValue(s);
79                     subject.set(s);
80                 }
81             });
82         } catch (Throwable e) {
83             fail("Write transaction threw an unknown exception ", e);
84         }
85
86         try {
87             session.syncRequest(new ReadRequest() {
88                 @Override
89                 public void run(ReadGraph graph) throws DatabaseException {
90                     Resource s = subject.get();
91                     String t = graph.getPossibleValue(s);
92                     if (null != t)
93                         fail("Failed to verify removal of value.");
94                     
95                 }
96             });
97         } catch (Throwable e) {
98             fail("Read transaction threw an unexpected exception ", e);
99         }
100
101         try {
102             session.syncRequest(new WriteRequest() {
103                 @Override
104                 public void perform(WriteGraph graph) throws DatabaseException {
105                     Resource s = subject.get();
106                     graph.claimValue(s, value);
107                     subject.set(s);
108                 }
109             });
110         } catch (Throwable e) {
111             fail("Write transaction threw an unknown exception ", e);
112         }
113
114         try {
115             session.syncRequest(new ReadRequest() {
116                 @Override
117                 public void run(ReadGraph graph) throws DatabaseException {
118                     Resource s = subject.get();
119                     String t = graph.getValue(s, Bindings.STRING);
120                     if (!t.equals(value))
121                         fail("Failed to verify modifications (value).");
122                     
123                 }
124             });
125         } catch (Throwable e) {
126             fail("Read transaction threw an unexpected exception ", e);
127         }
128
129         try {
130             support.undo(session, 1);
131         } catch (Throwable e) {
132             fail("Undo operation threw an unexpected exception.", e);
133         }
134
135         try {
136             session.syncRequest(new ReadRequest() {
137                 @Override
138                 public void run(ReadGraph graph) throws DatabaseException {
139                     Resource s = subject.get();
140                     String t = graph.getPossibleValue(s);
141                     if (null != t)
142                         fail("Failed to verify removal of value.");
143                     
144                 }
145             });
146         } catch (Throwable e) {
147             fail("Read transaction threw an unexpected exception ", e);
148         }
149
150     }
151
152 }
153