]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/undoRedoSupport/UndoTest20.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 / UndoTest20.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.Arrays;
15
16 import org.junit.Test;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.common.request.WriteRequest;
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  * Tests undo of deletion of large literal when done twice in sequence.
32  * This was added because of Bug #4591.
33  */
34 public class UndoTest20 extends ExistingDatabaseTest {
35     private void initValue(byte[] value) {
36         for (int i=0; i<value.length; ++i)
37             value[i] = (byte)i;
38     }
39
40     @Test
41     public void testUndo20() throws Exception {
42         Session session = getSession();
43         final byte[] value = new byte[100000];
44         initValue(value);
45         final DataContainer<Resource> subject1 = new DataContainer<Resource>();
46         try {
47             session.syncRequest(new WriteRequest() {
48                 @Override
49                 public void perform(WriteGraph graph) throws DatabaseException {
50                     graph.markUndoPoint();
51                     Layer0 b = Layer0.getInstance(graph);
52                     Resource s1 = graph.newResource();
53                     graph.claim(s1, b.InstanceOf, b.Entity);
54                     graph.claimValue(s1, value);
55                     subject1.set(s1);
56                 }
57             });
58         } catch (Throwable e) {
59             fail("Write transaction threw an unknown exception ", e);
60         }
61         testUndo(session, value, subject1);
62         testUndo(session, value, subject1);
63     }
64     public void testUndo(final Session session, final byte[] value, final DataContainer<Resource> subject1)
65     throws Exception {
66         final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
67         try {
68             session.syncRequest(new ReadRequest() {
69                 @Override
70                 public void run(ReadGraph graph) throws DatabaseException {
71                     Resource s1 = subject1.get();
72                     byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
73                     if (!Arrays.equals(t1, value))
74                         fail("Failed to verify value.");
75                 }
76             });
77         } catch (Throwable e) {
78             fail("Read transaction threw an unexpected exception ", e);
79         }
80         try {
81             session.syncRequest(new WriteRequest() {
82                 @Override
83                 public void perform(WriteGraph graph) throws DatabaseException {
84                     graph.markUndoPoint();
85                     Resource s1 = subject1.get();
86                     graph.denyValue(s1);
87                 }
88             });
89         } catch (Throwable e) {
90             fail("Write transaction threw an unknown exception ", e);
91         }
92         try {
93             session.syncRequest(new ReadRequest() {
94                 @Override
95                 public void run(ReadGraph graph) throws DatabaseException {
96                     Resource s1 = subject1.get();
97                     byte[] t1 = graph.getPossibleValue(s1, Bindings.BYTE_ARRAY);
98                     if (null != t1)
99                         fail("Failed to verify delettion of value.");
100                 }
101             });
102         } catch (Throwable e) {
103             fail("Read transaction threw an unexpected exception ", e);
104         }
105         try {
106             support.undo(session, 1);
107         } catch (Throwable e) {
108             fail("Undo operation threw an unexpected exception.", e);
109         }
110         try {
111             session.syncRequest(new ReadRequest() {
112                 @Override
113                 public void run(ReadGraph graph) throws DatabaseException {
114                     Resource s1 = subject1.get();
115                     byte[] t1 = graph.getValue(s1, Bindings.BYTE_ARRAY);
116                     if (!Arrays.equals(t1, value))
117                         fail("Failed to verify value1.");
118                 }
119             });
120         } catch (Throwable e) {
121             fail("Read transaction threw an unexpected exception ", e);
122         }
123     }
124 }