]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest.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 / SetValueTest.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 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);
35     }
36         @Test
37     public void testSetValue() throws DatabaseException {
38         final Session s = getSession();
39         setValue(s);
40         checkValue(s);
41         final Session s2 = Tests.getTestHandler().getSession();
42         try {
43             checkValue(s2);
44         } finally {
45             LifecycleSupport ls = s2.getService(LifecycleSupport.class);
46             ls.close();
47         }
48     }
49     private void setValue(Session s) throws DatabaseException {
50         s.syncRequest(new WriteRequest() {
51             @Override
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++) {
56                     data[i] = (byte)(i);
57                 }
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);
65                 checkValue(g, rv);
66                 g.claim(r, b.HasProperty, rv);
67             }
68         });
69     }
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.");
78         }
79     }
80     private void checkValue(Session s) throws DatabaseException {
81         s.syncRequest(new Read<Resource>() {
82             @Override
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.");
96                         }
97                         return rv;
98                     }
99                 }
100                 fail("Value not found.");
101                 return null;
102             }
103         });
104     }
105 }