/******************************************************************************* * 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 java.util.UUID; import org.junit.Test; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.testing.base.ExistingDatabaseTest; import org.simantics.db.testing.common.WriteQuery; import org.simantics.layer0.Layer0; /** * Creates large data array and stores it into the database. * * @author Marko Luukkainen * */ public class DataBlobTest extends ExistingDatabaseTest { private static int ARRAY_SIZE = 1*1024*1024; // 60*1024*1024; @Test public void testDataBlob() throws Exception{ final Resource rootLib = getSession().getRootLibrary(); final String random = UUID.randomUUID().toString(); final String newInstanceName = "New Instance" + random; if (DEBUG) System.out.println("Test start"); getSession().syncRequest(new WriteQuery(this) { @Override public void run(WriteGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); byte data[] = new byte[ARRAY_SIZE]; for (int i = 0; i < data.length; i++) { data[i] = (byte)(i % 2); } Resource instance = g.newResource(); g.claim(instance, b.InstanceOf, null, b.Type); g.claimLiteral(instance, b.HasName, newInstanceName); g.claim(rootLib, b.ConsistsOf, instance); Resource prop = g.newResource(); g.claim(prop, b.InstanceOf, b.ByteArray); g.claimValue(prop, data); // Link it to the thing g.claim(instance, b.HasProperty, prop); } }); checkException(); if (DEBUG) System.out.println("Reading stage"); getSession().syncRequest(new TestReadRequest() { @Override public void run(ReadGraph g) throws Throwable { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(rootLib, b.ConsistsOf); for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, b.HasName); if (null != value && value.equals(newInstanceName)) { for (Resource p : g.getObjects(r, b.HasProperty)) if (g.isInstanceOf(p, b.ByteArray)) { byte[] data = g.getValue(p); if (data.length != ARRAY_SIZE) fail("Data blob lenght does not match."); for (int i = 0; i < data.length; i++) { if (data[i] != (byte)(i % 2)) fail("Data blob content does not match."); } return; } } } fail("Data blob not found."); } }); if (DEBUG) System.out.println("test done"); checkException(); } }