]> WPIA git - gigi.git/blobdiff - tests/org/cacert/gigi/testUtils/ManagedTest.java
A try to speed up database resetting for testcases.
[gigi.git] / tests / org / cacert / gigi / testUtils / ManagedTest.java
index a9dd42015c8bdb4930d9fb1e6225b69c2ef0ef5f..ed5f1eec2c831c4fbb5ac5511dcc3004ef33f0ef 100644 (file)
@@ -78,6 +78,11 @@ public class ManagedTest {
     }
 
     static Properties testProps = new Properties();
+
+    public static Properties getTestProps() {
+        return testProps;
+    }
+
     static {
         InitTruststore.run();
         HttpURLConnection.setFollowRedirects(false);
@@ -90,20 +95,17 @@ public class ManagedTest {
             if ( !DatabaseConnection.isInited()) {
                 DatabaseConnection.init(testProps);
             }
-            System.out.println("... purging Database");
-            DatabaseManager.run(new String[] {
-                    testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
-            });
+            purgeDatabase();
             String type = testProps.getProperty("type");
             Properties mainProps = generateMainProps();
             ServerConstants.init(mainProps);
             if (type.equals("local")) {
-                url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort");
+                url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
                 String[] parts = testProps.getProperty("mail").split(":", 2);
                 ter = new TestEmailReciever(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
                 return;
             }
-            url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort");
+            url = testProps.getProperty("name.www") + ":" + testProps.getProperty("serverPort.https");
             gigi = Runtime.getRuntime().exec(testProps.getProperty("java"));
             DataOutputStream toGigi = new DataOutputStream(gigi.getOutputStream());
             System.out.println("... starting server");
@@ -139,8 +141,6 @@ public class ManagedTest {
             SimpleSigner.runSigner();
         } catch (IOException e) {
             throw new Error(e);
-        } catch (ClassNotFoundException e1) {
-            e1.printStackTrace();
         } catch (SQLException e1) {
             e1.printStackTrace();
         } catch (InterruptedException e) {
@@ -149,6 +149,19 @@ public class ManagedTest {
 
     }
 
+    public static void purgeDatabase() throws SQLException, IOException {
+        System.out.print("... resetting Database");
+        long ms = System.currentTimeMillis();
+        try {
+            DatabaseManager.run(new String[] {
+                    testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password")
+            }, true);
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        }
+        System.out.println(" in " + (System.currentTimeMillis() - ms) + " ms");
+    }
+
     private static Properties generateMainProps() {
         Properties mainProps = new Properties();
         mainProps.setProperty("host", "127.0.0.1");
@@ -156,7 +169,8 @@ public class ManagedTest {
         mainProps.setProperty("name.www", testProps.getProperty("name.www"));
         mainProps.setProperty("name.static", testProps.getProperty("name.static"));
 
-        mainProps.setProperty("port", testProps.getProperty("serverPort"));
+        mainProps.setProperty("https.port", testProps.getProperty("serverPort.https"));
+        mainProps.setProperty("http.port", testProps.getProperty("serverPort.http"));
         mainProps.setProperty("emailProvider", "org.cacert.gigi.email.TestEmailProvider");
         mainProps.setProperty("emailProvider.port", "8473");
         mainProps.setProperty("sql.driver", testProps.getProperty("sql.driver"));
@@ -395,6 +409,10 @@ public class ManagedTest {
 
     public static String getCSRF(URLConnection u, int formIndex) throws IOException {
         String content = IOUtils.readURL(u);
+        return getCSRF(formIndex, content);
+    }
+
+    public static String getCSRF(int formIndex, String content) throws Error {
         Pattern p = Pattern.compile("<input type='hidden' name='csrf' value='([^']+)'>");
         Matcher m = p.matcher(content);
         for (int i = 0; i < formIndex + 1; i++) {
@@ -459,4 +477,9 @@ public class ManagedTest {
         return adrr;
     }
 
+    public static URLConnection cookie(URLConnection openConnection, String cookie) {
+        openConnection.setRequestProperty("Cookie", cookie);
+        return openConnection;
+    }
+
 }