[New] added realm testing with different connections

This commit is contained in:
Robert von Burg 2014-02-19 00:53:00 +01:00
parent be033f3e35
commit ecfb2c8e00
6 changed files with 143 additions and 17 deletions

View File

@ -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$
}
}
}

View File

@ -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();
}
}

View File

@ -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 <eitch@eitchnet.ch>
@ -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);

View File

@ -0,0 +1,118 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* 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();
}
}

View File

@ -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);

View File

@ -25,12 +25,12 @@
<allowSchemaCreation>true</allowSchemaCreation>
<allowSchemaDrop>true</allowSchemaDrop>
<db.url.first>jdbc:postgresql://localhost/testdb</db.url.first>
<db.username.first>testuser</db.username.first>
<db.url.first>jdbc:postgresql://localhost/testdb1</db.url.first>
<db.username.first>testuser1</db.username.first>
<db.password.first>test</db.password.first>
<db.url.second>jdbc:postgresql://localhost/testdb</db.url.second>
<db.username.second>testuser</db.username.second>
<db.url.second>jdbc:postgresql://localhost/testdb2</db.url.second>
<db.username.second>testuser2</db.username.second>
<db.password.second>test</db.password.second>
</Properties>
</Component>