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.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.ListIterator;
19 import org.junit.Test;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Session;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.request.ReadRequest;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.common.utils.OrderedSetUtils;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.service.UndoRedoSupport;
29 import org.simantics.db.testing.base.ExistingDatabaseTest;
30 import org.simantics.db.tests.common.Configuration;
31 import org.simantics.layer0.Layer0;
32 import org.simantics.utils.DataContainer;
35 * Test case description missing!
37 public class UndoTest09 extends ExistingDatabaseTest {
40 public void testUndo9() throws Exception {
42 Session session = getSession();
43 final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
45 UndoTestOrderedSet c1 = new UndoTestOrderedSet(session, support);
46 List<Resource> elements = new ArrayList<Resource>();
47 Resource list = c1.create(elements);
48 c1.check(list, elements);
49 List<Resource> elements2 = new ArrayList<Resource>();
50 elements2.addAll(elements);
51 c1.front(list, elements2);
52 c1.check(list, elements2);
53 c1.back(list, elements2);
54 c1.check(list, elements2);
57 c1.check(list, elements);
58 c1.add(list, elements);
60 } catch (Throwable t) {
61 fail("Test failed with exception " + t);
66 class UndoTestOrderedSet {
67 protected final Session session;
68 protected final UndoRedoSupport support;
69 protected final boolean DEBUG = Configuration.get().debug;
70 UndoTestOrderedSet(Session session, UndoRedoSupport support) {
71 this.session = session;
72 this.support = support;
74 Resource create(final List<Resource> els)
75 throws DatabaseException {
77 final DataContainer<Resource> subject = new DataContainer<Resource>();
78 session.syncRequest(new WriteRequest() {
80 public void perform(WriteGraph g) throws DatabaseException {
82 Layer0 b = Layer0.getInstance(g);
83 Resource l = OrderedSetUtils.create(g, b.Type);
85 for (int i=0; i<size; ++i) {
87 Resource el = g.newResource();
88 g.claim(el, b.InstanceOf, null, b.Type);
89 OrderedSetUtils.add(g, l, el);
96 void check(final Resource list, final List<Resource> els)
97 throws DatabaseException {
98 session.syncRequest(new ReadRequest() {
100 public void run(ReadGraph g) throws DatabaseException {
101 getFirstLast(g, list, els);
105 void front(final Resource list, final List<Resource> els)
106 throws DatabaseException {
107 session.syncRequest(new WriteRequest() {
109 public void perform(WriteGraph g) throws DatabaseException {
111 FirstLast fl = getFirstLast(g, list, els);
112 if (null == fl.first)
114 OrderedSetUtils.remove(g, list, fl.last);
115 OrderedSetUtils.addFirst(g, list, fl.last);
116 els.remove(fl.last2);
117 els.add(0, fl.last2);
121 void back(final Resource list, final List<Resource> els)
122 throws DatabaseException {
123 session.syncRequest(new WriteRequest() {
125 public void perform(WriteGraph g) throws DatabaseException {
127 FirstLast fl = getFirstLast(g, list, els);
128 if (null == fl.first)
130 OrderedSetUtils.remove(g, list, fl.first);
131 OrderedSetUtils.addAfter(g, list, fl.last, fl.first);
132 els.remove(fl.first2);
133 els.add(els.size(), fl.first2);
137 void add(final Resource list, final List<Resource> els)
138 throws DatabaseException {
139 session.syncRequest(new WriteRequest() {
141 public void perform(WriteGraph g) throws DatabaseException {
143 FirstLast fl = getFirstLast(g, list, els);
144 if (null == fl.first)
146 Layer0 b = Layer0.getInstance(g);
148 Resource el = g.newResource();
149 g.claim(el, b.InstanceOf, null, b.Type);
150 OrderedSetUtils.add(g, list, el);
155 void undo() throws DatabaseException {
156 support.undo(session, 1);
159 public Resource first;
160 public Resource first2;
161 public Resource last;
162 public Resource last2;
164 FirstLast getFirstLast(ReadGraph g, Resource list, List<Resource> els)
165 throws DatabaseException {
166 ListIterator<Resource> it = OrderedSetUtils.iterator(g, list);
168 Iterator<Resource> it2 = els.iterator();
169 FirstLast fl = new FirstLast();
170 while (it.hasNext()) {
172 if (null == fl.first)
175 System.out.println("Resource " + fl.last + "in list " + list + ".");
176 fl.last2 = it2.next();
177 if (null == fl.first2)
178 fl.first2 = fl.last2;
179 if (null == fl.last2 || fl.last2.getResourceId() != fl.last.getResourceId())
180 throw new DatabaseException("Error in list element rid=" + fl.last + ".");
183 if (els.size() != count)
184 throw new DatabaseException("Number of ordered set elements doesn't match!");
186 System.out.println("" + count + " elements in resource list.");