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