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.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; 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.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -394,10 +393,7 @@ public class LocaleUtilsTest {
break; break;
} }
} }
if (!found) { assertTrue(found, "Could not find language: " + language + " for country: " + country);
fail("Could not find language: " + language
+ " for country: " + country);
}
} }
assertUnmodifiableCollection(list); assertUnmodifiableCollection(list);
} }
@ -445,10 +441,7 @@ public class LocaleUtilsTest {
break; break;
} }
} }
if (!found) { assertTrue(found, "Could not find language: " + country + " for country: " + language);
fail("Could not find language: " + country
+ " for country: " + language);
}
} }
assertUnmodifiableCollection(list); assertUnmodifiableCollection(list);
} }

View File

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

View File

@ -24,7 +24,6 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@ -2849,18 +2848,16 @@ public class StringUtilsTest {
// don't actively test for that. // don't actively test for that.
final Class<?>[] params = m.getParameterTypes(); final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) { if (params.length > 0 && (params[0] == CharSequence.class || params[0] == CharSequence[].class)) {
if (!ArrayUtils.contains(excludeMethods, methodStr)) { assertTrue(!ArrayUtils.contains(excludeMethods, methodStr),
fail("The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence"); "The method \"" + methodStr + "\" appears to be mutable in spirit and therefore must not accept a CharSequence");
}
} }
} else { } else {
// Assume this is immutable in spirit and ensure the first parameter is not String. // Assume this is immutable in spirit and ensure the first parameter is not String.
// As above, it may be something other than CharSequence. // As above, it may be something other than CharSequence.
final Class<?>[] params = m.getParameterTypes(); final Class<?>[] params = m.getParameterTypes();
if (params.length > 0 && (params[0] == String.class || params[0] == String[].class)) { if (params.length > 0 && (params[0] == String.class || params[0] == String[].class)) {
if (!ArrayUtils.contains(excludeMethods, methodStr)) { assertTrue(ArrayUtils.contains(excludeMethods, methodStr),
fail("The method \"" + methodStr + "\" appears to be immutable in spirit and therefore must not accept a String"); "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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; 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.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -105,9 +104,7 @@ public class TimedSemaphoreTest {
int count = 0; int count = 0;
do { do {
Thread.sleep(PERIOD); Thread.sleep(PERIOD);
if (count++ > trials) { assertFalse(count++ > trials, "endOfPeriod() not called!");
fail("endOfPeriod() not called!");
}
} while (semaphore.getPeriodEnds() <= 0); } while (semaphore.getPeriodEnds() <= 0);
semaphore.shutdown(); semaphore.shutdown();
} }

View File

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

View File

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

View File

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