[New] added realm testing with different connections
This commit is contained in:
parent
be033f3e35
commit
ecfb2c8e00
|
@ -139,10 +139,12 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
|
||||||
"Strolch Transaction for realm " + getRealmName() + " failed due to " + e.getMessage(), e); //$NON-NLS-1$
|
"Strolch Transaction for realm " + getRealmName() + " failed due to " + e.getMessage(), e); //$NON-NLS-1$
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
if (this.connection != null) {
|
||||||
this.connection.close();
|
try {
|
||||||
} catch (Exception e) {
|
this.connection.close();
|
||||||
logger.error("Failed to close connection due to " + e.getMessage(), e); //$NON-NLS-1$
|
} catch (Exception e) {
|
||||||
|
logger.error("Failed to close connection due to " + e.getMessage(), e); //$NON-NLS-1$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,9 @@ public class CachedDaoTest extends AbstractModelTest {
|
||||||
public static final String DB_STORE_PATH_DIR = "dbStore"; //$NON-NLS-1$
|
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$
|
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$
|
public static final String DB_URL = "jdbc:postgresql://localhost/testdb"; //$NON-NLS-1$
|
||||||
private static final String DB_USERNAME = "testuser"; //$NON-NLS-1$
|
public static final String DB_USERNAME = "testuser"; //$NON-NLS-1$
|
||||||
private static final String DB_PASSWORD = "test"; //$NON-NLS-1$
|
public static final String DB_PASSWORD = "test"; //$NON-NLS-1$
|
||||||
|
|
||||||
protected static RuntimeMock runtimeMock;
|
protected static RuntimeMock runtimeMock;
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public class CachedDaoTest extends AbstractModelTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws SQLException {
|
public static void beforeClass() throws SQLException {
|
||||||
|
|
||||||
dropSchema();
|
dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD);
|
||||||
|
|
||||||
File rootPath = new File(RUNTIME_PATH);
|
File rootPath = new File(RUNTIME_PATH);
|
||||||
File configSrc = new File(CONFIG_SRC);
|
File configSrc = new File(CONFIG_SRC);
|
||||||
|
@ -57,10 +57,10 @@ public class CachedDaoTest extends AbstractModelTest {
|
||||||
runtimeMock.startContainer();
|
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 dbVersion = DbSchemaVersionCheck.getExpectedDbVersion();
|
||||||
String sql = DbSchemaVersionCheck.getSql(dbVersion, "drop"); //$NON-NLS-1$
|
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();
|
connection.prepareStatement(sql).execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,7 @@ import li.strolch.testbase.runtime.RuntimeMock;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
@ -60,7 +61,7 @@ public class ObserverUpdateTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws SQLException {
|
public static void beforeClass() throws SQLException {
|
||||||
|
|
||||||
CachedDaoTest.dropSchema();
|
dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD);
|
||||||
|
|
||||||
File rootPath = new File(RUNTIME_PATH);
|
File rootPath = new File(RUNTIME_PATH);
|
||||||
File configSrc = new File(CONFIG_SRC);
|
File configSrc = new File(CONFIG_SRC);
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,6 +15,11 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.persistence.postgresql.dao.test;
|
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.io.File;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
@ -40,7 +45,7 @@ public class TransactionalDaoTest extends AbstractModelTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws SQLException {
|
public static void beforeClass() throws SQLException {
|
||||||
|
|
||||||
CachedDaoTest.dropSchema();
|
dropSchema(DB_URL, DB_USERNAME, DB_PASSWORD);
|
||||||
|
|
||||||
File rootPath = new File(RUNTIME_PATH);
|
File rootPath = new File(RUNTIME_PATH);
|
||||||
File configSrc = new File(CONFIG_SRC);
|
File configSrc = new File(CONFIG_SRC);
|
||||||
|
|
|
@ -25,12 +25,12 @@
|
||||||
<allowSchemaCreation>true</allowSchemaCreation>
|
<allowSchemaCreation>true</allowSchemaCreation>
|
||||||
<allowSchemaDrop>true</allowSchemaDrop>
|
<allowSchemaDrop>true</allowSchemaDrop>
|
||||||
|
|
||||||
<db.url.first>jdbc:postgresql://localhost/testdb</db.url.first>
|
<db.url.first>jdbc:postgresql://localhost/testdb1</db.url.first>
|
||||||
<db.username.first>testuser</db.username.first>
|
<db.username.first>testuser1</db.username.first>
|
||||||
<db.password.first>test</db.password.first>
|
<db.password.first>test</db.password.first>
|
||||||
|
|
||||||
<db.url.second>jdbc:postgresql://localhost/testdb</db.url.second>
|
<db.url.second>jdbc:postgresql://localhost/testdb2</db.url.second>
|
||||||
<db.username.second>testuser</db.username.second>
|
<db.username.second>testuser2</db.username.second>
|
||||||
<db.password.second>test</db.password.second>
|
<db.password.second>test</db.password.second>
|
||||||
</Properties>
|
</Properties>
|
||||||
</Component>
|
</Component>
|
||||||
|
|
Loading…
Reference in New Issue