Skip to content

Commit 86fdbb2

Browse files
Simplify Assertions (#234)
1 parent a740a04 commit 86fdbb2

22 files changed

Lines changed: 282 additions & 274 deletions

src/test/java/org/apache/commons/io/ByteOrderMarkTestCase.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
package org.apache.commons.io;
1818

1919

20+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2021
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2223
import static org.junit.jupiter.api.Assertions.assertNotNull;
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425
import static org.junit.jupiter.api.Assertions.fail;
@@ -79,26 +80,26 @@ public void get() {
7980
/** Test {@link ByteOrderMark#getBytes()} */
8081
@Test
8182
public void getBytes() {
82-
assertTrue(Arrays.equals(TEST_BOM_1.getBytes(), new byte[] {(byte)1}), "test1 bytes");
83+
assertArrayEquals(TEST_BOM_1.getBytes(), new byte[]{(byte) 1}, "test1 bytes");
8384
TEST_BOM_1.getBytes()[0] = 2;
84-
assertTrue(Arrays.equals(TEST_BOM_1.getBytes(), new byte[] {(byte)1}), "test1 bytes");
85-
assertTrue(Arrays.equals(TEST_BOM_2.getBytes(), new byte[] {(byte)1, (byte)2}), "test1 bytes");
86-
assertTrue(Arrays.equals(TEST_BOM_3.getBytes(), new byte[] {(byte)1, (byte)2, (byte)3}), "test1 bytes");
85+
assertArrayEquals(TEST_BOM_1.getBytes(), new byte[]{(byte) 1}, "test1 bytes");
86+
assertArrayEquals(TEST_BOM_2.getBytes(), new byte[]{(byte) 1, (byte) 2}, "test1 bytes");
87+
assertArrayEquals(TEST_BOM_3.getBytes(), new byte[]{(byte) 1, (byte) 2, (byte) 3}, "test1 bytes");
8788
}
8889

8990
/** Test {@link ByteOrderMark#equals(Object)} */
9091
@SuppressWarnings("EqualsWithItself")
9192
@Test
9293
public void testEquals() {
93-
assertTrue(TEST_BOM_1.equals(TEST_BOM_1), "test1 equals");
94-
assertTrue(TEST_BOM_2.equals(TEST_BOM_2), "test2 equals");
95-
assertTrue(TEST_BOM_3.equals(TEST_BOM_3), "test3 equals");
96-
97-
assertFalse(TEST_BOM_1.equals(new Object()), "Object not equal");
98-
assertFalse(TEST_BOM_1.equals(new ByteOrderMark("1a", 2)), "test1-1 not equal");
99-
assertFalse(TEST_BOM_1.equals(new ByteOrderMark("1b", 1, 2)), "test1-2 not test2");
100-
assertFalse(TEST_BOM_2.equals(new ByteOrderMark("2", 1, 1)), "test2 not equal");
101-
assertFalse(TEST_BOM_3.equals(new ByteOrderMark("3", 1, 2, 4)), "test3 not equal");
94+
assertEquals(TEST_BOM_1, TEST_BOM_1, "test1 equals");
95+
assertEquals(TEST_BOM_2, TEST_BOM_2, "test2 equals");
96+
assertEquals(TEST_BOM_3, TEST_BOM_3, "test3 equals");
97+
98+
assertNotEquals(TEST_BOM_1, new Object(), "Object not equal");
99+
assertNotEquals(TEST_BOM_1, new ByteOrderMark("1a", 2), "test1-1 not equal");
100+
assertNotEquals(TEST_BOM_1, new ByteOrderMark("1b", 1, 2), "test1-2 not test2");
101+
assertNotEquals(TEST_BOM_2, new ByteOrderMark("2", 1, 1), "test2 not equal");
102+
assertNotEquals(TEST_BOM_3, new ByteOrderMark("3", 1, 2, 4), "test3 not equal");
102103
}
103104

104105
/** Test {@link ByteOrderMark#hashCode()} */

src/test/java/org/apache/commons/io/CopyUtilsTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
package org.apache.commons.io;
1818

19+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

2222
import java.io.ByteArrayInputStream;
2323
import java.io.InputStream;
@@ -26,7 +26,6 @@
2626
import java.io.StringWriter;
2727
import java.io.Writer;
2828
import java.nio.charset.StandardCharsets;
29-
import java.util.Arrays;
3029

3130
import org.apache.commons.io.output.ByteArrayOutputStream;
3231
import org.apache.commons.io.test.TestUtils;
@@ -72,7 +71,7 @@ public void copy_byteArrayToOutputStream() throws Exception {
7271
CopyUtils.copy(inData, out);
7372

7473
assertEquals(inData.length, baout.size(), "Sizes differ");
75-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
74+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
7675
}
7776

7877
@Test
@@ -85,7 +84,7 @@ public void copy_byteArrayToWriter() throws Exception {
8584
writer.flush();
8685

8786
assertEquals(inData.length, baout.size(), "Sizes differ");
88-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
87+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
8988
}
9089

9190
@Test
@@ -110,7 +109,7 @@ public void testCopy_inputStreamToOutputStream() throws Exception {
110109

111110
assertEquals(0, in.available(), "Not all bytes were read");
112111
assertEquals(inData.length, baout.size(), "Sizes differ");
113-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
112+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
114113
assertEquals(inData.length, count);
115114
}
116115

@@ -129,7 +128,7 @@ public void copy_inputStreamToWriter() throws Exception {
129128

130129
assertEquals(0, in.available(), "Not all bytes were read");
131130
assertEquals(inData.length, baout.size(), "Sizes differ");
132-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
131+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
133132
}
134133

135134
@Test
@@ -160,7 +159,7 @@ public void testCopy_readerToOutputStream() throws Exception {
160159

161160
// Note: rely on the method to flush
162161
assertEquals(inData.length, baout.size(), "Sizes differ");
163-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
162+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
164163
}
165164

166165
@SuppressWarnings("resource") // 'in' is deliberately not closed
@@ -178,7 +177,7 @@ public void copy_readerToWriter() throws Exception {
178177
writer.flush();
179178
assertEquals(inData.length, count, "The number of characters returned by copy is wrong");
180179
assertEquals(inData.length, baout.size(), "Sizes differ");
181-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
180+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
182181
}
183182

184183
@Test
@@ -197,7 +196,7 @@ public void copy_stringToOutputStream() throws Exception {
197196
// note: we don't flush here; this IOUtils method does it for us
198197

199198
assertEquals(inData.length, baout.size(), "Sizes differ");
200-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
199+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
201200
}
202201

203202
@Test
@@ -212,7 +211,7 @@ public void copy_stringToWriter() throws Exception {
212211
writer.flush();
213212

214213
assertEquals(inData.length, baout.size(), "Sizes differ");
215-
assertTrue(Arrays.equals(inData, baout.toByteArray()), "Content differs");
214+
assertArrayEquals(inData, baout.toByteArray(), "Content differs");
216215
}
217216

218217
} // CopyUtilsTest

src/test/java/org/apache/commons/io/FileCleaningTrackerTestCase.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
2223
import static org.junit.jupiter.api.Assertions.fail;
2324

@@ -105,7 +106,7 @@ public void testFileCleanerFile() throws Exception {
105106
pauseForDeleteToComplete(new File(path));
106107

107108
assertEquals(0, theInstance.getTrackCount());
108-
assertEquals(false, new File(path).exists(), showFailures());
109+
assertFalse(new File(path).exists(), showFailures());
109110
}
110111

111112
@Test
@@ -172,8 +173,8 @@ public void testFileCleanerDirectory_ForceStrategy() throws Exception {
172173
pauseForDeleteToComplete(testFile.getParentFile());
173174

174175
assertEquals(0, theInstance.getTrackCount());
175-
assertEquals(false, new File(testFile.getPath()).exists(), showFailures());
176-
assertEquals(false, testFile.getParentFile().exists(), showFailures());
176+
assertFalse(new File(testFile.getPath()).exists(), showFailures());
177+
assertFalse(testFile.getParentFile().exists(), showFailures());
177178
}
178179

179180
@Test
@@ -209,21 +210,21 @@ public void testFileCleanerExitWhenFinishedFirst() throws Exception {
209210
assertFalse(theInstance.exitWhenFinished);
210211
theInstance.exitWhenFinished();
211212
assertTrue(theInstance.exitWhenFinished);
212-
assertEquals(null, theInstance.reaper);
213+
assertNull(theInstance.reaper);
213214

214215
waitUntilTrackCount();
215216

216217
assertEquals(0, theInstance.getTrackCount());
217218
assertTrue(theInstance.exitWhenFinished);
218-
assertEquals(null, theInstance.reaper);
219+
assertNull(theInstance.reaper);
219220
}
220221

221222
@Test
222223
public void testFileCleanerExitWhenFinished_NoTrackAfter() {
223224
assertFalse(theInstance.exitWhenFinished);
224225
theInstance.exitWhenFinished();
225226
assertTrue(theInstance.exitWhenFinished);
226-
assertEquals(null, theInstance.reaper);
227+
assertNull(theInstance.reaper);
227228

228229
final String path = testFile.getPath();
229230
final Object marker = new Object();
@@ -234,27 +235,27 @@ public void testFileCleanerExitWhenFinished_NoTrackAfter() {
234235
// expected
235236
}
236237
assertTrue(theInstance.exitWhenFinished);
237-
assertEquals(null, theInstance.reaper);
238+
assertNull(theInstance.reaper);
238239
}
239240

240241
@Test
241242
public void testFileCleanerExitWhenFinished1() throws Exception {
242243
final String path = testFile.getPath();
243244

244-
assertEquals(false, testFile.exists(), "1-testFile exists: " + testFile);
245+
assertFalse(testFile.exists(), "1-testFile exists: " + testFile);
245246
RandomAccessFile r = new RandomAccessFile(testFile, "rw");
246-
assertEquals(true, testFile.exists(), "2-testFile exists");
247+
assertTrue(testFile.exists(), "2-testFile exists");
247248

248249
assertEquals(0, theInstance.getTrackCount(), "3-Track Count");
249250
theInstance.track(path, r);
250251
assertEquals(1, theInstance.getTrackCount(), "4-Track Count");
251-
assertEquals(false, theInstance.exitWhenFinished, "5-exitWhenFinished");
252-
assertEquals(true, theInstance.reaper.isAlive(), "6-reaper.isAlive");
252+
assertFalse(theInstance.exitWhenFinished, "5-exitWhenFinished");
253+
assertTrue(theInstance.reaper.isAlive(), "6-reaper.isAlive");
253254

254-
assertEquals(false, theInstance.exitWhenFinished, "7-exitWhenFinished");
255+
assertFalse(theInstance.exitWhenFinished, "7-exitWhenFinished");
255256
theInstance.exitWhenFinished();
256-
assertEquals(true, theInstance.exitWhenFinished, "8-exitWhenFinished");
257-
assertEquals(true, theInstance.reaper.isAlive(), "9-reaper.isAlive");
257+
assertTrue(theInstance.exitWhenFinished, "8-exitWhenFinished");
258+
assertTrue(theInstance.reaper.isAlive(), "9-reaper.isAlive");
258259

259260
r.close();
260261
testFile = null;
@@ -264,9 +265,9 @@ public void testFileCleanerExitWhenFinished1() throws Exception {
264265
pauseForDeleteToComplete(new File(path));
265266

266267
assertEquals(0, theInstance.getTrackCount(), "10-Track Count");
267-
assertEquals(false, new File(path).exists(), "11-testFile exists " + showFailures());
268-
assertEquals(true, theInstance.exitWhenFinished, "12-exitWhenFinished");
269-
assertEquals(false, theInstance.reaper.isAlive(), "13-reaper.isAlive");
268+
assertFalse(new File(path).exists(), "11-testFile exists " + showFailures());
269+
assertTrue(theInstance.exitWhenFinished, "12-exitWhenFinished");
270+
assertFalse(theInstance.reaper.isAlive(), "13-reaper.isAlive");
270271
}
271272

272273
@Test
@@ -291,7 +292,7 @@ public void testFileCleanerExitWhenFinished2() throws Exception {
291292
pauseForDeleteToComplete(new File(path));
292293

293294
assertEquals(0, theInstance.getTrackCount());
294-
assertEquals(false, new File(path).exists(), showFailures());
295+
assertFalse(new File(path).exists(), showFailures());
295296
assertFalse(theInstance.exitWhenFinished);
296297
assertTrue(theInstance.reaper.isAlive());
297298

0 commit comments

Comments
 (0)