From 30ddf3cfbf8ff1106d5268e553a93c280d3aee64 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Sat, 19 Oct 2013 17:45:33 +0200 Subject: [PATCH] [New] implemented a basic test for multiple realms (mandates) --- .../ch/eitchnet/xmlpers/test/RealmTest.java | 101 +++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/src/test/java/ch/eitchnet/xmlpers/test/RealmTest.java b/src/test/java/ch/eitchnet/xmlpers/test/RealmTest.java index 755a09c56..aeef634ca 100644 --- a/src/test/java/ch/eitchnet/xmlpers/test/RealmTest.java +++ b/src/test/java/ch/eitchnet/xmlpers/test/RealmTest.java @@ -1,11 +1,110 @@ package ch.eitchnet.xmlpers.test; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.Properties; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import ch.eitchnet.xmlpers.api.IoMode; +import ch.eitchnet.xmlpers.api.PersistenceConstants; +import ch.eitchnet.xmlpers.api.PersistenceTransaction; +import ch.eitchnet.xmlpers.objref.ObjectRef; +import ch.eitchnet.xmlpers.test.impl.TestConstants; +import ch.eitchnet.xmlpers.test.model.ModelBuilder; +import ch.eitchnet.xmlpers.test.model.Resource; + @SuppressWarnings("nls") public class RealmTest extends AbstractPersistenceTest { - protected static final String BASE_PATH = "target/db/RealmTest"; + private static final String REALM_2 = "Realm2"; + private static final String REALM_1 = "Realm1"; + protected static final String BASE_PATH = "target/db/RealmTest/"; + @BeforeClass public static void beforeClass() { cleanPath(BASE_PATH); } + + @Before + public void before() { + Properties properties = new Properties(); + properties.setProperty(PersistenceConstants.PROP_BASEPATH, BASE_PATH + IoMode.DOM.name()); + setup(properties); + } + + @Test + public void shouldNotFindObjInBothRealms() { + + // object details + String objType = TestConstants.TYPE_RES; + String type = ModelBuilder.RES_TYPE; + String name = ModelBuilder.RES_NAME; + String id = "shouldNotFindObjInBothRealms"; + + // create in first realm + try (PersistenceTransaction txRealm1 = this.persistenceManager.openTx(REALM_1);) { + Resource resource1 = ModelBuilder.createResource(id, name, type); + txRealm1.getObjectDao().add(resource1); + } + + // find in first realm + try (PersistenceTransaction txRealm1 = this.persistenceManager.openTx(REALM_1);) { + ObjectRef objectRef = txRealm1.getObjectRefCache().getIdOfSubTypeRef(objType, type, id); + Resource resource = txRealm1.getObjectDao().queryById(objectRef); + assertNotNull("Resource was not found in first realm!", resource); + } + + // fail to find in second realm + try (PersistenceTransaction txRealm2 = this.persistenceManager.openTx(REALM_2);) { + ObjectRef objectRef = txRealm2.getObjectRefCache().getIdOfSubTypeRef(objType, type, id); + Resource resource = txRealm2.getObjectDao().queryById(objectRef); + assertNull("Resource was not created in second realm, thus not expected to be found there!", resource); + } + } + + @Test + public void shouldNotDeleteObjInWrongRealm() { + + // object details + String objType = TestConstants.TYPE_RES; + String subType = ModelBuilder.RES_TYPE; + String name = ModelBuilder.RES_NAME; + String id = "shouldNotDeleteObjInWrongRealm"; + + // create in first realm + try (PersistenceTransaction txRealm1 = this.persistenceManager.openTx(REALM_1);) { + Resource resource1 = ModelBuilder.createResource(id, name, subType); + txRealm1.getObjectDao().add(resource1); + } + + // create in second realm + try (PersistenceTransaction txRealm2 = this.persistenceManager.openTx(REALM_2);) { + Resource resource1 = ModelBuilder.createResource(id, name, subType); + txRealm2.getObjectDao().add(resource1); + } + + // delete in second realm + try (PersistenceTransaction txRealm2 = this.persistenceManager.openTx(REALM_2);) { + ObjectRef objectRef = txRealm2.getObjectRefCache().getIdOfSubTypeRef(objType, subType, id); + txRealm2.getObjectDao().removeById(objectRef); + } + + // fail to find in second realm + try (PersistenceTransaction txRealm2 = this.persistenceManager.openTx(REALM_2);) { + ObjectRef objectRef = txRealm2.getObjectRefCache().getIdOfSubTypeRef(objType, subType, id); + Resource resource = txRealm2.getObjectDao().queryById(objectRef); + assertNull("Resource was not deleted from second realm, thus not expected to be found there!", resource); + } + + // find in first realm + try (PersistenceTransaction txRealm1 = this.persistenceManager.openTx(REALM_1);) { + ObjectRef objectRef = txRealm1.getObjectRefCache().getIdOfSubTypeRef(objType, subType, id); + Resource resource = txRealm1.getObjectDao().queryById(objectRef); + assertNotNull("Resource was not found in first realm!", resource); + } + } }