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
|
|
2 | 2 | package org.openstreetmap.josm; |
3 | 3 | |
4 | 4 | import static org.junit.Assert.assertEquals; |
| 5 | import static org.junit.Assert.assertArrayEquals; |
| 6 | import static org.junit.Assert.assertTrue; |
5 | 7 | import static org.junit.Assert.fail; |
6 | 8 | |
7 | 9 | import java.awt.Component; |
8 | 10 | import java.awt.Container; |
9 | 11 | import java.awt.Graphics2D; |
10 | 12 | import java.io.File; |
| 13 | import java.io.FileInputStream; |
11 | 14 | import java.io.IOException; |
12 | 15 | import java.io.InputStream; |
13 | 16 | import java.lang.reflect.Field; |
… |
… |
import org.openstreetmap.josm.tools.Utils;
|
45 | 48 | import com.github.tomakehurst.wiremock.WireMockServer; |
46 | 49 | import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
47 | 50 | |
| 51 | import com.google.common.io.ByteStreams; |
| 52 | |
48 | 53 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
49 | 54 | |
50 | 55 | /** |
… |
… |
public final class TestUtils {
|
428 | 433 | public static String getHTTPDate(long time) { |
429 | 434 | return getHTTPDate(Instant.ofEpochMilli(time)); |
430 | 435 | } |
| 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 | } |
431 | 461 | } |
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 {
|
86 | 86 | |
87 | 87 | // the ".jar.new" file should have been deleted |
88 | 88 | 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); |
101 | 91 | } |
102 | 92 | |
103 | 93 | /** |
… |
… |
public class PluginDownloadTaskTest extends AbstractDownloadTaskTestParent {
|
139 | 129 | pluginDownloadTask.run(); |
140 | 130 | } |
141 | 131 | |
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); |
144 | 134 | // the ".jar" file should still exist |
145 | 135 | assertTrue(pluginFile.exists()); |
146 | 136 | try ( |
147 | | FileInputStream pluginDirPluginNewStream = new FileInputStream(pluginFileNew); |
148 | 137 | FileInputStream pluginDirPluginStream = new FileInputStream(pluginFile); |
149 | | FileInputStream srcPluginStream = new FileInputStream(srcPluginFile); |
150 | 138 | ) { |
151 | 139 | // the ".jar" file's contents should be as before |
152 | 140 | assertArrayEquals( |
153 | 141 | existingPluginContents, |
154 | 142 | ByteStreams.toByteArray(pluginDirPluginStream) |
155 | 143 | ); |
156 | | // just assert that the "corrupt" jar file made it through in tact |
157 | | assertArrayEquals( |
158 | | ByteStreams.toByteArray(pluginDirPluginNewStream), |
159 | | ByteStreams.toByteArray(srcPluginStream) |
160 | | ); |
161 | 144 | } |
162 | 145 | } |
163 | 146 | } |