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