]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest15.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 / UndoTest15.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.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.NoSingleResultException;
23 import org.simantics.db.service.UndoRedoSupport;
24 import org.simantics.db.testing.base.ExistingDatabaseTest;
25
26 /**
27  * Tests cases where a non-functional relation uses
28  * {@link ReadGraph#getSingleObject(org.simantics.db.Resource, org.simantics.db.Resource)}
29  * with > 1 objects.
30  *
31  * <p>
32  * Should throw {@link NoSingleResultException}, at the time of writing this
33  * test, AsyncBarrierImpl refcounting is trashed and DB client is stuck.
34  */
35 public class UndoTest15 extends ExistingDatabaseTest {
36
37         @Test
38     public void testUndo15() throws DatabaseException {
39
40         Session session = getSession();
41         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
42         final Resource[] r = new Resource[2];
43         try {
44             session.syncRequest(new WriteRequest() {
45                 @Override
46                 public void perform(WriteGraph g) throws DatabaseException {
47                     g.markUndoPoint();
48                     r[0] = g.newResource();
49                     g.claim(r[0], r[0], r[0]);
50                 }
51             });
52         } catch (Throwable e) {
53             fail("Write transaction threw an unknown exception " + e);
54         }
55
56         session.syncRequest(new WriteRequest() {
57             @Override
58             public void perform(WriteGraph g) throws DatabaseException {
59                 g.markUndoPoint();
60                 r[1] = g.newResource();
61                 g.deny(r[0], r[0], r[0]);
62                 g.claim(r[0], r[0], r[1]);
63             }
64         });
65
66         session.syncRequest(new ReadRequest() {
67             @Override
68             public void run(ReadGraph g) throws DatabaseException {
69                 if (g.hasStatement(r[0], r[0], r[0]))
70                     fail("Deny did not work as expected.");
71                 if (!g.hasStatement(r[0], r[0], r[1]))
72                     fail("Claim did not work as expected.");
73             }
74         });
75         // Note that the implementation and interpretation of this operation has changed.
76         // This used to be undoRevisions but now this is undoOperations.
77         // Works as expected only for single operations.
78         support.undo(session, 2);
79
80         session.syncRequest(new ReadRequest() {
81             @Override
82             public void run(ReadGraph g) throws DatabaseException {
83                 if (g.hasStatement(r[0], r[0], r[0]))
84                     fail("Undo did not work as expected.");
85                 if (g.hasStatement(r[0], r[0], r[1]))
86                     fail("Undo did not work as expected.");
87             }
88         });
89         // Note that the implementation and interpretation of this operation has changed.
90         // Works as expected only for count one.
91         support.redo(session, 1);
92
93         session.syncRequest(new ReadRequest() {
94             @Override
95             public void run(ReadGraph g) throws DatabaseException {
96                 if (g.hasStatement(r[0], r[0], r[0]))
97                     fail("Undo did not work as expected.");
98                 if (!g.hasStatement(r[0], r[0], r[1]))
99                     fail("Undo did not work as expected.");
100             }
101         });
102     }
103 }