1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.regression.bugs;
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;
30 public class Issue3176Test3 extends ExistingDatabaseTest {
33 public void testAccessor() {
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);
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);
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);
61 test.validateAccessor(r, 0, -1);
62 test.removeLiteralValue(r);
63 test.checkNoAccessor(r);
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);
73 for (int i=1; i<N+1; ++i) {
74 test.modifyAccessor(r, i, false);
75 test.validateAccessor(r, i, -1);
78 test.validateAccessor(r, 0, -1);
79 test.removeLiteralValue(r);
80 test.checkNoAccessor(r);
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);
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);
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);
109 void createAccessor(final Resource resource)
110 throws DatabaseException {
111 session.syncRequest(new WriteRequest() {
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) {
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);
124 } catch (AccessorException e) {
125 throw new DatabaseException("Failed to add block=" + i + ".");
131 void modifyAccessor(final Resource resource, final int inc, final boolean validate)
132 throws DatabaseException {
133 session.syncRequest(new WriteRequest() {
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) {
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 + ".");
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);
161 } catch (AccessorException e) {
162 throw new DatabaseException("Failed to validate modifications.", e);
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() {
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) {
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 + ".");
186 int index = newBlocks * BLOCK_SIZE;
187 int count = (N_BLOCKS - newBlocks) * BLOCK_SIZE;
189 a.remove(index, count);
190 } catch (AccessorException e) {
191 throw new DatabaseException("Failed to truncate.", e);
196 void validateAccessor(final Resource resource, final int inc, final int nBlocks)
197 throws DatabaseException {
198 session.syncRequest(new ReadRequest() {
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;
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);
215 } catch (AccessorException e) {
216 throw new DatabaseException(e);
221 void checkNoAccessor(final Resource resource)
222 throws DatabaseException {
223 session.syncRequest(new WriteRequest() {
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);
231 if (!graph.hasValue(literal))
234 graph.getAccessor(literal);
235 } catch (DatabaseException e) {
236 return; // no accessor
238 throw new DatabaseException("Literal=" + literal + " has accessor.");