changeset 1158:99cfefc38e04

Properly fix deletion of storage data during tests Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-July/007283.html
author Jon VanAlten <vanaltj@gmail.com>
date Wed, 10 Jul 2013 17:39:17 -0600
parents 8cd620e9a658
children 9f499f1ff126
files integration-tests/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java
diffstat 3 files changed, 22 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java	Thu Jul 04 09:51:54 2013 -0600
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/IntegrationTest.java	Wed Jul 10 17:39:17 2013 -0600
@@ -42,6 +42,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -73,6 +74,11 @@
         return "../distribution/target/storage/db";
     }
 
+    public static void clearStorageDataDirectory() throws IOException {
+        String storageDir = getStorageDataDirectory();
+        deleteFilesRecursivelyUnder(storageDir);
+    }
+
     public static Spawn spawnThermostat(String... args) throws IOException {
         return spawnThermostat(false, args);
     }
@@ -86,7 +92,6 @@
             }
         }
         String toExecute = result.toString();
-        //System.out.println("executing: '" + toExecute + "'");
         if (localeDependent) {
             Executor exec = new LocaleExecutor(toExecute);
             return expect.spawn(exec);
@@ -158,15 +163,20 @@
         return (int) pidField.get(process);
     }
 
-    public static void deleteFilesUnder(String path) throws IOException {
-        String[] filesToDelete = new File(path).list();
-        for (String toDelete : filesToDelete) {
-            File theFile = new File(path, toDelete);
-            if (!theFile.delete()) {
-                if (theFile.exists()) {
-                    throw new IOException("cant delete: '" + theFile.toString() + "'.");
-                }
+    private static void deleteFilesRecursivelyUnder(String path) throws IOException {
+        File directory = new File(path);
+        if (!directory.isDirectory()) {
+            throw new IOException("Cannot delete files under a non-directory: " + path);
+        }
+        File[] filesToDelete = directory.listFiles();
+        if (filesToDelete == null) {
+            throw new IOException("Error getting directory listing: " + path);
+        }
+        for (File theFile : filesToDelete) {
+            if (theFile.isDirectory()) {
+                deleteFilesRecursivelyUnder(theFile.getCanonicalPath());
             }
+            Files.deleteIfExists(theFile.toPath());
         }
     }
 
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java	Thu Jul 04 09:51:54 2013 -0600
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/VmCommandsTest.java	Wed Jul 10 17:39:17 2013 -0600
@@ -36,8 +36,6 @@
 
 package com.redhat.thermostat.itest;
 
-import static org.junit.Assert.assertEquals;
-
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -55,8 +53,7 @@
 
     @BeforeClass
     public static void setUpOnce() throws IOException, InterruptedException {
-        String staleDataDir = getStorageDataDirectory();
-        deleteFilesUnder(staleDataDir);
+        clearStorageDataDirectory();
 
         Process startStorage = new ProcessBuilder(getThermostatExecutable(), "storage", "--start").start();
         startStorage.waitFor();
--- a/integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java	Thu Jul 04 09:51:54 2013 -0600
+++ b/integration-tests/src/test/java/com/redhat/thermostat/itest/WebAppTest.java	Wed Jul 10 17:39:17 2013 -0600
@@ -36,11 +36,6 @@
 
 package com.redhat.thermostat.itest;
 
-import static com.redhat.thermostat.itest.IntegrationTest.assertNoExceptions;
-import static com.redhat.thermostat.itest.IntegrationTest.deleteFilesUnder;
-import static com.redhat.thermostat.itest.IntegrationTest.getStorageDataDirectory;
-import static com.redhat.thermostat.itest.IntegrationTest.getThermostatHome;
-import static com.redhat.thermostat.itest.IntegrationTest.spawnThermostat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -85,7 +80,7 @@
 
 import expectj.Spawn;
 
-public class WebAppTest {
+public class WebAppTest extends IntegrationTest {
 
     private static final String THERMOSTAT_USERS_FILE = getThermostatHome() +
             "/etc/thermostat-users.properties";
@@ -99,8 +94,7 @@
 
     @BeforeClass
     public static void setUpOnce() throws Exception {
-        String staleDataDir = getStorageDataDirectory();
-        deleteFilesUnder(staleDataDir);
+        clearStorageDataDirectory();
 
         Spawn storage = spawnThermostat("storage", "--start");
         storage.expect("pid:");