X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fsupport%2FserialisationSupport%2FRandomAccessIdTestCommon.java;fp=tests%2Forg.simantics.db.tests%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftests%2Fapi%2Fsupport%2FserialisationSupport%2FRandomAccessIdTestCommon.java;h=d23da5e1218574a84224ccd7a0457de45bc6241b;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/serialisationSupport/RandomAccessIdTestCommon.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/serialisationSupport/RandomAccessIdTestCommon.java new file mode 100644 index 000000000..d23da5e12 --- /dev/null +++ b/tests/org.simantics.db.tests/src/org/simantics/db/tests/api/support/serialisationSupport/RandomAccessIdTestCommon.java @@ -0,0 +1,89 @@ +package org.simantics.db.tests.api.support.serialisationSupport; + +import java.util.Collection; + +import org.junit.Before; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.SerialisationSupport; +import org.simantics.db.testing.base.ExistingDatabaseTest; +import org.simantics.db.testing.common.WriteQuery; +import org.simantics.layer0.Layer0; + +public class RandomAccessIdTestCommon extends ExistingDatabaseTest { + protected long randomAccessID; + protected Resource resource; + protected final String newName = "New name " + getRandomString(); + protected Resource rl; + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + rl = getSession().getRootLibrary(); + } + protected void createResourceWithName() + throws DatabaseException { + // Create a new resource. + getSession().syncRequest(new WriteQuery(this) { + public void run(WriteGraph g) throws Throwable { + Layer0 b = Layer0.getInstance(g); + Resource instance = g.newResource(); + g.claim(instance, b.InstanceOf, b.Type); + g.claim(rl, b.ConsistsOf, instance); + g.claimLiteral(instance, b.HasName, newName); + }; + }); + checkException(); + } + protected void validateResourceWithName() + throws DatabaseException { + // Find the created resource. + getSession().syncRequest(new TestReadRequest() { + @Override + public void run(ReadGraph g) throws Throwable { + Layer0 l0 = Layer0.getInstance(g); + Collection resources = g.getObjects(rl, l0.ConsistsOf); + Resource found = null; + for (Resource r : resources) { + String name = g.getPossibleRelatedValue(r, l0.HasName); + if (null != name && name.equals(newName)) { + found = r; + break; + } + } + if (found == null) { + throw new DatabaseException("Cannot find created resource"); + } + resource = found; + } + }); + checkException(); + } + protected void createRandomAccessId() + throws DatabaseException { + // Create random access id for resource. + SerialisationSupport support = getSession().getService(SerialisationSupport.class); + randomAccessID = support.getRandomAccessId(resource); + if (randomAccessID == 0) + throw new DatabaseException("Could not create random access id"); + } + protected void validateRandomAccessId() + throws DatabaseException { + // test that random access id works and the given resource is the instance that we created + getSession().syncRequest(new TestReadRequest() { + @Override + public void run(ReadGraph g) throws Throwable { + Layer0 l0 = Layer0.getInstance(g); + SerialisationSupport support = getSession().getService(SerialisationSupport.class); + Resource res = support.getResource(randomAccessID); + String name = g.getRelatedValue(res, l0.HasName); + if (!name.equals(newName)) { + throw new Exception("Name is different that was written, returned resource is not a correct one!"); + } + } + }); + checkException(); + } +} \ No newline at end of file