From 8714193c97f22c44db0c14d24fd75898b3422ade Mon Sep 17 00:00:00 2001 From: mposolda Date: Wed, 16 Jul 2014 18:36:59 +0200 Subject: [PATCH] Fix ExportImportTest to pass on windows --- .../exportimport/ExportImportConfig.java | 19 +++--- .../exportimport/ExportImportTest.java | 68 ++++++------------- .../testsuite/rule/AbstractKeycloakRule.java | 37 +++++++--- 3 files changed, 58 insertions(+), 66 deletions(-) diff --git a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/ExportImportConfig.java b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/ExportImportConfig.java index 8014c21f4e..a91cc3178a 100644 --- a/export-import/export-import-api/src/main/java/org/keycloak/exportimport/ExportImportConfig.java +++ b/export-import/export-import-api/src/main/java/org/keycloak/exportimport/ExportImportConfig.java @@ -5,30 +5,31 @@ package org.keycloak.exportimport; */ public class ExportImportConfig { - public static final String ACTION = "keycloak.migration.action"; + public static final String PREFIX = "keycloak.migration."; + public static final String ACTION = PREFIX + "action"; public static final String ACTION_EXPORT = "export"; public static final String ACTION_IMPORT = "import"; - public static final String PROVIDER = "keycloak.migration.provider"; + public static final String PROVIDER = PREFIX + "provider"; public static final String PROVIDER_DEFAULT = "zip"; // Name of the realm to export. If null, then full export will be triggered - public static final String REALM_NAME = "keycloak.migration.realmName"; + public static final String REALM_NAME = PREFIX + "realmName"; // used for "dir" provider - public static final String DIR = "keycloak.migration.dir"; + public static final String DIR = PREFIX + "dir"; // used for "zip" provider - public static final String ZIP_FILE = "keycloak.migration.zipFile"; - public static final String ZIP_PASSWORD = "keycloak.migration.zipPassword"; + public static final String ZIP_FILE = PREFIX + "zipFile"; + public static final String ZIP_PASSWORD = PREFIX + "zipPassword"; // used for "singleFile" provider - public static final String FILE = "keycloak.migration.file"; + public static final String FILE = PREFIX + "file"; // Number of users per file used in "dir" and "zip" providers. -1 means adding users to same file with realm. 0 means adding to separate file with unlimited page number - public static final String USERS_PER_FILE = "keycloak.migration.usersPerFile"; + public static final String USERS_PER_FILE = PREFIX + "usersPerFile"; public static final Integer DEFAULT_USERS_PER_FILE = 5000; // Strategy used during import data - public static final String STRATEGY = "keycloak.migration.strategy"; + public static final String STRATEGY = PREFIX + "strategy"; public static final Strategy DEFAULT_STRATEGY = Strategy.OVERWRITE_EXISTING; public static String getAction() { diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java index ede1808ebd..81b4c39c9e 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/exportimport/ExportImportTest.java @@ -1,7 +1,10 @@ package org.keycloak.testsuite.exportimport; import java.io.File; +import java.util.HashSet; import java.util.Properties; +import java.util.Set; +import java.util.regex.Matcher; import org.junit.Assert; import org.junit.ClassRule; @@ -101,50 +104,21 @@ public class ExportImportTest { }) { @Override protected void after() { - { - KeycloakSession session = server.getSessionFactory().create(); - session.getTransaction().begin(); - - try { - RealmManager manager = new RealmManager(session); - - RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm()); - RealmModel appRealm = manager.getRealm("test"); - boolean removed = session.users().removeUser(appRealm, "user1"); - removed = session.users().removeUser(appRealm, "user2"); - removed = session.users().removeUser(appRealm, "user3"); - removed = session.users().removeUser(adminstrationRealm, "admin2"); - - session.getTransaction().commit(); - } finally { - session.close(); - } - } - { - KeycloakSession session = server.getSessionFactory().create(); - session.getTransaction().begin(); - - try { - RealmManager manager = new RealmManager(session); - - RealmModel adminstrationRealm = manager.getRealm(Config.getAdminRealm()); - RealmModel appRealm = manager.getRealm("test"); - UserModel user1 = session.users().getUserByUsername("user1", appRealm); - UserModel user2= session.users().getUserByUsername("user2", appRealm); - UserModel user3 = session.users().getUserByUsername("user3", appRealm); - UserModel admin2 = session.users().getUserByUsername("admin2", adminstrationRealm); - Assert.assertNull(user1); - Assert.assertNull(user2); - Assert.assertNull(user3); - Assert.assertNull(admin2); - - session.getTransaction().commit(); - } finally { - session.close(); - } - } - super.after(); + + // Clear export/import properties after test + Properties systemProps = System.getProperties(); + Set propsToRemove = new HashSet(); + + for (Object key : systemProps.keySet()) { + if (key.toString().startsWith(ExportImportConfig.PREFIX)) { + propsToRemove.add(key.toString()); + } + } + + for (String propToRemove : propsToRemove) { + systemProps.remove(propToRemove); + } } }; @@ -337,7 +311,7 @@ public class ExportImportTest { Assert.fail("user " + username + " not found"); } - Assert.assertTrue(realm.validatePassword(user, password)); + Assert.assertTrue(userProvider.validCredentials(realm, user, UserCredentialModel.password(password))); } private void assertNotAuthenticated(UserProvider userProvider, RealmProvider realmProvider, String realmName, String username, String password) { @@ -351,7 +325,7 @@ public class ExportImportTest { return; } - Assert.assertFalse(realm.validatePassword(user, password)); + Assert.assertFalse(userProvider.validCredentials(realm, user, UserCredentialModel.password(password))); } private static void addUser(UserProvider userProvider, RealmModel appRealm, String username, String password) { @@ -370,11 +344,11 @@ public class ExportImportTest { String relativeDirExportImportPath = "testsuite" + File.separator + "integration" + File.separator + "target" + File.separator + "export-import"; if (System.getProperties().containsKey("maven.home")) { - dirPath = System.getProperty("user.dir").replaceFirst("testsuite.integration.*", relativeDirExportImportPath); + dirPath = System.getProperty("user.dir").replaceFirst("testsuite.integration.*", Matcher.quoteReplacement(relativeDirExportImportPath)); } else { for (String c : System.getProperty("java.class.path").split(File.pathSeparator)) { if (c.contains(File.separator + "testsuite" + File.separator + "integration")) { - dirPath = c.replaceFirst("testsuite.integration.*", relativeDirExportImportPath); + dirPath = c.replaceFirst("testsuite.integration.*", Matcher.quoteReplacement(relativeDirExportImportPath)); } } } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java index b313d70d51..7a5db77c6a 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java @@ -14,6 +14,7 @@ import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.services.managers.RealmManager; +import org.keycloak.testsuite.Retry; import org.keycloak.testutils.KeycloakServer; import org.keycloak.util.JsonSerialization; @@ -21,6 +22,8 @@ import javax.servlet.Servlet; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.ConnectException; +import java.net.Socket; /** * @author Bill Burke @@ -114,15 +117,7 @@ public abstract class AbstractKeycloakRule extends ExternalResource { @Override protected void after() { - server.stop(); - - // Add some variable delay (Some windows envs have issues as server is not stopped immediately after server.stop) - try { - int sleepInterval = Integer.parseInt(System.getProperty("testsuite.delay", "0")); - Thread.sleep(sleepInterval); - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - } + stopServer(); } public RealmRepresentation loadJson(String path) throws IOException { @@ -151,10 +146,32 @@ public abstract class AbstractKeycloakRule extends ExternalResource { public void restartServer() { try { - server.stop(); + stopServer(); server.start(); } catch (Throwable t) { throw new RuntimeException(t); } } + + private void stopServer() { + server.stop(); + + // Add some variable delay (Some windows envs have issues as server is not stopped immediately after server.stop) + try { + Retry.execute(new Runnable() { + + @Override + public void run() { + try { + new Socket(server.getConfig().getHost(), server.getConfig().getPort()); + throw new RuntimeException(); + } catch (IOException expected) { + } + } + + }, 10, 500); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } }