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 java.util.Collection;
15 import java.util.UUID;
17 import org.junit.Test;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.testing.base.ExistingDatabaseTest;
22 import org.simantics.db.testing.common.WriteQuery;
23 import org.simantics.layer0.Layer0;
26 * Creates large data array and stores it into the database.
28 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
31 public class DataBlobTest extends ExistingDatabaseTest {
33 private static int ARRAY_SIZE = 1*1024*1024; // 60*1024*1024;
36 public void testDataBlob() throws Exception{
37 final Resource rootLib = getSession().getRootLibrary();
38 final String random = UUID.randomUUID().toString();
39 final String newInstanceName = "New Instance" + random;
41 System.out.println("Test start");
42 getSession().syncRequest(new WriteQuery(this) {
44 public void run(WriteGraph g) throws Throwable {
45 Layer0 b = Layer0.getInstance(g);
46 byte data[] = new byte[ARRAY_SIZE];
47 for (int i = 0; i < data.length; i++) {
48 data[i] = (byte)(i % 2);
50 Resource instance = g.newResource();
51 g.claim(instance, b.InstanceOf, null, b.Type);
52 g.claimLiteral(instance, b.HasName, newInstanceName);
53 g.claim(rootLib, b.ConsistsOf, instance);
55 Resource prop = g.newResource();
56 g.claim(prop, b.InstanceOf, b.ByteArray);
57 g.claimValue(prop, data);
59 // Link it to the thing
60 g.claim(instance, b.HasProperty, prop);
68 System.out.println("Reading stage");
69 getSession().syncRequest(new TestReadRequest() {
71 public void run(ReadGraph g) throws Throwable {
72 Layer0 b = Layer0.getInstance(g);
73 Collection<Resource> resources = g.getObjects(rootLib, b.ConsistsOf);
74 for (Resource r : resources) {
75 String value = g.getPossibleRelatedValue(r, b.HasName);
76 if (null != value && value.equals(newInstanceName)) {
77 for (Resource p : g.getObjects(r, b.HasProperty))
78 if (g.isInstanceOf(p, b.ByteArray)) {
79 byte[] data = g.getValue(p);
80 if (data.length != ARRAY_SIZE)
81 fail("Data blob lenght does not match.");
82 for (int i = 0; i < data.length; i++) {
83 if (data[i] != (byte)(i % 2))
84 fail("Data blob content does not match.");
90 fail("Data blob not found.");
94 System.out.println("test done");