1 package org.simantics.db.tests.api.support.serialisationSupport;
3 import java.util.Collection;
5 import org.junit.Before;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.service.SerialisationSupport;
11 import org.simantics.db.testing.base.ExistingDatabaseTest;
12 import org.simantics.db.testing.common.WriteQuery;
13 import org.simantics.layer0.Layer0;
15 public class RandomAccessIdTestCommon extends ExistingDatabaseTest {
16 protected long randomAccessID;
17 protected Resource resource;
18 protected final String newName = "New name " + getRandomString();
19 protected Resource rl;
22 public void setUp() throws Exception {
24 rl = getSession().getRootLibrary();
26 protected void createResourceWithName()
27 throws DatabaseException {
28 // Create a new resource.
29 getSession().syncRequest(new WriteQuery(this) {
30 public void run(WriteGraph g) throws Throwable {
31 Layer0 b = Layer0.getInstance(g);
32 Resource instance = g.newResource();
33 g.claim(instance, b.InstanceOf, b.Type);
34 g.claim(rl, b.ConsistsOf, instance);
35 g.claimLiteral(instance, b.HasName, newName);
40 protected void validateResourceWithName()
41 throws DatabaseException {
42 // Find the created resource.
43 getSession().syncRequest(new TestReadRequest() {
45 public void run(ReadGraph g) throws Throwable {
46 Layer0 l0 = Layer0.getInstance(g);
47 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
48 Resource found = null;
49 for (Resource r : resources) {
50 String name = g.getPossibleRelatedValue(r, l0.HasName);
51 if (null != name && name.equals(newName)) {
57 throw new DatabaseException("Cannot find created resource");
64 protected void createRandomAccessId()
65 throws DatabaseException {
66 // Create random access id for resource.
67 SerialisationSupport support = getSession().getService(SerialisationSupport.class);
68 randomAccessID = support.getRandomAccessId(resource);
69 if (randomAccessID == 0)
70 throw new DatabaseException("Could not create random access id");
72 protected void validateRandomAccessId()
73 throws DatabaseException {
74 // test that random access id works and the given resource is the instance that we created
75 getSession().syncRequest(new TestReadRequest() {
77 public void run(ReadGraph g) throws Throwable {
78 Layer0 l0 = Layer0.getInstance(g);
79 SerialisationSupport support = getSession().getService(SerialisationSupport.class);
80 Resource res = support.getResource(randomAccessID);
81 String name = g.getRelatedValue(res, l0.HasName);
82 if (!name.equals(newName)) {
83 throw new Exception("Name is different that was written, returned resource is not a correct one!");