]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3176Test3.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / regression / bugs / Issue3176Test3.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.regression.bugs;
13
14 import static org.junit.Assert.assertTrue;
15 import org.junit.Test;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.databoard.accessor.ArrayAccessor;
18 import org.simantics.databoard.accessor.error.AccessorException;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.testing.base.ExistingDatabaseTest;
27 import org.simantics.db.testing.impl.Configuration;
28 import org.simantics.layer0.Layer0;
29
30 public class Issue3176Test3 extends ExistingDatabaseTest {
31
32     @Test
33     public void testAccessor() {
34         try {
35             int nBlocks = Configuration.get().i3176BlockCount;
36             int[] blockSize = { 1024, 0xFFFF, 1<<20 };
37             final int LENGTH = blockSize.length - 2; // too slow
38             for (int i=0; i<LENGTH; ++i) {
39                 testAccessor1(blockSize[i], nBlocks);
40                 testAccessor2(blockSize[i], nBlocks);
41                 testAccessor3(blockSize[i], nBlocks);
42             }
43             int nBlocksBig = Configuration.get().i3176BigBlockCount / 10; // too slow
44             nBlocksBig = Math.max(2, nBlocksBig);
45             testAccessor4(blockSize[blockSize.length - 2], nBlocksBig); // too slow
46         } catch (DatabaseException e) {
47             fail("Test failed with exception " + e);
48         }
49     }
50     public void testAccessor1(int blockSize, int nBlocks)
51     throws DatabaseException {
52         final AccessorTest test = new AccessorTest(getSession(), DEBUG, blockSize, nBlocks);
53         Resource r = test.createResource();
54         test.checkNoAccessor(r);
55         test.createAccessor(r);
56         test.checkLiteralValue(r);
57         test.validateAccessor(r, 0, -1);
58         test.modifyAccessor(r, 1, false);
59         test.validateAccessor(r, 1, -1);
60         test.undoModi(r, 1);
61         test.validateAccessor(r, 0, -1);
62         test.removeLiteralValue(r);
63         test.checkNoAccessor(r);
64     }
65     public void testAccessor2(int blockSize, int nBlocks)
66     throws DatabaseException {
67         final AccessorTest test = new AccessorTest(getSession(), DEBUG, blockSize, nBlocks);
68         Resource r = test.createResource();
69         test.checkNoAccessor(r);
70         test.createAccessor(r);
71         test.validateAccessor(r, 0, -1);
72         final int N=10;
73         for (int i=1; i<N+1; ++i) {
74             test.modifyAccessor(r, i, false);
75             test.validateAccessor(r, i, -1);
76         }
77         test.undoModi(r, N);
78         test.validateAccessor(r, 0, -1);
79         test.removeLiteralValue(r);
80         test.checkNoAccessor(r);
81     }
82     public void testAccessor3(int blockSize, int nBlocks)
83     throws DatabaseException {
84         final AccessorTest test = new AccessorTest(getSession(), DEBUG, blockSize, nBlocks);
85         Resource r = test.createResource();
86         test.checkNoAccessor(r);
87         test.createAccessor(r);
88         test.validateAccessor(r, 0, -1);
89         test.modifyAccessor(r, 1, false);
90         test.validateAccessor(r, 1, -1);
91         test.truncateAccessor(r, 1, 1);
92         test.validateAccessor(r, 1, 1);
93     }
94     public void testAccessor4(int blockSize, int nBlocks)
95     throws DatabaseException {
96         final AccessorTest test = new AccessorTest(getSession(), DEBUG, blockSize, nBlocks);
97         Resource r = test.createResource();
98         test.createAccessor(r);
99         test.validateAccessor(r, 0, -1);
100         test.modifyAccessor(r, 1, false);
101         test.validateAccessor(r, 1, -1);
102     }
103 }
104 class AccessorTest extends Issue3176Test {
105     AccessorTest(final Session session, boolean DEBUG, int blockSize, int nBlocks)
106     throws DatabaseException {
107         super(session, DEBUG, blockSize, nBlocks);
108     }
109    void createAccessor(final Resource resource)
110     throws DatabaseException {
111         session.syncRequest(new WriteRequest() {
112             @Override
113             public void perform(WriteGraph graph) throws DatabaseException {
114                 graph.markUndoPoint();
115                 Layer0 b = Layer0.getInstance(graph);
116                 ArrayAccessor ra = graph.newLiteral(resource, b.Literal, Bindings.BYTE_ARRAY.type(), null);
117                 for (int i=0; i<N_BLOCKS; ++i) {
118                     try {
119                         for (int j=0; j<BLOCK_SIZE; ++j) {
120                             if (DEBUG && VERBOSE)
121                                 System.out.println("DEBUG: created value for index " + (i*BLOCK_SIZE+j) + "=" + (byte)j);
122                             ra.add(i*BLOCK_SIZE+j, Bindings.BYTE, (byte)j);
123                         }
124                     } catch (AccessorException e) {
125                         throw new DatabaseException("Failed to add block=" + i + ".");
126                     }
127                 }
128             }
129         });
130     }
131     void modifyAccessor(final Resource resource, final int inc, final boolean validate)
132     throws DatabaseException {
133         session.syncRequest(new WriteRequest() {
134             @Override
135             public void perform(WriteGraph graph) throws DatabaseException {
136                 graph.markUndoPoint();
137                 Layer0 l0 = Layer0.getInstance(graph);
138                 Resource literal = graph.getSingleObject(resource, l0.Literal);
139                 ArrayAccessor a = graph.getAccessor(literal);
140                 for (int i=0; i<N_BLOCKS; ++i) {
141                     for (int j=0; j<BLOCK_SIZE; ++j) {
142                         try {
143                             a.set(i*BLOCK_SIZE+j, Bindings.BYTE, (byte)(j+inc));
144                         } catch (AccessorException e) {
145                             throw new DatabaseException("Failed to add byte i=" + i + " j=" + j + ".");
146                         }
147                     }
148                 }
149                 if (!validate)
150                     return;
151                 try {
152                     if (a.size() != N_BLOCKS * BLOCK_SIZE)
153                         throw new DatabaseException("Failed to validate block size.");
154                     for (int i=0; i<N_BLOCKS; ++i) {
155                         for (int j=0; j<BLOCK_SIZE; ++j) {
156                             byte b = (Byte)a.get(i*BLOCK_SIZE + j, Bindings.BYTE);
157                             if (b != (byte)(j+inc))
158                                 throw new DatabaseException("Failed to validate modifications. i=" + i + " j)" + j);
159                         }
160                     }
161                 } catch (AccessorException e) {
162                     throw new DatabaseException("Failed to validate modifications.", e);
163                 }
164             }
165         });
166     }
167     void truncateAccessor(final Resource resource, final int inc, final int newBlocks)
168     throws DatabaseException {
169         assertTrue(N_BLOCKS > newBlocks);
170         session.syncRequest(new WriteRequest() {
171             @Override
172             public void perform(WriteGraph graph) throws DatabaseException {
173                 graph.markUndoPoint();
174                 Layer0 l0 = Layer0.getInstance(graph);
175                 Resource literal = graph.getSingleObject(resource, l0.Literal);
176                 ArrayAccessor a = graph.getAccessor(literal);
177                 for (int i=0; i<newBlocks; ++i) {
178                     for (int j=0; j<BLOCK_SIZE; ++j) {
179                         try {
180                             a.set(i*BLOCK_SIZE+j, Bindings.BYTE, (byte)(j+inc));
181                         } catch (AccessorException e) {
182                             throw new DatabaseException("Failed to add byte i=" + i + " j=" + j + ".");
183                         }
184                     }
185                 }
186                 int index = newBlocks * BLOCK_SIZE;
187                 int count = (N_BLOCKS - newBlocks) * BLOCK_SIZE;
188                 try {
189                     a.remove(index, count);
190                 } catch (AccessorException e) {
191                     throw new DatabaseException("Failed to truncate.", e);
192                 }
193             }
194         });
195     }
196     void validateAccessor(final Resource resource, final int inc, final int nBlocks)
197     throws DatabaseException {
198         session.syncRequest(new ReadRequest() {
199             @Override
200             public void run(ReadGraph graph) throws DatabaseException {
201                 Layer0 l0 = Layer0.getInstance(graph);
202                 Resource literal = graph.getSingleObject(resource, l0.Literal);
203                 ArrayAccessor a = graph.getAccessor(literal);
204                 final int T_BLOCKS = nBlocks < 0 ? N_BLOCKS : nBlocks;
205                 try {
206                     if (a.size() != T_BLOCKS * BLOCK_SIZE)
207                         throw new DatabaseException("Failed to validate block size.");
208                     for (int i=0; i<T_BLOCKS; ++i) {
209                         for (int j=0; j<BLOCK_SIZE; ++j) {
210                             byte b = (Byte)a.get(i*BLOCK_SIZE + j, Bindings.BYTE);
211                             if (b != (byte)(j+inc))
212                                 throw new DatabaseException("Failed to validate modified file contents. i=" + i + " j=" + j);
213                         }
214                     }
215                 } catch (AccessorException e) {
216                     throw new DatabaseException(e);
217                 }
218             }
219         });
220     }
221     void checkNoAccessor(final Resource resource)
222     throws DatabaseException {
223         session.syncRequest(new WriteRequest() {
224             @Override
225             public void perform(WriteGraph graph) throws DatabaseException {
226                 graph.markUndoPoint();
227                 Layer0 l0 = Layer0.getInstance(graph);
228                 Resource literal = graph.getPossibleObject(resource, l0.Literal);
229                 if (null == literal)
230                     return;
231                 if (!graph.hasValue(literal))
232                     return;
233                 try {
234                     graph.getAccessor(literal);
235                 } catch (DatabaseException e) {
236                     return; // no accessor
237                 }
238                 throw new DatabaseException("Literal=" + literal + " has accessor.");
239             }
240         });
241     }
242 }