Skip to content

Commit f4d53c7

Browse files
CODEC-300 - Simplify assertion (#84)
1 parent bd40de8 commit f4d53c7

15 files changed

Lines changed: 110 additions & 110 deletions

src/test/java/org/apache/commons/codec/StringEncoderComparatorTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.commons.codec;
1919

2020
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
2221

2322
import java.util.Arrays;
2423
import java.util.Collections;
@@ -39,9 +38,8 @@ public void testComparatorWithSoundex() throws Exception {
3938
final StringEncoderComparator sCompare =
4039
new StringEncoderComparator( new Soundex() );
4140

42-
assertTrue( "O'Brien and O'Brian didn't come out with " +
43-
"the same Soundex, something must be wrong here",
44-
0 == sCompare.compare( "O'Brien", "O'Brian" ) );
41+
assertEquals("O'Brien and O'Brian didn't come out with " +
42+
"the same Soundex, something must be wrong here", 0, sCompare.compare("O'Brien", "O'Brian"));
4543
}
4644

4745
@SuppressWarnings("unchecked") // cannot easily avoid this warning

src/test/java/org/apache/commons/codec/binary/Base16Test.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import java.nio.charset.Charset;
2727
import java.nio.charset.StandardCharsets;
28-
import java.util.Arrays;
2928
import java.util.Random;
3029

3130
import static org.junit.Assert.assertArrayEquals;
@@ -140,15 +139,15 @@ public void testEmptyBase16() {
140139
byte[] empty = {};
141140
byte[] result = new Base16().encode(empty);
142141
assertEquals("empty Base16 encode", 0, result.length);
143-
assertEquals("empty Base16 encode", null, new Base16().encode(null));
142+
assertNull("empty Base16 encode", new Base16().encode(null));
144143
result = new Base16().encode(empty, 0, 1);
145144
assertEquals("empty Base16 encode with offset", 0, result.length);
146-
assertEquals("empty Base16 encode with offset", null, new Base16().encode(null));
145+
assertNull("empty Base16 encode with offset", new Base16().encode(null));
147146

148147
empty = new byte[0];
149148
result = new Base16().decode(empty);
150149
assertEquals("empty Base16 decode", 0, result.length);
151-
assertEquals("empty Base16 encode", null, new Base16().decode((byte[]) null));
150+
assertNull("empty Base16 encode", new Base16().decode((byte[]) null));
152151
}
153152

154153
// encode/decode a large random array
@@ -412,7 +411,7 @@ public void testSingletons() {
412411
assertEquals("68", new String(new Base16().encode(new byte[] { (byte) 104 })));
413412
for (int i = -128; i <= 127; i++) {
414413
final byte test[] = { (byte) i };
415-
assertTrue(Arrays.equals(test, new Base16().decode(new Base16().encode(test))));
414+
assertArrayEquals(test, new Base16().decode(new Base16().encode(test)));
416415
}
417416
}
418417

@@ -447,8 +446,8 @@ public void testByteToStringVariations() throws DecoderException {
447446
assertEquals("byteToString static Hello World", "48656C6C6F20576F726C64", StringUtils.newStringUtf8(new Base16().encode(b1)));
448447
assertEquals("byteToString \"\"", "", base16.encodeToString(b2));
449448
assertEquals("byteToString static \"\"", "", StringUtils.newStringUtf8(new Base16().encode(b2)));
450-
assertEquals("byteToString null", null, base16.encodeToString(b3));
451-
assertEquals("byteToString static null", null, StringUtils.newStringUtf8(new Base16().encode(b3)));
449+
assertNull("byteToString null", base16.encodeToString(b3));
450+
assertNull("byteToString static null", StringUtils.newStringUtf8(new Base16().encode(b3)));
452451
}
453452

454453
@Test
@@ -465,8 +464,8 @@ public void testStringToByteVariations() throws DecoderException {
465464
StringUtils.newStringUtf8(new Base16().decode(s1)));
466465
assertEquals("StringToByte \"\"", "", StringUtils.newStringUtf8(new Base16().decode(s2)));
467466
assertEquals("StringToByte static \"\"", "", StringUtils.newStringUtf8(new Base16().decode(s2)));
468-
assertEquals("StringToByte null", null, StringUtils.newStringUtf8(new Base16().decode(s3)));
469-
assertEquals("StringToByte static null", null, StringUtils.newStringUtf8(new Base16().decode(s3)));
467+
assertNull("StringToByte null", StringUtils.newStringUtf8(new Base16().decode(s3)));
468+
assertNull("StringToByte static null", StringUtils.newStringUtf8(new Base16().decode(s3)));
470469
}
471470

