Merge pull request #540 from mposolda/master

Update DB profiles. Fix ExportImportTest to not clash with other tests
This commit is contained in:
Stian Thorgersen 2014-07-21 14:18:04 +01:00
commit 9ff4947abd
3 changed files with 92 additions and 63 deletions

37
pom.xml
View file

@ -627,42 +627,5 @@
<module>distribution</module> <module>distribution</module>
</modules> </modules>
</profile> </profile>
<!-- MySQL -->
<profile>
<activation>
<property>
<name>hibernate.connection.driver_class</name>
<value>com.mysql.jdbc.Driver</value>
</property>
</activation>
<id>mysql</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</profile>
<!-- PostgreSQL -->
<profile>
<activation>
<property>
<name>hibernate.connection.driver_class</name>
<value>org.postgresql.Driver</value>
</property>
</activation>
<id>postgresql</id>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
</profile>
</profiles> </profiles>
</project> </project>

View file

@ -403,6 +403,12 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
@ -560,5 +566,42 @@
</build> </build>
</profile> </profile>
<!-- MySQL -->
<profile>
<activation>
<property>
<name>keycloak.connectionsJpa.driver</name>
<value>com.mysql.jdbc.Driver</value>
</property>
</activation>
<id>mysql</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</profile>
<!-- PostgreSQL -->
<profile>
<activation>
<property>
<name>keycloak.connectionsJpa.driver</name>
<value>org.postgresql.Driver</value>
</property>
</activation>
<id>postgresql</id>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
</dependencies>
</profile>
</profiles> </profiles>
</project> </project>

View file

@ -1,17 +1,21 @@
package org.keycloak.testsuite.exportimport; package org.keycloak.testsuite.exportimport;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.junit.Assert; import org.junit.Assert;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain; import org.junit.rules.RuleChain;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runners.MethodSorters;
import org.keycloak.Config; import org.keycloak.Config;
import org.keycloak.exportimport.ExportImportConfig; import org.keycloak.exportimport.ExportImportConfig;
import org.keycloak.exportimport.dir.DirExportProvider; import org.keycloak.exportimport.dir.DirExportProvider;
@ -33,15 +37,20 @@ import org.keycloak.testsuite.rule.KeycloakRule;
*/ */
public class ExportImportTest { public class ExportImportTest {
private static SystemPropertiesHelper propsHelper = new SystemPropertiesHelper();
private static final String JPA_CONNECTION_URL = "keycloak.connectionsJpa.url";
private static final String JPA_DB_SCHEMA = "keycloak.connectionsJpa.databaseSchema";
private static final String MONGO_CLEAR_ON_STARTUP = "keycloak.connectionsMongo.clearOnStartup";
// We want data to be persisted among server restarts // We want data to be persisted among server restarts
private static ExternalResource hibernateSetupRule = new ExternalResource() { private static ExternalResource persistenceSetupRule = new ExternalResource() {
private boolean setupDone = false; private boolean connectionURLSet = false;
@Override @Override
protected void before() throws Throwable { protected void before() throws Throwable {
if (System.getProperty("keycloak.connectionsJpa.url") == null) { if (System.getProperty(JPA_CONNECTION_URL) == null) {
String baseExportImportDir = getExportImportTestDirectory(); String baseExportImportDir = getExportImportTestDirectory();
File oldDBFile = new File(baseExportImportDir, "keycloakDB.h2.db"); File oldDBFile = new File(baseExportImportDir, "keycloakDB.h2.db");
@ -50,43 +59,33 @@ public class ExportImportTest {
} }
String dbDir = baseExportImportDir + "/keycloakDB"; String dbDir = baseExportImportDir + "/keycloakDB";
System.setProperty("keycloak.connectionsJpa.url", "jdbc:h2:file:" + dbDir + ";DB_CLOSE_DELAY=-1"); propsHelper.pushProperty(JPA_CONNECTION_URL, "jdbc:h2:file:" + dbDir + ";DB_CLOSE_DELAY=-1");
System.setProperty("keycloak.connectionsJpa.databaseSchema", "update"); connectionURLSet = true;
setupDone = true;
} }
propsHelper.pushProperty(JPA_DB_SCHEMA, "create");
} }
@Override @Override
protected void after() { protected void after() {
if (setupDone) { if (connectionURLSet) {
Properties sysProps = System.getProperties(); propsHelper.pullProperty(JPA_CONNECTION_URL);
sysProps.remove("keycloak.connectionsJpa.url");
sysProps.remove("keycloak.connectionsJpa.databaseSchema");
} }
} }
}; };
// We want data to be persisted among server restarts private static ExternalResource outerPersistenceSetupRule = new ExternalResource() {
private static ExternalResource mongoRule = new ExternalResource() {
private static final String MONGO_CLEAR_ON_STARTUP_PROP_NAME = "keycloak.connectionsMongo.clearOnStartup";
private String previousMongoClearOnStartup;
@Override @Override
protected void before() throws Throwable { protected void before() throws Throwable {
previousMongoClearOnStartup = System.getProperty(MONGO_CLEAR_ON_STARTUP_PROP_NAME); System.setProperty(JPA_DB_SCHEMA, "update");
System.setProperty(MONGO_CLEAR_ON_STARTUP_PROP_NAME, "false"); propsHelper.pushProperty(MONGO_CLEAR_ON_STARTUP, "false");
} }
@Override @Override
protected void after() { protected void after() {
if (previousMongoClearOnStartup != null) { propsHelper.pullProperty(JPA_DB_SCHEMA);
System.setProperty(MONGO_CLEAR_ON_STARTUP_PROP_NAME, previousMongoClearOnStartup); propsHelper.pullProperty(MONGO_CLEAR_ON_STARTUP);
} else {
System.getProperties().remove(MONGO_CLEAR_ON_STARTUP_PROP_NAME);
}
} }
}; };
private static KeycloakRule keycloakRule = new KeycloakRule( new KeycloakRule.KeycloakSetup() { private static KeycloakRule keycloakRule = new KeycloakRule( new KeycloakRule.KeycloakSetup() {
@ -124,9 +123,9 @@ public class ExportImportTest {
@ClassRule @ClassRule
public static TestRule chain = RuleChain public static TestRule chain = RuleChain
.outerRule(hibernateSetupRule) .outerRule(persistenceSetupRule)
.around(mongoRule) .around(keycloakRule)
.around(keycloakRule); .around(outerPersistenceSetupRule);
@Test @Test
public void testDirFullExportImport() throws Throwable { public void testDirFullExportImport() throws Throwable {
@ -357,4 +356,28 @@ public class ExportImportTest {
return absolutePath; return absolutePath;
} }
private static class SystemPropertiesHelper {
private Map<String,String> previousValues = new HashMap<String,String>();
private void pushProperty(String name, String value) {
String currentValue = System.getProperty(name);
if (currentValue != null) {
previousValues.put(name, value);
}
System.setProperty(name, value);
}
private void pullProperty(String name) {
String prevValue = previousValues.get(name);
if (prevValue == null) {
System.getProperties().remove(name);
} else {
System.setProperty(name, prevValue);
}
}
}
} }