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.api.write.claimValue;
14 import org.junit.Test;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.request.Read;
22 import org.simantics.db.service.LifecycleSupport;
23 import org.simantics.db.testing.base.SimpleBase;
24 import org.simantics.db.testing.common.TestBase;
25 import org.simantics.db.testing.common.Tests;
26 import org.simantics.layer0.Layer0;
27 public class SetValueTest extends SimpleBase {
28 private static int ARRAY_SIZE;
29 private final String value = "SetValue" + getRandomString();
30 public SetValueTest() {
31 long max = Runtime.getRuntime().maxMemory() / 4;
32 if (max>Integer.MAX_VALUE)
33 max = Integer.MAX_VALUE;
34 ARRAY_SIZE = Math.min((int)max, 1<<20);
37 public void testSetValue() throws DatabaseException {
38 final Session s = getSession();
41 final Session s2 = Tests.getTestHandler().getSession();
45 LifecycleSupport ls = s2.getService(LifecycleSupport.class);
49 private void setValue(Session s) throws DatabaseException {
50 s.syncRequest(new WriteRequest() {
52 public void perform(WriteGraph g) throws DatabaseException {
53 Layer0 b = Layer0.getInstance(g);
54 byte data[] = new byte[ARRAY_SIZE];
55 for (int i = 0; i < data.length; i++) {
58 Resource r = g.newResource();
59 g.claim(r, b.InstanceOf, null, b.Type);
60 g.claimLiteral(r, b.HasName, value);
61 g.claim(rl, b.ConsistsOf, r);
62 Resource rv = g.newResource();
63 g.claim(rv, b.InstanceOf, b.ByteArray);
64 g.claimValue(rv, data);
66 g.claim(r, b.HasProperty, rv);
70 private void checkValue(ReadGraph g, Resource rv)
71 throws DatabaseException {
72 byte[] data = g.getValue(rv);
73 if (data.length != ARRAY_SIZE)
74 fail("Value lenght does not match.");
75 for (int i = 0; i < data.length; i++) {
76 if (data[i] != (byte)i)
77 fail("Value content does not match.");
80 private void checkValue(Session s) throws DatabaseException {
81 s.syncRequest(new Read<Resource>() {
83 public Resource perform(ReadGraph g) throws DatabaseException {
84 Layer0 l0 = Layer0.getInstance(g);
85 String uri = TestBase.ROOT_LIBRARY_URI + "/" + value;
86 Resource r = g.getResource(uri);
87 assertTrue(null != r);
88 for (Resource rv : g.getObjects(r, l0.HasProperty)) {
89 if (g.isInstanceOf(rv, l0.ByteArray)) {
90 byte[] data = g.getValue(rv);
91 if (data.length != ARRAY_SIZE)
92 fail("Value lenght does not match.");
93 for (int i = 0; i < data.length; i++) {
94 if (data[i] != (byte)i)
95 fail("Value content does not match.");
100 fail("Value not found.");