]> source.dussan.org Git - vaadin-framework.git/commitdiff
Prevent erroneous screenshot removal (#14446)
authorJuuso Valli <juuso@vaadin.com>
Mon, 18 Aug 2014 09:15:33 +0000 (12:15 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 19 Aug 2014 13:52:25 +0000 (13:52 +0000)
Change-Id: I6832399a888a8f99a77b6d345271d02e5cec3f54

uitest/src/com/vaadin/tests/tb3/ScreenshotTB3Test.java

index 1782e0042e2b1b127d3d288b4c3e9317f7667567..a99eea9bf68b00f87bdefd2591247fb03f855259 100644 (file)
@@ -180,9 +180,20 @@ public abstract class ScreenshotTB3Test extends AbstractTB3Test {
      *         given reference image fails.
      */
     private File getErrorFileFromReference(File referenceFile) {
-        return new File(referenceFile.getAbsolutePath().replace(
-                getScreenshotReferenceDirectory(),
-                getScreenshotErrorDirectory()));
+
+        String absolutePath = referenceFile.getAbsolutePath();
+        String screenshotReferenceDirectory = getScreenshotReferenceDirectory();
+        String screenshotErrorDirectory = getScreenshotErrorDirectory();
+        // We throw an exception to safeguard against accidental reference
+        // deletion. See (#14446)
+        if (!absolutePath.contains(screenshotReferenceDirectory)) {
+            throw new IllegalStateException(
+                    "Reference screenshot not in reference directory. Screenshot path: '"
+                            + absolutePath + "', directory path: '"
+                            + screenshotReferenceDirectory + "'");
+        }
+        return new File(absolutePath.replace(screenshotReferenceDirectory,
+                screenshotErrorDirectory));
     }
 
     /**
@@ -247,12 +258,24 @@ public abstract class ScreenshotTB3Test extends AbstractTB3Test {
      */
     protected abstract String getScreenshotDirectory();
 
+    /**
+     * @return the base directory of 'reference' and 'errors' screenshots with a
+     *         trailing file separator
+     */
+    private String getScreenshotDirectoryWithTrailingSeparator() {
+        String screenshotDirectory = getScreenshotDirectory();
+        if (!screenshotDirectory.endsWith(File.separator)) {
+            screenshotDirectory += File.separator;
+        }
+        return screenshotDirectory;
+    }
+
     /**
      * @return the directory where reference images are stored (the 'reference'
      *         folder inside the screenshot directory)
      */
     private String getScreenshotReferenceDirectory() {
-        return getScreenshotDirectory() + "/reference";
+        return getScreenshotDirectoryWithTrailingSeparator() + "reference";
     }
 
     /**
@@ -260,7 +283,7 @@ public abstract class ScreenshotTB3Test extends AbstractTB3Test {
      *         (the 'errors' folder inside the screenshot directory)
      */
     private String getScreenshotErrorDirectory() {
-        return getScreenshotDirectory() + "/errors";
+        return getScreenshotDirectoryWithTrailingSeparator() + "errors";
     }
 
     /**
@@ -366,7 +389,7 @@ public abstract class ScreenshotTB3Test extends AbstractTB3Test {
         }
 
         // WindowMaximizeRestoreTest_Windows_InternetExplorer_8_window-1-moved-maximized-restored.png
-        return getScreenshotReferenceDirectory() + "/"
+        return getScreenshotReferenceDirectory() + File.separator
                 + getScreenshotBaseName() + "_" + uniqueBrowserIdentifier + "_"
                 + identifier + ".png";
     }
@@ -399,13 +422,12 @@ public abstract class ScreenshotTB3Test extends AbstractTB3Test {
             errorDirectory.mkdirs();
         }
 
-        final String errorBase = getScreenshotErrorBaseName()
-                .replace("\\", "/");
+        final String errorBase = getScreenshotErrorBaseName();
         File[] files = errorDirectory.listFiles(new FileFilter() {
 
             @Override
             public boolean accept(File pathname) {
-                String thisFile = pathname.getAbsolutePath().replace("\\", "/");
+                String thisFile = pathname.getAbsolutePath();
                 if (thisFile.startsWith(errorBase)) {
                     return true;
                 }