Skip to content

Commit

Permalink
Use Assertions.assertInstanceOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 1, 2024
1 parent 9cac8b5 commit a9bb7e9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/test/java/org/apache/commons/lang3/ArrayUtilsTest.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.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
Expand Down Expand Up @@ -115,7 +116,7 @@ public void testArrayCreationWithDifferentTypes() {
@Test
public void testArrayCreationWithGeneralReturnType() {
final Object obj = ArrayUtils.toArray("foo", "bar");
assertTrue(obj instanceof String[]);
assertInstanceOf(String[].class, obj);
}

@Test
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/org/apache/commons/lang3/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.lang3;

import static org.junit.jupiter.api.Assertions.assertEquals;
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.assertSame;
Expand Down Expand Up @@ -543,7 +544,7 @@ public void testAsCallable() {
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, callable::call);
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final FailureOnOddInvocations instance;
try {
Expand Down Expand Up @@ -585,7 +586,7 @@ public void testAsRunnable() {
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, runnable::run);
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());

// Even invocations, should not throw an exception
Expand All @@ -600,7 +601,7 @@ public void testAsSupplier() {
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, supplier::get);
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
assertNotNull(supplier.get());
}
Expand Down Expand Up @@ -669,7 +670,7 @@ public void testBiPredicate() {
() -> predicate.test(null, null));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final boolean instance = predicate.test(null, null);
assertNotNull(instance);
Expand All @@ -682,7 +683,7 @@ public void testCallable() {
() -> Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
assertNotNull(instance);
Expand Down Expand Up @@ -729,7 +730,7 @@ public void testGetFromSupplier() {
() -> Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final FailureOnOddInvocations instance = Functions.call(FailureOnOddInvocations::new);
assertNotNull(instance);
Expand Down Expand Up @@ -771,7 +772,7 @@ public void testPredicate() {
() -> predicate.test(null));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final boolean instance = predicate.test(null);
assertNotNull(instance);
Expand All @@ -784,7 +785,7 @@ public void testRunnable() {
() -> Functions.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());

// Even invocations, should not throw an exception
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.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void setUp() {
public void testClone() {
final Object test = SerializationUtils.clone(iMap);
assertNotNull(test);
assertTrue(test instanceof HashMap<?, ?>);
assertInstanceOf(HashMap.class, test);
assertNotSame(test, iMap);
final HashMap<?, ?> testMap = (HashMap<?, ?>) test;
assertEquals(iString, testMap.get("FOO"));
Expand Down Expand Up @@ -117,7 +118,7 @@ public void testDeserializeBytes() throws Exception {

final Object test = SerializationUtils.deserialize(streamReal.toByteArray());
assertNotNull(test);
assertTrue(test instanceof HashMap<?, ?>);
assertInstanceOf(HashMap.class, test);
assertNotSame(test, iMap);
final HashMap<?, ?> testMap = (HashMap<?, ?>) test;
assertEquals(iString, testMap.get("FOO"));
Expand Down Expand Up @@ -172,7 +173,7 @@ public void testDeserializeStream() throws Exception {
final ByteArrayInputStream inTest = new ByteArrayInputStream(streamReal.toByteArray());
final Object test = SerializationUtils.deserialize(inTest);
assertNotNull(test);
assertTrue(test instanceof HashMap<?, ?>);
assertInstanceOf(HashMap.class, test);
assertNotSame(test, iMap);
final HashMap<?, ?> testMap = (HashMap<?, ?>) test;
assertEquals(iString, testMap.get("FOO"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.apache.commons.lang3.event;

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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
Expand Down Expand Up @@ -248,7 +248,7 @@ public void testThrowingListener() {
final Exception e = assertThrows(UndeclaredThrowableException.class,
() -> listenerSupport.fire().vetoableChange(new PropertyChangeEvent(new Date(), "Day", 0, 1)));
final Throwable rootCause = ExceptionUtils.getRootCause(e);
assertTrue(rootCause instanceof PropertyVetoException);
assertInstanceOf(PropertyVetoException.class, rootCause);
assertEquals(vetoLimit + 1, count.get());
}

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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand Down Expand Up @@ -550,7 +551,7 @@ public void testAsCallable() {
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, callable::call);
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final FailureOnOddInvocations instance;
try {
Expand Down Expand Up @@ -590,7 +591,7 @@ public void testAsRunnable() {
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, runnable::run);
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());

// Even invocations, should not throw an exception
Expand All @@ -605,7 +606,7 @@ public void testAsSupplier() {
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, supplier::get);
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
assertNotNull(supplier.get());
}
Expand Down Expand Up @@ -719,7 +720,7 @@ public void testBiPredicate() {
() -> predicate.test(null, null));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
assertTrue(predicate.test(null, null));
}
Expand Down Expand Up @@ -765,7 +766,7 @@ public void testCallable() {
() -> Failable.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final FailureOnOddInvocations instance = Failable.call(FailureOnOddInvocations::new);
assertNotNull(instance);
Expand Down Expand Up @@ -1195,7 +1196,7 @@ public void testGetFromSupplier() {
() -> Failable.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final FailureOnOddInvocations instance = Failable.call(FailureOnOddInvocations::new);
assertNotNull(instance);
Expand Down Expand Up @@ -1444,7 +1445,7 @@ public void testPredicate() {
() -> predicate.test(null));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());
final boolean instance = predicate.test(null);
assertNotNull(instance);
Expand Down Expand Up @@ -1487,7 +1488,7 @@ public void testRunnable() {
() -> Failable.run(FailureOnOddInvocations::new));
final Throwable cause = e.getCause();
assertNotNull(cause);
assertTrue(cause instanceof SomeException);
assertInstanceOf(SomeException.class, cause);
assertEquals("Odd Invocation: 1", cause.getMessage());

// Even invocations, should not throw an exception
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/apache/commons/lang3/tuple/PairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.lang3.tuple;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -177,11 +178,11 @@ public void testPairOfMapEntry() {
@Test
public void testPairOfObjects() {
final Pair<Integer, String> pair = Pair.of(0, "foo");
assertTrue(pair instanceof ImmutablePair<?, ?>);
assertInstanceOf(ImmutablePair.class, pair);
assertEquals(0, ((ImmutablePair<Integer, String>) pair).left.intValue());
assertEquals("foo", ((ImmutablePair<Integer, String>) pair).right);
final Pair<Object, String> pair2 = Pair.of(null, "bar");
assertTrue(pair2 instanceof ImmutablePair<?, ?>);
assertInstanceOf(ImmutablePair.class, pair2);
assertNull(((ImmutablePair<Object, String>) pair2).left);
assertEquals("bar", ((ImmutablePair<Object, String>) pair2).right);
final Pair<?, ?> pair3 = Pair.of(null, null);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/apache/commons/lang3/tuple/TripleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.lang3.tuple;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -138,12 +139,12 @@ public void testToStringCustom() {
@Test
public void testTripleOf() {
final Triple<Integer, String, Boolean> triple = Triple.of(0, "foo", Boolean.TRUE);
assertTrue(triple instanceof ImmutableTriple<?, ?, ?>);
assertInstanceOf(ImmutableTriple.class, triple);
assertEquals(0, ((ImmutableTriple<Integer, String, Boolean>) triple).left.intValue());
assertEquals("foo", ((ImmutableTriple<Integer, String, Boolean>) triple).middle);
assertEquals(Boolean.TRUE, ((ImmutableTriple<Integer, String, Boolean>) triple).right);
final Triple<Object, String, Long> triple2 = Triple.of(null, "bar", Long.valueOf(200L));
assertTrue(triple2 instanceof ImmutableTriple<?, ?, ?>);
assertInstanceOf(ImmutableTriple.class, triple2);
assertNull(((ImmutableTriple<Object, String, Long>) triple2).left);
assertEquals("bar", ((ImmutableTriple<Object, String, Long>) triple2).middle);
assertEquals(Long.valueOf(200L), ((ImmutableTriple<Object, String, Long>) triple2).right);
Expand Down

0 comments on commit a9bb7e9

Please sign in to comment.