diff --git a/src/main/java/li/strolch/persistence/postgresql/PostgreSqlStrolchTransaction.java b/src/main/java/li/strolch/persistence/postgresql/PostgreSqlStrolchTransaction.java index 99e61830a..17b2739b1 100644 --- a/src/main/java/li/strolch/persistence/postgresql/PostgreSqlStrolchTransaction.java +++ b/src/main/java/li/strolch/persistence/postgresql/PostgreSqlStrolchTransaction.java @@ -139,10 +139,12 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction { "Strolch Transaction for realm " + getRealmName() + " failed due to " + e.getMessage(), e); //$NON-NLS-1$ } finally { - try { - this.connection.close(); - } catch (Exception e) { - logger.error("Failed to close connection due to " + e.getMessage(), e); //$NON-NLS-1$ + if (this.connection != null) { + try { + this.connection.close(); + } catch (Exception e) { + logger.error("Failed to close connection due to " + e.getMessage(), e); //$NON-NLS-1$ + } } } diff --git a/src/test/java/li/strolch/persistence/postgresql/dao/test/CachedDaoTest.java b/src/test/java/li/strolch/persistence/postgresql/dao/test/CachedDaoTest.java index d07f272b9..f689524ed 100644 --- a/src/test/java/li/strolch/persistence/postgresql/dao/test/CachedDaoTest.java +++ b/src/test/java/li/strolch/persistence/postgresql/dao/test/CachedDaoTest.java @@ -33,12 +33,12 @@ public class CachedDaoTest extends AbstractModelTest { public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$ public static final String CONFIG_SRC = "src/test/resources/cachedruntime"; //$NON-NLS-1$ - private static final String DB_URL = "jdbc:postgresql://localhost/testdb"; //$NON-NLS-1$ - private static final String DB_USERNAME = "testuser"; //$NON-NLS-1$ - private static final String DB_PASSWORD = "test"; //$NON-NLS-1$ + public static final String DB_URL = "jdbc:postgresql://localhost/testdb"; //$NON-NLS-1$ + public static final String DB_USERNAME = "testuser"; //$NON-NLS-1$ + public static final String DB_PASSWORD = "test"; //$NON-NLS-1$ protected static RuntimeMock runtimeMock; - + @Override protected RuntimeMock getRuntimeMock() { return runtimeMock; @@ -47,7 +47,7 @@ public class CachedDaoTest extends AbstractModelTest { @BeforeClass public static void beforeClass() throws SQLException { - dropSchema(); + dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD); File rootPath = new File(RUNTIME_PATH); File configSrc = new File(CONFIG_SRC); @@ -57,10 +57,10 @@ public class CachedDaoTest extends AbstractModelTest { runtimeMock.startContainer(); } - public static void dropSchema() throws SQLException { + public static void dropSchema(String dbUrl, String dbUsername, String dbPassword) throws SQLException { String dbVersion = DbSchemaVersionCheck.getExpectedDbVersion(); String sql = DbSchemaVersionCheck.getSql(dbVersion, "drop"); //$NON-NLS-1$ - try (Connection connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)) { + try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword)) { connection.prepareStatement(sql).execute(); } } diff --git a/src/test/java/li/strolch/persistence/postgresql/dao/test/ObserverUpdateTest.java b/src/test/java/li/strolch/persistence/postgresql/dao/test/ObserverUpdateTest.java index a2d204681..6b26990a7 100644 --- a/src/test/java/li/strolch/persistence/postgresql/dao/test/ObserverUpdateTest.java +++ b/src/test/java/li/strolch/persistence/postgresql/dao/test/ObserverUpdateTest.java @@ -41,6 +41,7 @@ import li.strolch.testbase.runtime.RuntimeMock; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.*; /** * @author Robert von Burg @@ -60,7 +61,7 @@ public class ObserverUpdateTest { @BeforeClass public static void beforeClass() throws SQLException { - CachedDaoTest.dropSchema(); + dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD); File rootPath = new File(RUNTIME_PATH); File configSrc = new File(CONFIG_SRC); diff --git a/src/test/java/li/strolch/persistence/postgresql/dao/test/RealmTest.java b/src/test/java/li/strolch/persistence/postgresql/dao/test/RealmTest.java new file mode 100644 index 000000000..682f25dbe --- /dev/null +++ b/src/test/java/li/strolch/persistence/postgresql/dao/test/RealmTest.java @@ -0,0 +1,118 @@ +/* + * Copyright 2013 Robert von Burg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.persistence.postgresql.dao.test; + +import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.dropSchema; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.File; +import java.sql.SQLException; + +import li.strolch.agent.impl.DataStoreMode; +import li.strolch.agent.impl.StrolchRealm; +import li.strolch.model.ModelGenerator; +import li.strolch.model.Resource; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.testbase.runtime.AbstractModelTest; +import li.strolch.testbase.runtime.RuntimeMock; + +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RealmTest extends AbstractModelTest { + + public static final String RUNTIME_PATH = "target/realmTest/"; //$NON-NLS-1$ + public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$ + public static final String CONFIG_SRC = "src/test/resources/realmTest"; //$NON-NLS-1$ + + protected static RuntimeMock runtimeMock; + + @Override + protected RuntimeMock getRuntimeMock() { + return runtimeMock; + } + + @BeforeClass + public static void beforeClass() throws SQLException { + + dropSchema("jdbc:postgresql://localhost/testdb1", "testuser1", "test"); + dropSchema("jdbc:postgresql://localhost/testdb2", "testuser2", "test"); + + File rootPath = new File(RUNTIME_PATH); + File configSrc = new File(CONFIG_SRC); + runtimeMock = new RuntimeMock(); + runtimeMock.mockRuntime(rootPath, configSrc); + new File(rootPath, DB_STORE_PATH_DIR).mkdir(); + runtimeMock.startContainer(); + } + + @Before + public void before() { + this.realmName = "second"; + } + + @Test + public void testDifferentRealms() { + + String expectedId1 = "@realmTestId1"; + String expectedId2 = "@realmTestId2"; + String type = "Bla"; + + { + StrolchRealm firstRealm = runtimeMock.getRealm("first"); + assertEquals(DataStoreMode.TRANSACTIONAL, firstRealm.getMode()); + Resource expectedRes1 = ModelGenerator.createResource(expectedId1, "Bla bla", type); + try (StrolchTransaction tx = firstRealm.openTx()) { + tx.getResourceMap().add(tx, expectedRes1); + } + + try (StrolchTransaction tx = firstRealm.openTx()) { + Resource res = tx.getResourceMap().getBy(tx, type, expectedId1); + assertEquals("Should find object previously added in same realm!", expectedRes1, res); + } + } + + { + StrolchRealm secondRealm = runtimeMock.getRealm("second"); + assertEquals(DataStoreMode.TRANSACTIONAL, secondRealm.getMode()); + Resource expectedRes2 = ModelGenerator.createResource(expectedId2, "Bla bla", type); + try (StrolchTransaction tx = secondRealm.openTx()) { + tx.getResourceMap().add(tx, expectedRes2); + } + + try (StrolchTransaction tx = secondRealm.openTx()) { + Resource res = tx.getResourceMap().getBy(tx, type, expectedId2); + assertEquals("Should find object previously added in same realm!", expectedRes2, res); + } + } + + { + StrolchRealm secondRealm = runtimeMock.getRealm("second"); + try (StrolchTransaction tx = secondRealm.openTx()) { + Resource res = tx.getResourceMap().getBy(tx, type, expectedId1); + assertNull("Should not find object added in differenct realm!", res); + } + } + } + + @AfterClass + public static void afterClass() { + runtimeMock.destroyRuntime(); + } +} diff --git a/src/test/java/li/strolch/persistence/postgresql/dao/test/TransactionalDaoTest.java b/src/test/java/li/strolch/persistence/postgresql/dao/test/TransactionalDaoTest.java index fe788760d..331b50e40 100644 --- a/src/test/java/li/strolch/persistence/postgresql/dao/test/TransactionalDaoTest.java +++ b/src/test/java/li/strolch/persistence/postgresql/dao/test/TransactionalDaoTest.java @@ -15,6 +15,11 @@ */ package li.strolch.persistence.postgresql.dao.test; +import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_PASSWORD; +import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_URL; +import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.DB_USERNAME; +import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.dropSchema; + import java.io.File; import java.sql.SQLException; @@ -40,7 +45,7 @@ public class TransactionalDaoTest extends AbstractModelTest { @BeforeClass public static void beforeClass() throws SQLException { - CachedDaoTest.dropSchema(); + dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD); File rootPath = new File(RUNTIME_PATH); File configSrc = new File(CONFIG_SRC); diff --git a/src/test/resources/realmtest/config/StrolchConfiguration.xml b/src/test/resources/realmtest/config/StrolchConfiguration.xml index 6dd922435..617535181 100644 --- a/src/test/resources/realmtest/config/StrolchConfiguration.xml +++ b/src/test/resources/realmtest/config/StrolchConfiguration.xml @@ -25,12 +25,12 @@ true true - jdbc:postgresql://localhost/testdb - testuser + jdbc:postgresql://localhost/testdb1 + testuser1 test - jdbc:postgresql://localhost/testdb - testuser + jdbc:postgresql://localhost/testdb2 + testuser2 test