From d981fe7fb164bede072f00e7b2dc6763b97e67f8 Mon Sep 17 00:00:00 2001
From: Robert Scott <code@humanleg.org.uk>
Date: Sun, 15 Apr 2018 23:30:24 +0100
Subject: [PATCH v2 18/28] TestUtils: add assertFileContentsEqual, use in
 PluginDownloadTaskTest

---
 test/unit/org/openstreetmap/josm/TestUtils.java    | 30 ++++++++++++++++++++++
 .../downloadtasks/PluginDownloadTaskTest.java      | 25 +++---------------
 2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/test/unit/org/openstreetmap/josm/TestUtils.java b/test/unit/org/openstreetmap/josm/TestUtils.java
index 42917c79f..53e995a30 100644
--- a/test/unit/org/openstreetmap/josm/TestUtils.java
+++ b/test/unit/org/openstreetmap/josm/TestUtils.java
@@ -2,12 +2,15 @@
 package org.openstreetmap.josm;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.awt.Component;
 import java.awt.Container;
 import java.awt.Graphics2D;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Field;
@@ -45,6 +48,8 @@ import org.openstreetmap.josm.tools.Utils;
 import com.github.tomakehurst.wiremock.WireMockServer;
 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
 
+import com.google.common.io.ByteStreams;
+
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 /**
@@ -428,4 +433,29 @@ public final class TestUtils {
     public static String getHTTPDate(long time) {
         return getHTTPDate(Instant.ofEpochMilli(time));
     }
+
+    /**
+     * Throws AssertionError if contents of both files are not equal
+     * @param fileA File A
+     * @param fileB File B
+     */
+    public static void assertFileContentsEqual(final File fileA, final File fileB) {
+        assertTrue(fileA.exists());
+        assertTrue(fileA.canRead());
+        assertTrue(fileB.exists());
+        assertTrue(fileB.canRead());
+        try {
+            try (
+                FileInputStream streamA = new FileInputStream(fileA);
+                FileInputStream streamB = new FileInputStream(fileB);
+            ) {
+                assertArrayEquals(
+                    ByteStreams.toByteArray(streamA),
+                    ByteStreams.toByteArray(streamB)
+                );
+            }
+        } catch (IOException e) {
+            fail(e.toString());
+        }
+    }
 }
diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
index 145177cc9..39de9a2b3 100644
--- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
+++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java
@@ -86,18 +86,8 @@ public class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent {
 
         // the ".jar.new" file should have been deleted
         assertFalse(pluginFileNew.exists());
-        // the ".jar" file should still exist
-        assertTrue(pluginFile.exists());
-        try (
-            FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
-            FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
-        ) {
-            // and its contents should equal those that were served to the task
-            assertArrayEquals(
-                ByteStreams.toByteArray(pluginDirPluginStream),
-                ByteStreams.toByteArray(srcPluginStream)
-            );
-        }
+        // the ".jar" file should still exist and its contents should equal those that were served to the task
+        TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile);
     }
 
     /**
@@ -139,25 +129,18 @@ public class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent {
             pluginDownloadTask.run();
         }
 
-        // the ".jar.new" file should exist, even though invalid
-        assertTrue(pluginFileNew.exists());
+        // assert that the "corrupt" jar file made it through in tact
+        TestUtils.assertFileContentsEqual(pluginFileNew, srcPluginFile);
         // the ".jar" file should still exist
         assertTrue(pluginFile.exists());
         try (
-            FileInputStream pluginDirPluginNewStream = new FileInputStream(pluginFileNew);
             FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
-            FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
         ) {
             // the ".jar" file's contents should be as before
             assertArrayEquals(
                 existingPluginContents,
                 ByteStreams.toByteArray(pluginDirPluginStream)
             );
-            // just assert that the "corrupt" jar file made it through in tact
-            assertArrayEquals(
-                ByteStreams.toByteArray(pluginDirPluginNewStream),
-                ByteStreams.toByteArray(srcPluginStream)
-            );
         }
     }
 }
-- 
2.11.0

