Use assertTrue/assertFalse instead of reimplementing them

Use assertTrue and assertFalse instead of reimplemeting their logic by
having an if statement conditionalling call fail.
This commit is contained in:
Allon Mureinik 2018-10-13 19:38:01 +03:00 committed by pascalschumacher
parent 3387734b4f
commit e00e871d1d
7 changed files with 32 additions and 62 deletions

View File

@ -24,7 +24,6 @@
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 static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@ -394,10 +393,7 @@ private static void assertLanguageByCountry(final String country, final String[]
break;
}
}
if (!found) {
fail("Could not find language: " + language
+ " for country: " + country);
}
assertTrue(found, "Could not find language: " + language + " for country: " + country);
}
assertUnmodifiableCollection(list);
}
@ -445,10 +441,7 @@ private static void assertCountriesByLanguage(final String language, final Strin
break;
}
}
if (!found) {
fail("Could not find language: " + country
+ " for country: " + language);
}
assertTrue(found, "Could not find language: " + country + " for country: " + language);
}
assertUnmodifiableCollection(list);
}

View File

@ -200,10 +200,9 @@ public void testRandomAlphaNumeric() {
}
}
for (int i = 0; i < testChars.length; i++) {
if (!found[i]) {
fail("alphanumeric character not generated in 1000 attempts: "
+ testChars[i] +" -- repeated failures indicate a problem ");
}
assertTrue(found[i],
"alphanumeric character not generated in 1000 attempts: " +
testChars[i] + " -- repeated failures indicate a problem ");
}
}
@ -224,10 +223,9 @@ public void testRandomNumeric() {
}
}
for (int i = 0; i < testChars.length; i++) {
if (!found[i]) {
fail("digit not generated in 1000 attempts: "
+ testChars[i] +" -- repeated failures indicate a problem ");
}
assertTrue(found[i],
"digit not generated in 1000 attempts: " + testChars[i] +
" -- repeated failures indicate a problem ");
}
}
@ -248,10 +246,9 @@ public void testRandomAlphabetic() {
}
}
for (int i = 0; i < testChars.length; i++) {
if (!found[i]) {
fail("alphanumeric character not generated in 1000 attempts: "
+ testChars[i] +" -- repeated failures indicate a problem ");
}
assertTrue(found[i],
"alphanumeric character not generated in 1000 attempts: " + testChars[i] +
" -- repeated failures indicate a problem ");
}
}
@ -272,11 +269,9 @@ public void testRandomAscii() {
}
}
for (int i = 0; i < testChars.length; i++) {
if (!found[i]) {
fail("ascii character not generated in 1000 attempts: "
+ (int) testChars[i] +
" -- repeated failures indicate a problem");
}
assertTrue(found[i],
"ascii character not generated in 1000 attempts: " + (int) testChars[i] +
" -- repeated failures indicate a problem");
}
}

View File

@ -24,7 +24,6 @@
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 static org.junit.jupiter.api.Assertions.fail;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
@ -2849,18 +2848,16 @@ public void testStringUtilsCharSequenceContract() {
// don't actively test for that.
final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) {
if (!ArrayUtils.contains(excludeMethods, methodStr)) {
fail("The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence");
}
assertTrue(!ArrayUtils.contains(excludeMethods, methodStr),
"The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence");
}
} else {
// Assume this is immutable in spirit and ensure the first parameter is not String.
// As above, it may be something other than CharSequence.
final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == String.class || params[0] == String[].class)) {
if (!ArrayUtils.contains(excludeMethods, methodStr)) {
fail("The method \"" + methodStr + "\" appears to be immutable in spirit and therefore must not accept a String");
}
assertTrue(ArrayUtils.contains(excludeMethods, methodStr),
"The method \"" + methodStr + "\" appears to be immutable in spirit and therefore must not accept a String");
}
}
}

View File

@ -21,7 +21,6 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
@ -105,9 +104,7 @@ public void testStartTimer() throws InterruptedException {
int count = 0;
do {
Thread.sleep(PERIOD);
if (count++ > trials) {
fail("endOfPeriod() not called!");
}
assertFalse(count++ > trials, "endOfPeriod() not called!");
} while (semaphore.getPeriodEnds() <= 0);
semaphore.shutdown();
}

View File

@ -21,7 +21,6 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@ -1395,10 +1394,9 @@ public void testLANG1252() {
private void compareIsCreatableWithCreateNumber(final String val, final boolean expected) {
final boolean isValid = NumberUtils.isCreatable(val);
final boolean canCreate = checkCreateNumber(val);
if (isValid == expected && canCreate == expected) {
return;
}
fail("Expecting "+ expected + " for isCreatable/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
assertTrue(
isValid == expected && canCreate == expected,
"Expecting " + expected + " for isCreatable/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
}
/**
@ -1501,10 +1499,9 @@ public void testIsNumberLANG1385() {
private void compareIsNumberWithCreateNumber(final String val, final boolean expected) {
final boolean isValid = NumberUtils.isCreatable(val);
final boolean canCreate = checkCreateNumber(val);
if (isValid == expected && canCreate == expected) {
return;
}
fail("Expecting "+ expected + " for isCreatable/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
assertTrue(
isValid == expected && canCreate == expected,
"Expecting "+ expected + " for isCreatable/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
}
@Test

View File

@ -17,9 +17,9 @@
package org.apache.commons.lang3.time;
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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.text.ParseException;
import java.text.ParsePosition;
@ -163,15 +163,11 @@ private void checkParse(final String formattedDate, String format, Locale locale
Class<?> fdfE = null;
try {
actualTime = fdf.parse(formattedDate);
if (!valid) {
// failure in test
fail("Expected FDP parse to fail, but got " + actualTime);
}
// failure in test
assertTrue(valid, "Expected FDP parse to fail, but got " + actualTime);
} catch (final ParseException e) {
if (valid) {
// failure in test
fail("Expected FDP parse to succeed, but got " + e);
}
// failure in test
assertFalse(valid, "Expected FDP parse to succeed, but got " + e);
fdfE = e.getClass();
}
if (valid) {

View File

@ -20,7 +20,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.Serializable;
import java.text.ParseException;
@ -444,9 +443,7 @@ private void testSdfAndFdp(final String format, final String date, final boolean
final SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
sdf.setTimeZone(NEW_YORK);
dsdf = sdf.parse(date);
if (shouldFail) {
fail("Expected SDF failure, but got " + dsdf + " for ["+format+","+date+"]");
}
assertFalse(shouldFail, "Expected SDF failure, but got " + dsdf + " for ["+format+","+date+"]");
} catch (final Exception e) {
s = e;
if (!shouldFail) {
@ -457,9 +454,7 @@ private void testSdfAndFdp(final String format, final String date, final boolean
try {
final DateParser fdp = getInstance(format, NEW_YORK, Locale.US);
dfdp = fdp.parse(date);
if (shouldFail) {
fail("Expected FDF failure, but got " + dfdp + " for ["+format+","+date+"]");
}
assertFalse(shouldFail, "Expected FDF failure, but got " + dfdp + " for ["+format+","+date+"]");
} catch (final Exception e) {
f = e;
if (!shouldFail) {