]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest05.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 / UndoTest05.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.exception.NoSingleResultException;
24 import org.simantics.db.service.UndoRedoSupport;
25 import org.simantics.db.testing.base.ExistingDatabaseTest;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.utils.DataContainer;
28
29 /**
30  * Tests cases where a non-functional relation uses
31  * {@link ReadGraph#getSingleObject(org.simantics.db.Resource, org.simantics.db.Resource)}
32  * with > 1 objects.
33  *
34  * <p>
35  * Should throw {@link NoSingleResultException}, at the time of writing this
36  * test, AsyncBarrierImpl refcounting is trashed and DB client is stuck.
37  */
38 public class UndoTest05 extends ExistingDatabaseTest {
39
40         @Test
41     public void testUndo5() throws Exception {
42
43         Session session = getSession();
44         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
45
46         final DataContainer<Resource> subject = new DataContainer<Resource>();
47         final String value = "value";
48         final String newValue = "new value";
49         try {
50             session.syncRequest(new WriteRequest() {
51                 @Override
52                 public void perform(WriteGraph graph) throws DatabaseException {
53                     graph.markUndoPoint();
54                     Layer0 b = Layer0.getInstance(graph);
55                     Resource s = graph.newResource();
56                     graph.claim(s, b.InstanceOf, b.Entity);
57                     graph.claimValue(s, value);
58                     subject.set(s);
59                 }
60             });
61         } catch (Throwable e) {
62             fail("Write transaction threw an unknown exception " + e);
63         }
64         try {
65             session.syncRequest(new ReadRequest() {
66                 @Override
67                 public void run(ReadGraph graph) throws DatabaseException {
68                     Resource s = subject.get();
69                     if (!graph.hasStatement(s))
70                         fail("Failed to verify modifications (stm).");
71                     String t = graph.getValue(s, Bindings.STRING);
72                     if (!t.equals(value))
73                         fail("Failed to verify modifications (value).");
74                 }
75             });
76         } catch (Throwable e) {
77             fail("Read transaction threw an unexpected exception " + e);
78         }
79
80         try {
81             session.syncRequest(new WriteRequest() {
82                 @Override
83                 public void perform(WriteGraph graph) throws DatabaseException {
84                     graph.markUndoPoint();
85                     Resource s = subject.get();
86                     graph.claimValue(s, newValue);
87                 }
88             });
89         } catch (Throwable e) {
90             fail("Write transaction threw an unknown exception " + e);
91         }
92
93         support.undo(session, 2);
94
95         try {
96             session.syncRequest(new ReadRequest() {
97                 @Override
98                 public void run(ReadGraph graph) throws DatabaseException {
99                     Resource s = subject.get();
100                     String t = graph.getPossibleValue(s, Bindings.STRING);
101                     if (null != t)
102                         fail("Failed to verify modifications (value).");
103                 }
104             });
105         } catch (Throwable e) {
106             fail("Read transaction threw an unexpected exception " + e);
107         }
108
109     }
110
111 }