Skip to content

Commit

Permalink
Use simplified JUnit assertion methods (#1298)
Browse files Browse the repository at this point in the history
* test: use simplified assertion methods

* test: deprecate test classes for deprecated source classes

* build: remove unused mockito-core dependency

da24624
  • Loading branch information
ParanoidUser authored Oct 16, 2024
1 parent 05d7fdb commit 2a208cf
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 99 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<!-- For Javadoc links -->
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/apache/commons/lang3/CharEncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
/**
* Tests CharEncoding.
*
* @see CharEncoding
* @deprecated this test can be removed once the deprecated source class {@link org.apache.commons.lang3.CharEncoding} is removed.
*/
@SuppressWarnings("deprecation")
@Deprecated
public class CharEncodingTest extends AbstractLangTest {

private void assertSupportedEncoding(final String name) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/apache/commons/lang3/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
* Tests Functions.
*
* @deprecated this test can be removed once the deprecated source class {@link org.apache.commons.lang3.Functions} is removed.
*/
@Deprecated
public class FunctionsTest extends AbstractLangTest {

public static class CloseableObject {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -792,7 +793,7 @@ public void testNotEqual() {
public void testNull() {
assertNotNull(ObjectUtils.NULL);
// 1 Check that NULL really is a Null i.e. the definition has not been changed
assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
assertInstanceOf(ObjectUtils.Null.class, ObjectUtils.NULL);
assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL));
}

Expand Down
23 changes: 11 additions & 12 deletions src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -533,55 +532,55 @@ public void testRandomApis(final RandomStringUtils rsu) {
assertEquals(50, r1.length(), "random(50) length");
String r2 = rsu.next(50);
assertEquals(50, r2.length(), "random(50) length");
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.nextAscii(50);
assertEquals(50, r1.length(), "randomAscii(50) length");
for (int i = 0; i < r1.length(); i++) {
assertThat("char >= 32 && <= 127", (int) r1.charAt(i), allOf(greaterThanOrEqualTo(32), lessThanOrEqualTo(127)));
}
r2 = rsu.nextAscii(50);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.nextAlphabetic(50);
assertEquals(50, r1.length(), "randomAlphabetic(50)");
for (int i = 0; i < r1.length(); i++) {
assertTrue(Character.isLetter(r1.charAt(i)) && !Character.isDigit(r1.charAt(i)), "r1 contains alphabetic");
}
r2 = rsu.nextAlphabetic(50);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.nextAlphanumeric(50);
assertEquals(50, r1.length(), "randomAlphanumeric(50)");
for (int i = 0; i < r1.length(); i++) {
assertTrue(Character.isLetterOrDigit(r1.charAt(i)), "r1 contains alphanumeric");
}
r2 = rsu.nextAlphabetic(50);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.nextGraph(50);
assertEquals(50, r1.length(), "randomGraph(50) length");
for (int i = 0; i < r1.length(); i++) {
assertTrue(r1.charAt(i) >= 33 && r1.charAt(i) <= 126, "char between 33 and 126");
}
r2 = rsu.nextGraph(50);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.nextNumeric(50);
assertEquals(50, r1.length(), "randomNumeric(50)");
for (int i = 0; i < r1.length(); i++) {
assertTrue(Character.isDigit(r1.charAt(i)) && !Character.isLetter(r1.charAt(i)), "r1 contains numeric");
}
r2 = rsu.nextNumeric(50);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.nextPrint(50);
assertEquals(50, r1.length(), "randomPrint(50) length");
for (int i = 0; i < r1.length(); i++) {
assertTrue(r1.charAt(i) >= 32 && r1.charAt(i) <= 126, "char between 32 and 126");
}
r2 = rsu.nextPrint(50);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

String set = "abcdefg";
r1 = rsu.next(50, set);
Expand All @@ -590,13 +589,13 @@ public void testRandomApis(final RandomStringUtils rsu) {
assertTrue(set.indexOf(r1.charAt(i)) > -1, "random char in set");
}
r2 = rsu.next(50, set);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.next(50, (String) null);
assertEquals(50, r1.length(), "random(50) length");
r2 = rsu.next(50, (String) null);
assertEquals(50, r2.length(), "random(50) length");
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

set = "stuvwxyz";
r1 = rsu.next(50, set.toCharArray());
Expand All @@ -605,13 +604,13 @@ public void testRandomApis(final RandomStringUtils rsu) {
assertTrue(set.indexOf(r1.charAt(i)) > -1, "random char in set");
}
r2 = rsu.next(50, set);
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.next(50, (char[]) null);
assertEquals(50, r1.length(), "random(50) length");
r2 = rsu.next(50, (char[]) null);
assertEquals(50, r2.length(), "random(50) length");
assertFalse(r1.equals(r2), "!r1.equals(r2)");
assertNotEquals(r1, r2, "!r1.equals(r2)");

r1 = rsu.next(0);
assertEquals("", r1, "random(0).equals(\"\")");
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/apache/commons/lang3/StreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
import org.junit.jupiter.api.function.Executable;
import org.xml.sax.SAXException;

/**
* Tests Streams.
*
* @deprecated this test can be removed once the deprecated source class {@link org.apache.commons.lang3.Streams} is removed.
*/
@Deprecated
public class StreamsTest extends AbstractLangTest {

protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(final T pThrowable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -164,7 +165,7 @@ public void testSetContextValue() {
assertTrue(message.contains("Crap"));

assertNull(exceptionContext.getFirstContextValue("crap"));
assertTrue(exceptionContext.getFirstContextValue("test Poorly written obj") instanceof ObjectWithFaultyToString);
assertInstanceOf(ObjectWithFaultyToString.class, exceptionContext.getFirstContextValue("test Poorly written obj"));

assertEquals(7, exceptionContext.getContextEntries().size());
assertEquals(6, exceptionContext.getContextLabels().size());
Expand Down
83 changes: 42 additions & 41 deletions src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -389,24 +390,24 @@ public void testCompareFloat() {
@SuppressWarnings("cast") // suppress instanceof warning check
@Test
public void testConstants() {
assertTrue(NumberUtils.LONG_ZERO instanceof Long);
assertTrue(NumberUtils.LONG_ONE instanceof Long);
assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
assertTrue(NumberUtils.SHORT_ONE instanceof Short);
assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
assertInstanceOf(Long.class, NumberUtils.LONG_ZERO);
assertInstanceOf(Long.class, NumberUtils.LONG_ONE);
assertInstanceOf(Long.class, NumberUtils.LONG_MINUS_ONE);
assertInstanceOf(Integer.class, NumberUtils.INTEGER_ZERO);
assertInstanceOf(Integer.class, NumberUtils.INTEGER_ONE);
assertInstanceOf(Integer.class, NumberUtils.INTEGER_MINUS_ONE);
assertInstanceOf(Short.class, NumberUtils.SHORT_ZERO);
assertInstanceOf(Short.class, NumberUtils.SHORT_ONE);
assertInstanceOf(Short.class, NumberUtils.SHORT_MINUS_ONE);
assertInstanceOf(Byte.class, NumberUtils.BYTE_ZERO);
assertInstanceOf(Byte.class, NumberUtils.BYTE_ONE);
assertInstanceOf(Byte.class, NumberUtils.BYTE_MINUS_ONE);
assertInstanceOf(Double.class, NumberUtils.DOUBLE_ZERO);
assertInstanceOf(Double.class, NumberUtils.DOUBLE_ONE);
assertInstanceOf(Double.class, NumberUtils.DOUBLE_MINUS_ONE);
assertInstanceOf(Float.class, NumberUtils.FLOAT_ZERO);
assertInstanceOf(Float.class, NumberUtils.FLOAT_ONE);
assertInstanceOf(Float.class, NumberUtils.FLOAT_MINUS_ONE);

assertEquals(0, NumberUtils.LONG_ZERO.longValue());
assertEquals(1, NumberUtils.LONG_ONE.longValue());
Expand Down Expand Up @@ -1551,31 +1552,31 @@ public void testMinShort_nullArray() {
*/
@Test
public void testStringCreateNumberEnsureNoPrecisionLoss() {
assertTrue(NumberUtils.createNumber("1.23") instanceof Float);
assertTrue(NumberUtils.createNumber("3.40282354e+38") instanceof Double);
assertTrue(NumberUtils.createNumber("1.797693134862315759e+308") instanceof BigDecimal);
assertInstanceOf(Float.class, NumberUtils.createNumber("1.23"));
assertInstanceOf(Double.class, NumberUtils.createNumber("3.40282354e+38"));
assertInstanceOf(BigDecimal.class, NumberUtils.createNumber("1.797693134862315759e+308"));
// LANG-1060
assertTrue(NumberUtils.createNumber("001.12") instanceof Float);
assertTrue(NumberUtils.createNumber("-001.12") instanceof Float);
assertTrue(NumberUtils.createNumber("+001.12") instanceof Float);
assertTrue(NumberUtils.createNumber("003.40282354e+38") instanceof Double);
assertTrue(NumberUtils.createNumber("-003.40282354e+38") instanceof Double);
assertTrue(NumberUtils.createNumber("+003.40282354e+38") instanceof Double);
assertTrue(NumberUtils.createNumber("0001.797693134862315759e+308") instanceof BigDecimal);
assertTrue(NumberUtils.createNumber("-001.797693134862315759e+308") instanceof BigDecimal);
assertTrue(NumberUtils.createNumber("+001.797693134862315759e+308") instanceof BigDecimal);
assertInstanceOf(Float.class, NumberUtils.createNumber("001.12"));
assertInstanceOf(Float.class, NumberUtils.createNumber("-001.12"));
assertInstanceOf(Float.class, NumberUtils.createNumber("+001.12"));
assertInstanceOf(Double.class, NumberUtils.createNumber("003.40282354e+38"));
assertInstanceOf(Double.class, NumberUtils.createNumber("-003.40282354e+38"));
assertInstanceOf(Double.class, NumberUtils.createNumber("+003.40282354e+38"));
assertInstanceOf(BigDecimal.class, NumberUtils.createNumber("0001.797693134862315759e+308"));
assertInstanceOf(BigDecimal.class, NumberUtils.createNumber("-001.797693134862315759e+308"));
assertInstanceOf(BigDecimal.class, NumberUtils.createNumber("+001.797693134862315759e+308"));
//LANG-1613
assertTrue(NumberUtils.createNumber("2.2250738585072014E-308") instanceof Double);
assertTrue(NumberUtils.createNumber("2.2250738585072014E-308D") instanceof Double);
assertTrue(NumberUtils.createNumber("2.2250738585072014E-308F") instanceof Double);
assertTrue(NumberUtils.createNumber("4.9E-324") instanceof Double);
assertTrue(NumberUtils.createNumber("4.9E-324D") instanceof Double);
assertTrue(NumberUtils.createNumber("4.9E-324F") instanceof Double);
assertTrue(NumberUtils.createNumber("1.7976931348623157E308") instanceof Double);
assertTrue(NumberUtils.createNumber("1.7976931348623157E308D") instanceof Double);
assertTrue(NumberUtils.createNumber("1.7976931348623157E308F") instanceof Double);
assertTrue(NumberUtils.createNumber("4.9e-324D") instanceof Double);
assertTrue(NumberUtils.createNumber("4.9e-324F") instanceof Double);
assertInstanceOf(Double.class, NumberUtils.createNumber("2.2250738585072014E-308"));
assertInstanceOf(Double.class, NumberUtils.createNumber("2.2250738585072014E-308D"));
assertInstanceOf(Double.class, NumberUtils.createNumber("2.2250738585072014E-308F"));
assertInstanceOf(Double.class, NumberUtils.createNumber("4.9E-324"));
assertInstanceOf(Double.class, NumberUtils.createNumber("4.9E-324D"));
assertInstanceOf(Double.class, NumberUtils.createNumber("4.9E-324F"));
assertInstanceOf(Double.class, NumberUtils.createNumber("1.7976931348623157E308"));
assertInstanceOf(Double.class, NumberUtils.createNumber("1.7976931348623157E308D"));
assertInstanceOf(Double.class, NumberUtils.createNumber("1.7976931348623157E308F"));
assertInstanceOf(Double.class, NumberUtils.createNumber("4.9e-324D"));
assertInstanceOf(Double.class, NumberUtils.createNumber("4.9e-324F"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -83,7 +84,7 @@ private void callRemoveFinalModifierCheckForException(final Field field, final B
FieldUtils.removeFinalModifier(field, forceAccess);
} catch (final UnsupportedOperationException exception) {
if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_12)) {
assertTrue(exception.getCause() instanceof NoSuchFieldException);
assertInstanceOf(NoSuchFieldException.class, exception.getCause());
} else {
fail("No exception should be thrown for java prior to 12.0");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package org.apache.commons.lang3.text;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -56,7 +56,7 @@ public void testCharSetMatcher_charArray() {
assertEquals(0, matcher.isMatch(BUFFER2, 5));
assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher());
assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher((char[]) null));
assertTrue(StrMatcher.charSetMatcher("a".toCharArray()) instanceof StrMatcher.CharMatcher);
assertInstanceOf(StrMatcher.CharMatcher.class, StrMatcher.charSetMatcher("a".toCharArray()));
}

@Test
Expand All @@ -70,7 +70,7 @@ public void testCharSetMatcher_String() {
assertEquals(0, matcher.isMatch(BUFFER2, 5));
assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher(""));
assertSame(StrMatcher.noneMatcher(), StrMatcher.charSetMatcher((String) null));
assertTrue(StrMatcher.charSetMatcher("a") instanceof StrMatcher.CharMatcher);
assertInstanceOf(StrMatcher.CharMatcher.class, StrMatcher.charSetMatcher("a"));
}

@Test
Expand Down
Loading

0 comments on commit 2a208cf

Please sign in to comment.