]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3176Test1.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 / Issue3176Test1.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3176Test1.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/regression/bugs/Issue3176Test1.java
new file mode 100644 (file)
index 0000000..ecf0c77
--- /dev/null
@@ -0,0 +1,318 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.regression.bugs;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ExternalValueSupport;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ExternalValueException;
+import org.simantics.db.service.UndoRedoSupport;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.testing.impl.Configuration;
+import org.simantics.layer0.Layer0;
+
+public class Issue3176Test1 extends ExistingDatabaseTest {
+    static final boolean DEBUG = false;
+    @Test
+    public void testRaw()
+    throws DatabaseException {
+        try{
+            int nBlocks = Configuration.get().i3176BlockCount;
+            int[] blockSize = { 1024, 0xFFFF, 1<<20 };
+            final int LENGTH = blockSize.length; 
+            for (int i=0; i<LENGTH; ++i) {
+              testRaw1(blockSize[i], nBlocks);
+              testRaw2(blockSize[i], nBlocks);
+              testRaw3(blockSize[i], nBlocks);
+            }
+            int nBlocksBig = Configuration.get().i3176BigBlockCount;
+            testRaw4(blockSize[blockSize.length - 1], nBlocksBig);
+        } catch (DatabaseException e) {
+            fail("Test failed with exception.", e);
+        }
+    }
+    public void testRaw1(int blockSize, int nBlocks)
+    throws DatabaseException {
+        RawTest test = new RawTest(getSession(), DEBUG, blockSize, nBlocks);
+        Resource r = test.createResource();
+        test.checkNoRaw(r);
+        test.createRaw(r, 0, -1);
+        test.checkLiteralValue(r);
+        test.validateRaw(r, 0, -1);
+        test.modifyRaw(r, 1);
+        test.validateRaw(r, 1, -1);
+        test.undoModi(r, 1);
+        test.validateRaw(r, 0, -1);
+        test.removeRaw(r);
+        test.checkNoRaw(r);
+    }
+    public void testRaw2(int blockSize, int nBlocks)
+    throws DatabaseException {
+        final RawTest test = new RawTest(getSession(), DEBUG, blockSize, nBlocks);
+        Resource r = test.createResource();
+        test.createRaw(r, 0, -1);
+        test.validateRaw(r, 0, -1);
+        for (int i=1; i<10; ++i) {
+            test.modifyRaw(r, i);
+            test.validateRaw(r, i, -1);
+        }
+        test.undoModi(r, 9);
+        test.validateRaw(r, 0, -1);
+        test.removeRaw(r);
+        test.checkNoRaw(r);
+    }
+    public void testRaw3(int blockSize, int nBlocks)
+    throws DatabaseException {
+        final RawTest test = new RawTest(getSession(), DEBUG, blockSize, nBlocks);
+        Resource r = test.createResource();
+        test.createRaw(r, 0, -1);
+        test.validateRaw(r, 0, -1);
+        test.truncateRaw(r, 1);
+        test.validateRaw(r, 0, 1);
+    }
+    public void testRaw4(int blockSize, int nBlocks)
+    throws DatabaseException {
+        final RawTest test = new RawTest(getSession(), DEBUG, blockSize, nBlocks);
+        Resource r = test.createResource();
+        test.createRaw(r, 0, -1);
+        test.validateRaw(r, 0, -1);
+        test.modifyRaw(r, 1);
+        test.validateRaw(r, 1, -1);
+        test.truncateRaw(r, 1);
+        test.validateRaw(r, 1, 1);
+    }
+}
+class Issue3176Test {
+    protected final boolean VERBOSE;
+    protected final long LIMIT = 10 * 1000 * 1000;
+    protected final Session session;
+    protected final boolean DEBUG;
+    protected final int BLOCK_SIZE;
+    protected final int N_BLOCKS;
+    Issue3176Test(final Session session, boolean DEBUG, int blockSize, int nBlocks)
+    throws DatabaseException {
+        this.VERBOSE = true;
+        this.BLOCK_SIZE = blockSize;
+        this.N_BLOCKS = nBlocks;
+        this.DEBUG = DEBUG;
+        this.session = session;
+    }
+    protected Resource createResource()
+    throws DatabaseException {
+        return session.syncRequest(new WriteResultRequest<Resource>() {
+            @Override
+            public Resource perform(WriteGraph g) throws DatabaseException {
+                g.markUndoPoint();
+                Layer0 b = Layer0.getInstance(g);
+                Resource resource = g.newResource();
+                g.claim(resource, b.InstanceOf, b.Entity);
+                return resource;
+            }
+        });
+    }
+    protected void checkLiteralValue(final Resource resource)
+    throws DatabaseException {
+        session.syncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getSingleObject(resource, l0.Literal);
+                // Note that graph.hasValue actually fetches the value from server.
+                // This is highly inefficient and thus do not use this kind of code!
+                if (!graph.hasValue(literal))
+                    throw new DatabaseException("Graph resource " + resource + " has no value.");
+            }
+        });
+    }
+    protected void undoModi(final Resource resource, int n)
+    throws DatabaseException {
+        final UndoRedoSupport support = session.getService(UndoRedoSupport.class);
+        support.undo(session, n);
+    }
+    protected byte[] createBlock(int size, int inc) {
+        final byte block[] = new byte[size];
+        for (int i = 0; i < block.length; ++i)
+            block[i] = (byte)(i+inc);
+        return block;
+    }
+    protected void removeLiteralValue(final Resource resource)
+    throws DatabaseException {
+        session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getSingleObject(resource, l0.Literal);
+                graph.denyValue(literal);
+            }
+        });
+    }
+}
+class RawTest extends Issue3176Test {
+    private final ExternalValueSupport rds;
+    RawTest(final Session session, boolean DEBUG, int blockSize, int nBlocks)
+    throws DatabaseException {
+        super(session, DEBUG, blockSize, nBlocks);
+        this.rds = session.getService(ExternalValueSupport.class);
+    }
+    void createRaw(final Resource resource, final int inc, int aBlocks)
+    throws DatabaseException {
+        if (aBlocks < 0)
+            aBlocks = N_BLOCKS;
+        final int T_BLOCKS = aBlocks;
+        session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 b = Layer0.getInstance(graph);
+                graph.claimLiteral(resource, b.Literal, new byte[0], Bindings.BYTE_ARRAY);
+                Resource literal = graph.getSingleObject(resource, b.Literal);
+                long count = 0;
+                for (int i=0, j=inc; i<T_BLOCKS; ++i, ++j) {
+                    byte[] block = createBlock(BLOCK_SIZE, j);
+                    try {
+                        if (DEBUG)
+                            System.out.println("Processing block=" + i);
+                        long offset = (long)i * block.length;
+                        rds.writeValue(graph, literal, offset, block.length, block);
+                        count += block.length;
+                        if (count > LIMIT) {
+                            count = 0;
+                            rds.wait4RequestsLess(1);
+                        }
+                    } catch (DatabaseException e) {
+                        throw new DatabaseException("Failed to add block=" + i);
+                    }
+                }
+            }
+        });
+    }
+    void modifyRaw(final Resource resource, final int inc)
+    throws DatabaseException {
+        session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getSingleObject(resource, l0.Literal);
+                long count = 0;
+                for (int i=0, j=inc; i<N_BLOCKS; ++i, ++j) {
+                    byte[] blockOut = createBlock(BLOCK_SIZE, j);
+                    try {
+                        if (DEBUG)
+                            System.out.println("Processing block=" + i);
+                        long offset = (long)i * blockOut.length;
+                        rds.writeValue(graph, literal, offset, blockOut.length, blockOut);
+                        count += blockOut.length;
+                        if (count > LIMIT) {
+                            count = 0;
+                            rds.wait4RequestsLess(1);
+                        }
+                        
+                    } catch (DatabaseException e) {
+                        throw new DatabaseException("Failed to add block=" + i);
+                    }
+                }
+            }
+        });
+    }
+    void truncateRaw(final Resource resource, final int nBlocks)
+    throws DatabaseException {
+        session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getSingleObject(resource, l0.Literal);
+                long len = (long)BLOCK_SIZE * nBlocks;
+                rds.writeValue(graph, literal, len, 0, new byte[0]);
+            }
+        });
+    }
+    void removeRaw(final Resource resource)
+    throws DatabaseException {
+        session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getSingleObject(resource, l0.Literal);
+                rds.removeValue(graph, literal);
+                try {
+                    rds.getValueSize(graph, literal);
+                } catch (ExternalValueException e) {
+                    return; // Assuming this is because there is no value.
+                }
+                throw new DatabaseException("Failed to delete value. Resource=" + literal);
+            }
+        });
+    }
+    void checkNoRaw(final Resource resource)
+    throws DatabaseException {
+        session.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph graph) throws DatabaseException {
+                graph.markUndoPoint();
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getPossibleObject(resource, l0.Literal);
+                if (null == literal)
+                    return;
+                try {
+                    rds.getValueSize(graph, literal);
+                } catch (DatabaseException e) {
+                    return; // Assuming this is because there is no value.
+                }
+                throw new DatabaseException("Resource=" + literal + " has raw value.");
+            }
+        });
+    }
+    void validateRaw(final Resource resource, final int inc, int aBlocks)
+    throws DatabaseException {
+        final int T_BLOCKS = aBlocks < 0 ? N_BLOCKS : aBlocks;
+        session.syncRequest(new ReadRequest() {
+            @Override
+            public void run(ReadGraph graph) throws DatabaseException {
+                Layer0 l0 = Layer0.getInstance(graph);
+                Resource literal = graph.getSingleObject(resource, l0.Literal);
+                long count = 0;
+                for (int i=0, j=inc; i<T_BLOCKS; ++i, ++j) {
+                    byte[] blockOut = createBlock(BLOCK_SIZE, j);
+                    long offset = (long)i * blockOut.length;
+                    byte[] blockIn = rds.readValue(graph, literal, offset, blockOut.length);
+                    assertTrue(blockIn.length == blockOut.length);
+                    assertTrue("Failed with block=" + i, Arrays.equals(blockOut, blockIn));
+                    if (DEBUG) {
+                        count += blockIn.length;
+                        if (count > LIMIT) {
+                            count = 0;
+                            if (DEBUG)
+                                System.out.println("Read block=" + i);
+                        }
+                    }
+                }
+                assertTrue(rds.getValueSize(graph, literal) == (long)T_BLOCKS * BLOCK_SIZE);
+            }
+        });
+    }
+}