Skip to content

Commit

Permalink
Use more specialized JUnit assertions
Browse files Browse the repository at this point in the history
In particular, use `assertInstanceOf` instead of `assertTrue` on the
result of an `instanceof` check.  If the assertion fails,
`assertInstanceOf` will produce a more informative error message.
  • Loading branch information
liblit committed Nov 16, 2024
1 parent 7be9fb5 commit cb4d05c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static com.ibm.wala.ipa.slicer.SlicerUtil.dumpSlice;
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.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -258,7 +259,7 @@ public void testArrayLiteral1() throws IllegalArgumentException, CancelException

CGNode node = cg.getNodes(mref).iterator().next();
SSAInstruction s = node.getIR().getInstructions()[2];
assertTrue(s instanceof SSANewInstruction, "Did not find new array instruction.");
assertInstanceOf(SSANewInstruction.class, s, "Did not find new array instruction.");
assertTrue(((SSANewInstruction) s).getNewSite().getDeclaredType().isArrayType(), "");
}),
true,
Expand Down Expand Up @@ -304,8 +305,9 @@ public void testArrayLiteral2() throws IllegalArgumentException, CancelException
{
final SymbolTable symbolTable = node.getIR().getSymbolTable();
for (int i = 4; i <= 7; i++) {
assertTrue(
instructions[i] instanceof SSAArrayStoreInstruction,
assertInstanceOf(
SSAArrayStoreInstruction.class,
instructions[i],
"Expected only array stores.");

SSAArrayStoreInstruction as = (SSAArrayStoreInstruction) instructions[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -753,7 +754,7 @@ public void testReflect24()

for (InstanceKey mappedObject : pts) {
// the type corresponding to the 0th parameter should be Helper
assertTrue(mappedObject instanceof ConstantKey);
assertInstanceOf(ConstantKey.class, mappedObject);
assertEquals(((ConstantKey<?>) mappedObject).getValue(), helperClass);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.ibm.wala.classLoader.FieldImpl;
import com.ibm.wala.classLoader.IClass;
Expand Down Expand Up @@ -265,7 +265,7 @@ private void testClassAnnotations(
throws InvalidClassFileException {
IClass classUnderTest = cha.lookupClass(typeUnderTest);
assertNotNull(classUnderTest, typeUnderTest.toString() + " not found");
assertTrue(classUnderTest instanceof ShrikeClass, classUnderTest + " must be BytecodeClass");
assertInstanceOf(ShrikeClass.class, classUnderTest, classUnderTest + " must be BytecodeClass");
ShrikeClass bcClassUnderTest = (ShrikeClass) classUnderTest;

Collection<TypeAnnotation> runtimeInvisibleAnnotations =
Expand All @@ -286,8 +286,8 @@ private void testMethodAnnotations(
throws InvalidClassFileException {
IMethod methodUnderTest = cha.resolveMethod(methodRefUnderTest);
assertNotNull(methodUnderTest, methodRefUnderTest.toString() + " not found");
assertTrue(
methodUnderTest instanceof ShrikeCTMethod, methodUnderTest + " must be ShrikeCTMethod");
assertInstanceOf(
ShrikeCTMethod.class, methodUnderTest, methodUnderTest + " must be ShrikeCTMethod");
ShrikeCTMethod bcMethodUnderTest = (ShrikeCTMethod) methodUnderTest;

Collection<TypeAnnotation> runtimeInvisibleAnnotations = HashSetFactory.make();
Expand All @@ -311,7 +311,7 @@ private void testFieldAnnotations(
Collection<TypeAnnotation> expectedAnnotations) {
IClass classUnderTest = cha.lookupClass(typeUnderTest);
assertNotNull(classUnderTest, typeUnderTest.toString() + " not found");
assertTrue(classUnderTest instanceof ShrikeClass, classUnderTest + " must be BytecodeClass");
assertInstanceOf(ShrikeClass.class, classUnderTest, classUnderTest + " must be BytecodeClass");
ShrikeClass bcClassUnderTest = (ShrikeClass) classUnderTest;

final Atom fieldName = Atom.findOrCreateUnicodeAtom(fieldNameStr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ibm.wala.core.tests.shrike;

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

Expand Down Expand Up @@ -196,7 +197,7 @@ private Object getConstantInstructionValue(String signature, int index, String t
IInstruction instruction = instructions[index];

// Check that the instruction type has not been changed
assertTrue(instruction instanceof ConstantInstruction);
assertInstanceOf(ConstantInstruction.class, instruction);

// The type type should be the same as well
ConstantInstruction instruction2 = (ConstantInstruction) instruction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package com.ibm.wala.core.tests.ir;

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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -160,7 +161,8 @@ private void testClassAnnotations(
throws InvalidClassFileException {
IClass classUnderTest = cha.lookupClass(typeUnderTest);
assertNotNull(classUnderTest, typeUnderTest.toString() + " not found");
assertTrue(classUnderTest instanceof BytecodeClass, classUnderTest + " must be BytecodeClass");
assertInstanceOf(
BytecodeClass.class, classUnderTest, classUnderTest + " must be BytecodeClass");
BytecodeClass<?> bcClassUnderTest = (BytecodeClass<?>) classUnderTest;

Collection<Annotation> runtimeInvisibleAnnotations = bcClassUnderTest.getAnnotations(true);
Expand Down Expand Up @@ -191,8 +193,8 @@ public void testClassAnnotations3() throws Exception {

IMethod methodUnderTest = cha.resolveMethod(methodRefUnderTest);
assertNotNull(methodUnderTest, methodRefUnderTest + " not found");
assertTrue(
methodUnderTest instanceof IBytecodeMethod, methodUnderTest + " must be IBytecodeMethod");
assertInstanceOf(
IBytecodeMethod.class, methodUnderTest, methodUnderTest + " must be IBytecodeMethod");
IBytecodeMethod<IInstruction> bcMethodUnderTest =
(IBytecodeMethod<IInstruction>) methodUnderTest;

Expand Down Expand Up @@ -301,8 +303,8 @@ protected void checkParameterAnnots(

IMethod methodUnderTest = cha.resolveMethod(methodRefUnderTest);
assertNotNull(methodUnderTest, methodRefUnderTest + " not found");
assertTrue(
methodUnderTest instanceof IBytecodeMethod, methodUnderTest + " must be bytecode method");
assertInstanceOf(
IBytecodeMethod.class, methodUnderTest, methodUnderTest + " must be bytecode method");
IBytecodeMethod<?> IBytecodeMethodUnderTest = (IBytecodeMethod<?>) methodUnderTest;

Collection<Annotation>[] parameterAnnotations =
Expand Down

0 comments on commit cb4d05c

Please sign in to comment.