X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fwrite%2FclaimValue%2FSetValueTest.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fwrite%2FclaimValue%2FSetValueTest.java;h=a5972776f3e5f292d2a62cef9f0b52b47841e19f;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest.java new file mode 100644 index 000000000..a5972776f --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * 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() { + @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