]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/DataBlobTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / write / claimValue / DataBlobTest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.tests.api.write.claimValue;
13
14 import java.util.Collection;
15 import java.util.UUID;
16
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;
24
25 /**
26  * Creates large data array and stores it into the database.
27  * 
28  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29  *
30  */
31 public class DataBlobTest extends ExistingDatabaseTest {
32         
33         private static int ARRAY_SIZE = 1*1024*1024; // 60*1024*1024;
34         
35         @Test
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;
40         if (DEBUG)
41             System.out.println("Test start");
42                         getSession().syncRequest(new WriteQuery(this) {
43                                 @Override
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);
49                                         }
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);
54                                 
55                                         Resource prop = g.newResource();
56                                 g.claim(prop, b.InstanceOf, b.ByteArray);
57                                 g.claimValue(prop, data);
58                                 
59                                 // Link it to the thing
60                                 g.claim(instance, b.HasProperty, prop);
61                                 }
62                         });
63                 
64                 
65                 checkException();
66
67         if (DEBUG)
68             System.out.println("Reading stage");
69                 getSession().syncRequest(new TestReadRequest() {
70                         @Override
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.");
85                                                         }
86                                                         return;
87                                 }
88                     }
89                                 }
90                         fail("Data blob not found.");
91                         }
92                 });
93         if (DEBUG)
94             System.out.println("test done");
95                 checkException();
96         }
97
98 }