]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest09.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 / UndoTest09.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 java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.ListIterator;
18
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;
33
34 /**
35  * Test case description missing!
36  */
37 public class UndoTest09 extends ExistingDatabaseTest {
38     
39         @Test
40     public void testUndo9() throws Exception {
41
42         Session session = getSession();
43         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
44         try {
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);
55             c1.undo();
56             c1.undo();
57             c1.check(list, elements);
58             c1.add(list, elements);
59             elements2.clear();
60         } catch (Throwable t) {
61             fail("Test failed with exception " + t);
62         }
63     }
64 }
65
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;
73     }
74     Resource create(final List<Resource> els)
75     throws DatabaseException {
76         final int size = 3;
77         final DataContainer<Resource> subject = new DataContainer<Resource>();
78         session.syncRequest(new WriteRequest() {
79             @Override
80             public void perform(WriteGraph g) throws DatabaseException {
81                 g.markUndoPoint();
82                 Layer0 b = Layer0.getInstance(g);
83                 Resource l = OrderedSetUtils.create(g, b.Type);
84                 subject.set(l);
85                 for (int i=0; i<size; ++i) {
86                     g.flushCluster();
87                     Resource el = g.newResource();
88                     g.claim(el, b.InstanceOf, null, b.Type);
89                     OrderedSetUtils.add(g, l, el);
90                     els.add(el);
91                 }
92             }
93         });
94         return subject.get();
95     }
96     void check(final Resource list, final List<Resource> els)
97     throws DatabaseException {
98         session.syncRequest(new ReadRequest() {
99             @Override
100             public void run(ReadGraph g) throws DatabaseException {
101                 getFirstLast(g, list, els);
102             }
103         });
104     }
105     void front(final Resource list, final List<Resource> els)
106     throws DatabaseException {
107         session.syncRequest(new WriteRequest() {
108             @Override
109             public void perform(WriteGraph g) throws DatabaseException {
110                 g.markUndoPoint();
111                 FirstLast fl = getFirstLast(g, list, els);
112                 if (null == fl.first)
113                     return;
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);
118             }
119         });
120     }
121     void back(final Resource list, final List<Resource> els)
122     throws DatabaseException {
123         session.syncRequest(new WriteRequest() {
124             @Override
125             public void perform(WriteGraph g) throws DatabaseException {
126                 g.markUndoPoint();
127                 FirstLast fl = getFirstLast(g, list, els);
128                 if (null == fl.first)
129                     return;
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);
134             }
135         });
136     }
137     void add(final Resource list, final List<Resource> els)
138     throws DatabaseException {
139         session.syncRequest(new WriteRequest() {
140             @Override
141             public void perform(WriteGraph g) throws DatabaseException {
142                 g.markUndoPoint();
143                 FirstLast fl = getFirstLast(g, list, els);
144                 if (null == fl.first)
145                     return;
146                 Layer0 b = Layer0.getInstance(g);
147                 g.flushCluster();
148                 Resource el = g.newResource();
149                 g.claim(el, b.InstanceOf, null, b.Type);
150                 OrderedSetUtils.add(g, list, el);
151                 els.add(el);
152             }
153         });
154     }
155     void undo() throws DatabaseException {
156         support.undo(session, 1);
157     }
158     class FirstLast {
159         public Resource first;
160         public Resource first2;
161         public Resource last;
162         public Resource last2;
163     }
164     FirstLast getFirstLast(ReadGraph g, Resource list, List<Resource> els)
165     throws DatabaseException {
166         ListIterator<Resource> it = OrderedSetUtils.iterator(g, list);
167         int count = 0;
168         Iterator<Resource> it2 = els.iterator();
169         FirstLast fl = new FirstLast();
170         while (it.hasNext()) {
171             fl.last = it.next();
172             if (null == fl.first)
173                 fl.first = fl.last;
174             if (DEBUG)
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 + ".");
181             ++count;
182         }
183         if (els.size() != count)
184             throw new DatabaseException("Number of ordered set elements doesn't match!");
185         if (DEBUG)
186             System.out.println("" + count + " elements in resource list.");
187         return fl;
188     }
189 }