Ticket #16010: v2-0018-TestUtils-add-assertFileContentsEqual-use-in-Plug.patch

File v2-0018-TestUtils-add-assertFileContentsEqual-use-in-Plug.patch, 5.0 KB (added by ris, 15 months ago)
  • test/unit/org/openstreetmap/josm/TestUtils.java

    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 b  
    22package org.openstreetmap.josm;
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertArrayEquals;
     6import static org.junit.Assert.assertTrue;
    57import static org.junit.Assert.fail;
    68
    79import java.awt.Component;
    810import java.awt.Container;
    911import java.awt.Graphics2D;
    1012import java.io.File;
     13import java.io.FileInputStream;
    1114import java.io.IOException;
    1215import java.io.InputStream;
    1316import java.lang.reflect.Field;
    import org.openstreetmap.josm.tools.Utils; 
    4548import com.github.tomakehurst.wiremock.WireMockServer;
    4649import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
    4750
     51import com.google.common.io.ByteStreams;
     52
    4853import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    4954
    5055/**
    public final class TestUtils { 
    428433    public static String getHTTPDate(long time) {
    429434        return getHTTPDate(Instant.ofEpochMilli(time));
    430435    }
     436
     437    /**
     438     * Throws AssertionError if contents of both files are not equal
     439     * @param fileA File A
     440     * @param fileB File B
     441     */
     442    public static void assertFileContentsEqual(final File fileA, final File fileB) {
     443        assertTrue(fileA.exists());
     444        assertTrue(fileA.canRead());
     445        assertTrue(fileB.exists());
     446        assertTrue(fileB.canRead());
     447        try {
     448            try (
     449                FileInputStream streamA = new FileInputStream(fileA);
     450                FileInputStream streamB = new FileInputStream(fileB);
     451            ) {
     452                assertArrayEquals(
     453                    ByteStreams.toByteArray(streamA),
     454                    ByteStreams.toByteArray(streamB)
     455                );
     456            }
     457        } catch (IOException e) {
     458            fail(e.toString());
     459        }
     460    }
    431461}
  • test/unit/org/openstreetmap/josm/actions/downloadtasks/PluginDownloadTaskTest.java

    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 b public class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent { 
    8686
    8787        // the ".jar.new" file should have been deleted
    8888        assertFalse(pluginFileNew.exists());
    89         // the ".jar" file should still exist
    90         assertTrue(pluginFile.exists());
    91         try (
    92             FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
    93             FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
    94         ) {
    95             // and its contents should equal those that were served to the task
    96             assertArrayEquals(
    97                 ByteStreams.toByteArray(pluginDirPluginStream),
    98                 ByteStreams.toByteArray(srcPluginStream)
    99             );
    100         }
     89        // the ".jar" file should still exist and its contents should equal those that were served to the task
     90        TestUtils.assertFileContentsEqual(pluginFile, srcPluginFile);
    10191    }
    10292
    10393    /**
    public class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent { 
    139129            pluginDownloadTask.run();
    140130        }
    141131
    142         // the ".jar.new" file should exist, even though invalid
    143         assertTrue(pluginFileNew.exists());
     132        // assert that the "corrupt" jar file made it through in tact
     133        TestUtils.assertFileContentsEqual(pluginFileNew, srcPluginFile);
    144134        // the ".jar" file should still exist
    145135        assertTrue(pluginFile.exists());
    146136        try (
    147             FileInputStream pluginDirPluginNewStream = new FileInputStream(pluginFileNew);
    148137            FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile);
    149             FileInputStream srcPluginStream = new FileInputStream(srcPluginFile);
    150138        ) {
    151139            // the ".jar" file's contents should be as before
    152140            assertArrayEquals(
    153141                existingPluginContents,
    154142                ByteStreams.toByteArray(pluginDirPluginStream)
    155143            );
    156             // just assert that the "corrupt" jar file made it through in tact
    157             assertArrayEquals(
    158                 ByteStreams.toByteArray(pluginDirPluginNewStream),
    159                 ByteStreams.toByteArray(srcPluginStream)
    160             );
    161144        }
    162145    }
    163146}