472471
private String toString(final byte[] data) {

src/test/java/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.assertNull;
2425
import static org.junit.Assert.assertTrue;
2526
import static org.junit.Assert.fail;
2627

@@ -298,15 +299,15 @@ public void testEmptyBase32() {
298299
byte[] empty = {};
299300
byte[] result = new Base32().encode(empty);
300301
assertEquals("empty Base32 encode", 0, result.length);
301-
assertEquals("empty Base32 encode", null, new Base32().encode(null));
302+
assertNull("empty Base32 encode", new Base32().encode(null));
302303
result = new Base32().encode(empty, 0, 1);
303304
assertEquals("empty Base32 encode with offset", 0, result.length);
304-
assertEquals("empty Base32 encode with offset", null, new Base32().encode(null));
305+
assertNull("empty Base32 encode with offset", new Base32().encode(null));
305306

306307
empty = new byte[0];
307308
result = new Base32().decode(empty);
308309
assertEquals("empty Base32 decode", 0, result.length);
309-
assertEquals("empty Base32 encode", null, new Base32().decode((byte[]) null));
310+
assertNull("empty Base32 encode", new Base32().decode((byte[]) null));
310311
}
311312

312313
@Test

src/test/java/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertNotNull;
24+
import static org.junit.Assert.assertNull;
2425
import static org.junit.Assert.assertTrue;
2526
import static org.junit.Assert.fail;
2627

@@ -178,7 +179,7 @@ public void testDecodeWithInnerPad() {
178179
final String content = "SGVsbG8gV29ybGQ=SGVsbG8gV29ybGQ=";
179180
final byte[] result = Base64.decodeBase64(content);
180181
final byte[] shouldBe = StringUtils.getBytesUtf8("Hello World");
181-
assertTrue("decode should halt at pad (=)", Arrays.equals(result, shouldBe));
182+
assertArrayEquals("decode should halt at pad (=)", result, shouldBe);
182183
}
183184

184185
/**
@@ -192,7 +193,7 @@ public void testChunkedEncodeMultipleOf76() {
192193
// in Base64TestData.ENCODED_76_CHARS_PER_LINE:
193194
final String actualResult = Base64TestData.ENCODED_76_CHARS_PER_LINE.replace("\n", "\r\n");
194195
final byte[] actualEncode = StringUtils.getBytesUtf8(actualResult);
195-
assertTrue("chunkedEncodeMultipleOf76", Arrays.equals(expectedEncode, actualEncode));
196+
assertArrayEquals("chunkedEncodeMultipleOf76", expectedEncode, actualEncode);
196197
}
197198

198199
/**
@@ -401,15 +402,15 @@ public void testEmptyBase64() {
401402
byte[] empty = {};
402403
byte[] result = Base64.encodeBase64(empty);
403404
assertEquals("empty base64 encode", 0, result.length);
404-
assertEquals("empty base64 encode", null, Base64.encodeBase64(null));
405+
assertNull("empty base64 encode", Base64.encodeBase64(null));
405406
result = new Base64().encode(empty, 0, 1);
406407
assertEquals("empty base64 encode", 0, result.length);
407-
assertEquals("empty base64 encode", null, new Base64().encode(null, 0, 1));
408+
assertNull("empty base64 encode", new Base64().encode(null, 0, 1));
408409

409410
empty = new byte[0];
410411
result = Base64.decodeBase64(empty);
411412
assertEquals("empty base64 decode", 0, result.length);
412-
assertEquals("empty base64 encode", null, Base64.decodeBase64((byte[]) null));
413+
assertNull("empty base64 encode", Base64.decodeBase64((byte[]) null));
413414
}
414415

415416
// encode/decode a large random array
@@ -421,7 +422,7 @@ public void testEncodeDecodeRandom() {
421422
final byte[] enc = Base64.encodeBase64(data);
422423
assertTrue(Base64.isBase64(enc));
423424
final byte[] data2 = Base64.decodeBase64(enc);
424-
assertTrue(Arrays.equals(data, data2));
425+
assertArrayEquals(data, data2);
425426
}
426427
}
427428

@@ -434,7 +435,7 @@ public void testEncodeDecodeSmall() {
434435
final byte[] enc = Base64.encodeBase64(data);
435436
assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc));
436437
final byte[] data2 = Base64.decodeBase64(enc);
437-
assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2));
438+
assertArrayEquals(toString(data) + " equals " + toString(data2), data, data2);
438439
}
439440
}
440441

@@ -619,7 +620,7 @@ public void testPairs() {
619620
assertEquals("AAA=", new String(Base64.encodeBase64(new byte[] { 0, 0 })));
620621
for (int i = -128; i <= 127; i++) {
621622
final byte test[] = { (byte) i, (byte) i };
622-
assertTrue(Arrays.equals(test, Base64.decodeBase64(Base64.encodeBase64(test))));
623+
assertArrayEquals(test, Base64.decodeBase64(Base64.encodeBase64(test)));
623624
}
624625
}
625626

@@ -628,7 +629,7 @@ public void testPairs() {
628629
*/
629630
@Test
630631
public void testRfc2045Section2Dot1CrLfDefinition() {
631-
assertTrue(Arrays.equals(new byte[] { 13, 10 }, Base64.CHUNK_SEPARATOR));
632+
assertArrayEquals(new byte[]{13, 10}, Base64.CHUNK_SEPARATOR);
632633
}
633634

634635
/**
@@ -899,7 +900,7 @@ public void testSingletons() {
899900
assertEquals("aA==", new String(Base64.encodeBase64(new byte[] { (byte) 104 })));
900901
for (int i = -128; i <= 127; i++) {
901902
final byte test[] = { (byte) i };
902-
assertTrue(Arrays.equals(test, Base64.decodeBase64(Base64.encodeBase64(test))));
903+
assertArrayEquals(test, Base64.decodeBase64(Base64.encodeBase64(test)));
903904
}
904905
}
905906

@@ -1160,7 +1161,7 @@ public void testUrlSafe() {
11601161
final byte[] encoded = randomData[1];
11611162
final byte[] decoded = randomData[0];
11621163
final byte[] result = Base64.decodeBase64(encoded);
1163-
assertTrue("url-safe i=" + i, Arrays.equals(decoded, result));
1164+
assertArrayEquals("url-safe i=" + i, decoded, result);
11641165
assertFalse("url-safe i=" + i + " no '='", BaseNTestData.bytesContain(encoded, (byte) '='));
11651166
assertFalse("url-safe i=" + i + " no '\\'", BaseNTestData.bytesContain(encoded, (byte) '\\'));
11661167
assertFalse("url-safe i=" + i + " no '+'", BaseNTestData.bytesContain(encoded, (byte) '+'));
@@ -1244,12 +1245,12 @@ public void testUUID() throws DecoderException {
12441245
// + StringUtils.newStringUtf8(urlSafe3[i]) + "]");
12451246
// }
12461247

1247-
assertTrue("standard encode uuid", Arrays.equals(encodedStandard, standard[i]));
1248-
assertTrue("url-safe encode uuid", Arrays.equals(encodedUrlSafe, urlSafe3[i]));
1249-
assertTrue("standard decode uuid", Arrays.equals(decodedStandard, ids[i]));
1250-
assertTrue("url-safe1 decode uuid", Arrays.equals(decodedUrlSafe1, ids[i]));
1251-
assertTrue("url-safe2 decode uuid", Arrays.equals(decodedUrlSafe2, ids[i]));
1252-
assertTrue("url-safe3 decode uuid", Arrays.equals(decodedUrlSafe3, ids[i]));
1248+
assertArrayEquals("standard encode uuid", encodedStandard, standard[i]);
1249+
assertArrayEquals("url-safe encode uuid", encodedUrlSafe, urlSafe3[i]);
1250+
assertArrayEquals("standard decode uuid", decodedStandard, ids[i]);
1251+
assertArrayEquals("url-safe1 decode uuid", decodedUrlSafe1, ids[i]);
1252+
assertArrayEquals("url-safe2 decode uuid", decodedUrlSafe2, ids[i]);
1253+
assertArrayEquals("url-safe3 decode uuid", decodedUrlSafe3, ids[i]);
12531254
}
12541255
}
12551256

@@ -1267,8 +1268,8 @@ public void testByteToStringVariations() throws DecoderException {
12671268
assertEquals("byteToString static Hello World", "SGVsbG8gV29ybGQ=", Base64.encodeBase64String(b1));
12681269
assertEquals("byteToString \"\"", "", base64.encodeToString(b2));
12691270
assertEquals("byteToString static \"\"", "", Base64.encodeBase64String(b2));
1270-
assertEquals("byteToString null", null, base64.encodeToString(b3));
1271-
assertEquals("byteToString static null", null, Base64.encodeBase64String(b3));
1271+
assertNull("byteToString null", base64.encodeToString(b3));
1272+
assertNull("byteToString static null", Base64.encodeBase64String(b3));
12721273
assertEquals("byteToString UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", base64.encodeToString(b4));
12731274
assertEquals("byteToString static UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", Base64.encodeBase64String(b4));
12741275
assertEquals("byteToString static-url-safe UUID", "K_fMJwH-Q5e0nr7tWsxwkA",
@@ -1294,11 +1295,11 @@ public void testStringToByteVariations() throws DecoderException {
12941295
StringUtils.newStringUtf8(Base64.decodeBase64(s1)));
12951296
assertEquals("StringToByte \"\"", "", StringUtils.newStringUtf8(base64.decode(s2)));
12961297
assertEquals("StringToByte static \"\"", "", StringUtils.newStringUtf8(Base64.decodeBase64(s2)));
1297-
assertEquals("StringToByte null", null, StringUtils.newStringUtf8(base64.decode(s3)));
1298-
assertEquals("StringToByte static null", null, StringUtils.newStringUtf8(Base64.decodeBase64(s3)));
1299-
assertTrue("StringToByte UUID", Arrays.equals(b4, base64.decode(s4b)));
1300-
assertTrue("StringToByte static UUID", Arrays.equals(b4, Base64.decodeBase64(s4a)));
1301-
assertTrue("StringToByte static-url-safe UUID", Arrays.equals(b4, Base64.decodeBase64(s4b)));
1298+
assertNull("StringToByte null", StringUtils.newStringUtf8(base64.decode(s3)));
1299+
assertNull("StringToByte static null", StringUtils.newStringUtf8(Base64.decodeBase64(s3)));
1300+
assertArrayEquals("StringToByte UUID", b4, base64.decode(s4b));
1301+
assertArrayEquals("StringToByte static UUID", b4, Base64.decodeBase64(s4a));
1302+
assertArrayEquals("StringToByte static-url-safe UUID", b4, Base64.decodeBase64(s4b));
13021303
}
13031304

13041305
private String toString(final byte[] data) {

0 commit comments

Comments
 (0)