1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.support.undoRedoSupport;
14 import java.util.ListIterator;
16 import org.junit.Test;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.ReadRequest;
22 import org.simantics.db.common.request.WriteRequest;
23 import org.simantics.db.common.utils.OrderedSetUtils;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.service.UndoRedoSupport;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.DataContainer;
31 * Test case description missing!
33 public class UndoTest02 extends ExistingDatabaseTest {
34 private boolean DEBUG = false;
36 public void testUndo2() throws Exception {
38 Session session = getSession();
39 final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
41 final DataContainer<Resource> subject = new DataContainer<Resource>();
44 session.syncRequest(new WriteRequest() {
46 public void perform(WriteGraph g) throws DatabaseException {
47 Layer0 b = Layer0.getInstance(g);
48 Resource l = OrderedSetUtils.create(g, b.Type);
50 for (int i=0; i<size; ++i) {
51 Resource el = g.newResource();
52 g.claim(el, b.InstanceOf, null, b.Type);
53 OrderedSetUtils.add(g, l, el);
57 } catch (Throwable e) {
58 fail("Write transaction threw an unknown exception ", e);
62 session.syncRequest(new ReadRequest() {
64 public void run(ReadGraph g) throws DatabaseException {
65 Resource l = subject.get();
66 ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
68 while (it.hasNext()) {
69 Resource r = it.next();
71 System.out.println("Resource " + r);
75 fail("Number of ordered set elements doesn't match!");
78 } catch (Throwable e) {
79 fail("Read transaction threw an unexpected exception ", e);
83 session.syncRequest(new WriteRequest() {
85 public void perform(WriteGraph g) throws DatabaseException {
87 Layer0 b = Layer0.getInstance(g);
88 Resource l = subject.get();
89 Resource el = g.newResource();
90 g.claim(el, b.InstanceOf, null, b.Type);
91 OrderedSetUtils.add(g, l, el);
94 } catch (Throwable e) {
95 fail("Write transaction threw an unknown exception ", e);
99 session.syncRequest(new ReadRequest() {
101 public void run(ReadGraph g) throws DatabaseException {
102 Resource l = subject.get();
103 ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
105 while (it.hasNext()) {
106 Resource r = it.next();
108 System.out.println("Resource " + r);
112 fail("Number of ordered set elements doesn't match!");
115 } catch (Throwable e) {
116 fail("Read transaction threw an unexpected exception ", e);
118 support.undo(session, 1);
121 session.syncRequest(new ReadRequest() {
123 public void run(ReadGraph g) throws DatabaseException {
124 Resource l = subject.get();
125 ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
127 while (it.hasNext()) {
128 Resource r = it.next();
130 System.out.println("Resource " + r);
134 fail("Number of ordered set elements doesn't match! expected=" + size + " got=" + count);
137 } catch (Throwable e) {
138 fail("Read transaction threw an unexpected exception ", e);