changeset 1393:c3eead964d9e

PR1738 Fix WebStorageEndpointTest in thermostat 1.0. Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-April/009645.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Tue, 18 Feb 2014 15:42:38 +0100
parents 0bb2a75521f3
children e6649b85a055
files web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndPointUnitTest.java web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndpointTest.java web/server/src/test/java/com/redhat/thermostat/web/server/WebstorageEndpointTestUtils.java
diffstat 3 files changed, 101 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndPointUnitTest.java	Fri Mar 21 11:15:06 2014 +0100
+++ b/web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndPointUnitTest.java	Tue Feb 18 15:42:38 2014 +0100
@@ -44,13 +44,9 @@
 import static org.mockito.Mockito.when;
 
 import java.io.File;
-import java.io.IOException;
 import java.lang.reflect.Method;
-import java.nio.file.FileVisitResult;
-import java.nio.file.FileVisitor;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.attribute.BasicFileAttributes;
 import java.nio.file.attribute.FileAttribute;
 import java.util.HashMap;
 import java.util.Map;
@@ -204,7 +200,7 @@
         } finally {
             etcDir.setExecutable(true);
             if (testThermostatHome != null) {
-                deleteDirectoryRecursive(testThermostatHome);
+                WebstorageEndpointTestUtils.deleteDirectoryRecursive(testThermostatHome);
             }
         }
     }
@@ -226,43 +222,9 @@
             }
         } finally {
             if (testThermostatHome != null) {
-                deleteDirectoryRecursive(testThermostatHome);
+                WebstorageEndpointTestUtils.deleteDirectoryRecursive(testThermostatHome);
             }
         }
     }
     
-    private void deleteDirectoryRecursive(Path dir) throws IOException {
-        Files.walkFileTree(dir, new FileVisitor<Path>() {
-
-            @Override
-            public FileVisitResult preVisitDirectory(Path dir,
-                    BasicFileAttributes attrs) throws IOException {
-                // nothing
-                return FileVisitResult.CONTINUE;
-            }
-
-            @Override
-            public FileVisitResult visitFile(Path file,
-                    BasicFileAttributes attrs) throws IOException {
-                Files.delete(file);
-                return FileVisitResult.CONTINUE;
-            }
-
-            @Override
-            public FileVisitResult visitFileFailed(Path file, IOException exc)
-                    throws IOException {
-                exc.printStackTrace();
-                return FileVisitResult.CONTINUE;
-            }
-
-            @Override
-            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
-                    throws IOException {
-                // All files have been visitated before that.
-                Files.delete(dir);
-                return FileVisitResult.CONTINUE;
-            }
-            
-        });
-    }
 }
--- a/web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndpointTest.java	Fri Mar 21 11:15:06 2014 +0100
+++ b/web/server/src/test/java/com/redhat/thermostat/web/server/WebStorageEndpointTest.java	Tue Feb 18 15:42:38 2014 +0100
@@ -61,6 +61,7 @@
 import java.net.ProtocolException;
 import java.net.URL;
 import java.net.URLEncoder;
+import java.nio.file.Path;
 import java.security.Principal;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -178,6 +179,7 @@
     private static Key<Integer> key2;
     private static Category<TestClass> category;
     private static String categoryName = "test";
+    private static File testThermostatHome;
 
     @BeforeClass
     public static void setupCategory() {
@@ -192,6 +194,15 @@
         category = null;
         key2 = null;
         key1 = null;
+        
+        if (testThermostatHome != null) {
+            Path testTh = testThermostatHome.toPath();
+            try {
+                WebstorageEndpointTestUtils.deleteDirectoryRecursive(testTh);
+            } catch (IOException e) {
+                e.printStackTrace(System.err);
+            }
+        }
     }
 
     @Before
@@ -224,6 +235,12 @@
                 // to create that file in order for the tests to get past this
                 // check.
                 File thermostatHome = new File(ctx.getInitParameter("THERMOSTAT_HOME"));
+                if (testThermostatHome == null) {
+                    testThermostatHome = thermostatHome;
+                }
+                if (!thermostatHome.exists()) {
+                    thermostatHome.mkdir();
+                }
                 File configDirectory = new File(thermostatHome, "etc");
                 if (!configDirectory.exists()) {
                     configDirectory.mkdir();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/server/src/test/java/com/redhat/thermostat/web/server/WebstorageEndpointTestUtils.java	Tue Feb 18 15:42:38 2014 +0100
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2012-2014 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.web.server;
+
+import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.BasicFileAttributes;
+
+public class WebstorageEndpointTestUtils {
+
+    static void deleteDirectoryRecursive(Path dir) throws IOException {
+        Files.walkFileTree(dir, new FileVisitor<Path>() {
+
+            @Override
+            public FileVisitResult preVisitDirectory(Path dir,
+                    BasicFileAttributes attrs) throws IOException {
+                // nothing
+                return FileVisitResult.CONTINUE;
+            }
+
+            @Override
+            public FileVisitResult visitFile(Path file,
+                    BasicFileAttributes attrs) throws IOException {
+                Files.delete(file);
+                return FileVisitResult.CONTINUE;
+            }
+
+            @Override
+            public FileVisitResult visitFileFailed(Path file, IOException exc)
+                    throws IOException {
+                exc.printStackTrace();
+                return FileVisitResult.CONTINUE;
+            }
+
+            @Override
+            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
+                    throws IOException {
+                // All files have been visitated before that.
+                Files.delete(dir);
+                return FileVisitResult.CONTINUE;
+            }
+            
+        });
+    }
+}