]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/write/claimValue/SetValueTest2.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 / SetValueTest2.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
16 import org.junit.Test;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.WriteRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.request.Read;
24 import org.simantics.db.service.LifecycleSupport;
25 import org.simantics.db.testing.base.SimpleBase;
26 import org.simantics.db.testing.common.TestBase;
27 import org.simantics.db.testing.common.Tests;
28 import org.simantics.layer0.Layer0;
29 public class SetValueTest2 extends SimpleBase {
30     private static int NV = 258;
31     private static int VS = (1<<14)-1;
32     private final String value = "SetValue2" + getRandomString();
33     private final byte[] data;
34     public SetValueTest2() {
35         data = new byte[VS];
36         for (int i = 0; i < data.length; i++) {
37             data[i] = (byte)(i);
38         }
39     }
40         @Test
41     public void testSetValue2() throws DatabaseException {
42         final Session s = getSession();
43         setValues(s);
44         checkValues(s);
45         final Session s2 = Tests.getTestHandler().getSession();
46         try  {
47             checkValues(s2);
48         } finally {
49             LifecycleSupport ls = s2.getService(LifecycleSupport.class);
50             ls.close();
51         }
52     }
53     private void setValues(Session s) throws DatabaseException {
54         s.syncRequest(new WriteRequest() {
55             @Override
56             public void perform(WriteGraph g) throws DatabaseException {
57                 Layer0 b = Layer0.getInstance(g);
58                 Resource r = g.newResource();
59                 g.claim(r, b.InstanceOf, null, b.Library);
60                 g.claimLiteral(r, b.HasName, value);
61                 g.claim(rl, b.ConsistsOf, r);
62                 g.flushCluster();
63                 for (int i=0; i<NV; ++i) {
64                     Resource rv = g.newResource();
65                     g.claim(rv, b.InstanceOf, b.ByteArray);
66                     g.claimValue(rv, data);
67                     g.claim(r, b.HasProperty, rv);
68                 }
69             }
70         });
71     }
72     private void checkValues(Session s) throws DatabaseException {
73         s.syncRequest(new Read<Resource>() {
74             @Override
75             public Resource perform(ReadGraph g) throws DatabaseException {
76                 Layer0 l0 = Layer0.getInstance(g);
77                 String uri = TestBase.ROOT_LIBRARY_URI + "/" + value;
78                 Resource r = g.getResource(uri);
79                 assertTrue(null != r);
80                 Collection<Resource> resources = g.getObjects(r, l0.HasProperty);
81                 int count = 0;
82                 for (Resource rv : resources) {
83                     if (g.isInstanceOf(rv, l0.ByteArray)) {
84                         byte[] data = g.getValue(rv);
85                         if (data.length != VS)
86                             fail("Value lenght does not match.");
87                         for (int i = 0; i < data.length; i++) {
88                             if (data[i] != (byte)i)
89                                 fail("Value content does not match.");
90                         }
91                         ++count;
92                     }
93                 }
94                 if (count != NV)
95                     fail("Value count does not match. count=" + count);
96                 return null;
97             }
98         });
99     }
100 }