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 UndoTest03 extends ExistingDatabaseTest {
36 public void testUndo3() throws Exception {
38 Session session = getSession();
39 final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
40 final DataContainer<Resource> subject = new DataContainer<Resource>();
43 session.syncRequest(new WriteRequest() {
45 public void perform(WriteGraph g) throws DatabaseException {
46 Layer0 b = Layer0.getInstance(g);
47 Resource l = OrderedSetUtils.create(g, b.Type);
49 for (int i=0; i<size; ++i) {
50 Resource el = g.newResource();
51 g.claim(el, b.InstanceOf, null, b.Type);
52 OrderedSetUtils.add(g, l, el);
56 } catch (Throwable e) {
57 fail("Write transaction threw an unknown exception " + e);
61 session.syncRequest(new ReadRequest() {
63 public void run(ReadGraph g) throws DatabaseException {
64 Resource l = subject.get();
65 ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
67 while (it.hasNext()) {
68 Resource r = it.next();
70 System.out.println("Resource " + r);
74 fail("Number of ordered set elements doesn't match!");
77 } catch (Throwable e) {
78 fail("Read transaction threw an unexpected exception " + e);
82 session.syncRequest(new WriteRequest() {
84 public void perform(WriteGraph g) throws DatabaseException {
86 Resource l = subject.get();
87 ListIterator<Resource> a = OrderedSetUtils.iterator(g, l);
90 fail("Number of elements doesn't match!");
92 OrderedSetUtils.remove(g, l, el);
95 } catch (Throwable e) {
96 fail("Write transaction threw an unknown exception " + e);
100 session.syncRequest(new ReadRequest() {
102 public void run(ReadGraph g) throws DatabaseException {
103 Resource l = subject.get();
104 ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
106 while (it.hasNext()) {
107 Resource r = it.next();
109 System.out.println("Resource " + r);
113 fail("Number of ordered set elements doesn't match!");
116 } catch (Throwable e) {
117 fail("Read transaction threw an unexpected exception " + e);
120 support.undo(session, 1);
123 session.syncRequest(new ReadRequest() {
125 public void run(ReadGraph g) throws DatabaseException {
126 Resource l = subject.get();
127 ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
129 while (it.hasNext()) {
130 Resource r = it.next();
132 System.out.println("Resource " + r);
136 fail("Number of ordered set elements doesn't match!");
139 } catch (Throwable e) {
140 fail("Read transaction threw an unexpected exception " + e);