]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest12.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 / UndoTest12.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.utils.DataContainer;
26
27 /**
28  * Tests cases where a non-functional relation uses
29  * {@link ReadGraph#getSingleObject(org.simantics.db.Resource, org.simantics.db.Resource)}
30  * with > 1 objects.
31  * 
32  * <p>
33  * Should throw {@link NoSingleResultException}, at the time of writing this
34  * test, AsyncBarrierImpl refcounting is trashed and DB client is stuck.
35  */
36 public class UndoTest12 extends ExistingDatabaseTest {
37
38         @Test
39     public void testUndo12() throws DatabaseException {
40
41         Session session = getSession();
42         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
43         final DataContainer<Resource> r1 = new DataContainer<Resource>();
44         final DataContainer<Resource> r2 = new DataContainer<Resource>();
45         try {
46             session.syncRequest(new WriteRequest() {
47                 @Override
48                 public void perform(WriteGraph g) throws DatabaseException {
49                     Resource a = g.newResource();
50                     Resource b = g.newResource();
51                     r1.set(a);
52                     r2.set(b);
53                     g.claim(a, a, a);
54                     g.deny(a, a, a);
55                     g.claim(a, a, b);
56                 }
57             });
58         } catch (Throwable e) {
59             fail("Write transaction threw an unknown exception " + e);
60         }
61
62         check(session, r1.get(), r2.get());
63
64         support.undo(session, 1);
65         support.redo(session, 1);
66
67         check(session, r1.get(), r2.get());
68
69     }
70     void check(Session session, final Resource a, final Resource b) {
71         try {
72             session.syncRequest(new ReadRequest() {
73                 @Override
74                 public void run(ReadGraph g) throws DatabaseException {
75                     if (g.hasStatement(a, a, a))
76                         fail("Deny did not work as expected.");
77                     if (!g.hasStatement(a, a, b))
78                         fail("Add did not work as expected.");
79                 }
80             });
81         } catch (Throwable e) {
82             fail("Read transaction threw an unexpected exception " + e);
83         }
84     }
85 }