]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest2.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / claimValue / SetValueTest2.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest2.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest2.java
new file mode 100644 (file)
index 0000000..6fc81d2
--- /dev/null
@@ -0,0 +1,100 @@
+/*******************************************************************************
+ * 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.api.write.claimValue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+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.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.db.testing.base.SimpleBase;
+import org.simantics.db.testing.common.TestBase;
+import org.simantics.db.testing.common.Tests;
+import org.simantics.layer0.Layer0;
+public class SetValueTest2 extends SimpleBase {
+    private static int NV = 258;
+    private static int VS = (1<<14)-1;
+    private final String value = "SetValue2" + getRandomString();
+    private final byte[] data;
+    public SetValueTest2() {
+        data = new byte[VS];
+        for (int i = 0; i < data.length; i++) {
+            data[i] = (byte)(i);
+        }
+    }
+       @Test
+    public void testSetValue2() throws DatabaseException {
+        final Session s = getSession();
+        setValues(s);
+        checkValues(s);
+        final Session s2 = Tests.getTestHandler().getSession();
+        try  {
+            checkValues(s2);
+        } finally {
+            LifecycleSupport ls = s2.getService(LifecycleSupport.class);
+            ls.close();
+        }
+    }
+    private void setValues(Session s) throws DatabaseException {
+        s.syncRequest(new WriteRequest() {
+            @Override
+            public void perform(WriteGraph g) throws DatabaseException {
+                Layer0 b = Layer0.getInstance(g);
+                Resource r = g.newResource();
+                g.claim(r, b.InstanceOf, null, b.Library);
+                g.claimLiteral(r, b.HasName, value);
+                g.claim(rl, b.ConsistsOf, r);
+                g.flushCluster();
+                for (int i=0; i<NV; ++i) {
+                    Resource rv = g.newResource();
+                    g.claim(rv, b.InstanceOf, b.ByteArray);
+                    g.claimValue(rv, data);
+                    g.claim(r, b.HasProperty, rv);
+                }
+            }
+        });
+    }
+    private void checkValues(Session s) throws DatabaseException {
+        s.syncRequest(new Read<Resource>() {
+            @Override
+            public Resource perform(ReadGraph g) throws DatabaseException {
+                Layer0 l0 = Layer0.getInstance(g);
+                String uri = TestBase.ROOT_LIBRARY_URI + "/" + value;
+                Resource r = g.getResource(uri);
+                assertTrue(null != r);
+                Collection<Resource> resources = g.getObjects(r, l0.HasProperty);
+                int count = 0;
+                for (Resource rv : resources) {
+                    if (g.isInstanceOf(rv, l0.ByteArray)) {
+                        byte[] data = g.getValue(rv);
+                        if (data.length != VS)
+                            fail("Value lenght does not match.");
+                        for (int i = 0; i < data.length; i++) {
+                            if (data[i] != (byte)i)
+                                fail("Value content does not match.");
+                        }
+                        ++count;
+                    }
+                }
+                if (count != NV)
+                    fail("Value count does not match. count=" + count);
+                return null;
+            }
+        });
+    }
+}
\ No newline at end of file