--- /dev/null
+/*******************************************************************************
+ * 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 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 SetValueTest extends SimpleBase {
+ private static int ARRAY_SIZE;
+ private final String value = "SetValue" + getRandomString();
+ public SetValueTest() {
+ long max = Runtime.getRuntime().maxMemory() / 4;
+ if (max>Integer.MAX_VALUE)
+ max = Integer.MAX_VALUE;
+ ARRAY_SIZE = Math.min((int)max, 1<<20);
+ }
+ @Test
+ public void testSetValue() throws DatabaseException {
+ final Session s = getSession();
+ setValue(s);
+ checkValue(s);
+ final Session s2 = Tests.getTestHandler().getSession();
+ try {
+ checkValue(s2);
+ } finally {
+ LifecycleSupport ls = s2.getService(LifecycleSupport.class);
+ ls.close();
+ }
+ }
+ private void setValue(Session s) throws DatabaseException {
+ s.syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(g);
+ byte data[] = new byte[ARRAY_SIZE];
+ for (int i = 0; i < data.length; i++) {
+ data[i] = (byte)(i);
+ }
+ Resource r = g.newResource();
+ g.claim(r, b.InstanceOf, null, b.Type);
+ g.claimLiteral(r, b.HasName, value);
+ g.claim(rl, b.ConsistsOf, r);
+ Resource rv = g.newResource();
+ g.claim(rv, b.InstanceOf, b.ByteArray);
+ g.claimValue(rv, data);
+ checkValue(g, rv);
+ g.claim(r, b.HasProperty, rv);
+ }
+ });
+ }
+ private void checkValue(ReadGraph g, Resource rv)
+ throws DatabaseException {
+ byte[] data = g.getValue(rv);
+ if (data.length != ARRAY_SIZE)
+ 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.");
+ }
+ }
+ private void checkValue(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);
+ for (Resource rv : g.getObjects(r, l0.HasProperty)) {
+ if (g.isInstanceOf(rv, l0.ByteArray)) {
+ byte[] data = g.getValue(rv);
+ if (data.length != ARRAY_SIZE)
+ 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.");
+ }
+ return rv;
+ }
+ }
+ fail("Value not found.");
+ return null;
+ }
+ });
+ }
+}
\ No newline at end of file