]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest03.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 / UndoTest03.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.ListIterator;
15
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;
29
30 /**
31  * Test case description missing!
32  */
33 public class UndoTest03 extends ExistingDatabaseTest {
34
35         @Test
36     public void testUndo3() throws Exception {
37
38         Session session = getSession();
39         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
40         final DataContainer<Resource> subject = new DataContainer<Resource>();
41         final int size = 3;
42         try {
43             session.syncRequest(new WriteRequest() {
44                 @Override
45                 public void perform(WriteGraph g) throws DatabaseException {
46                     Layer0 b = Layer0.getInstance(g);
47                     Resource l = OrderedSetUtils.create(g, b.Type);
48                     subject.set(l);
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);
53                     }
54                 }
55             });
56         } catch (Throwable e) {
57             fail("Write transaction threw an unknown exception " + e);
58         }
59
60         try {
61             session.syncRequest(new ReadRequest() {
62                 @Override
63                 public void run(ReadGraph g) throws DatabaseException {
64                     Resource l = subject.get();
65                     ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
66                     int count = 0;
67                     while (it.hasNext()) {
68                         Resource r = it.next();
69                         if (DEBUG)
70                             System.out.println("Resource " + r);
71                         ++count;
72                     }
73                     if (size != count)
74                         fail("Number of ordered set elements doesn't match!");
75                 }
76             });
77         } catch (Throwable e) {
78             fail("Read transaction threw an unexpected exception " + e);
79         }
80
81         try {
82             session.syncRequest(new WriteRequest() {
83                 @Override
84                 public void perform(WriteGraph g) throws DatabaseException {
85                     g.markUndoPoint();
86                     Resource l = subject.get();
87                     ListIterator<Resource> a = OrderedSetUtils.iterator(g, l);
88                     Resource el = null;
89                     if (!a.hasNext())
90                         fail("Number of elements doesn't match!");
91                     el = a.next();
92                     OrderedSetUtils.remove(g, l, el);
93                 }
94             });
95         } catch (Throwable e) {
96             fail("Write transaction threw an unknown exception " + e);
97         }
98
99         try {
100             session.syncRequest(new ReadRequest() {
101                 @Override
102                 public void run(ReadGraph g) throws DatabaseException {
103                     Resource l = subject.get();
104                     ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
105                     int count = 0;
106                     while (it.hasNext()) {
107                         Resource r = it.next();
108                         if (DEBUG)
109                             System.out.println("Resource " + r);
110                         ++count;
111                     }
112                     if (size != count+1)
113                         fail("Number of ordered set elements doesn't match!");
114                 }
115             });
116         } catch (Throwable e) {
117             fail("Read transaction threw an unexpected exception " + e);
118         }
119
120         support.undo(session, 1);
121
122         try {
123             session.syncRequest(new ReadRequest() {
124                 @Override
125                 public void run(ReadGraph g) throws DatabaseException {
126                     Resource l = subject.get();
127                     ListIterator<Resource> it = OrderedSetUtils.iterator(g, l);
128                     int count = 0;
129                     while (it.hasNext()) {
130                         Resource r = it.next();
131                         if (DEBUG)
132                             System.out.println("Resource " + r);
133                         ++count;
134                     }
135                     if (size != count)
136                         fail("Number of ordered set elements doesn't match!");
137                 }
138             });
139         } catch (Throwable e) {
140             fail("Read transaction threw an unexpected exception " + e);
141         }
142
143     }
144
145 }