You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2018/10/13 19:11:13 UTC

[09/14] [lang] Use assertTrue/assertFalse instead of reimplementing them

Use assertTrue/assertFalse instead of reimplementing them

Use assertTrue and assertFalse instead of reimplemeting their logic by
having an if statement conditionalling call fail.


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/e00e871d
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/e00e871d
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/e00e871d

Branch: refs/heads/master
Commit: e00e871d1d57ac76680087876b36764c4f8b28f8
Parents: 3387734
Author: Allon Mureinik <mu...@gmail.com>
Authored: Sat Oct 13 19:38:01 2018 +0300
Committer: pascalschumacher <pa...@gmx.net>
Committed: Sat Oct 13 21:09:37 2018 +0200

----------------------------------------------------------------------
 .../apache/commons/lang3/LocaleUtilsTest.java   | 11 ++------
 .../commons/lang3/RandomStringUtilsTest.java    | 29 ++++++++------------
 .../apache/commons/lang3/StringUtilsTest.java   | 11 +++-----
 .../lang3/concurrent/TimedSemaphoreTest.java    |  5 +---
 .../commons/lang3/math/NumberUtilsTest.java     | 15 ++++------
 .../lang3/time/FastDateParserSDFTest.java       | 14 ++++------
 .../commons/lang3/time/FastDateParserTest.java  |  9 ++----
 7 files changed, 32 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
index c445e6e..d428162 100644
--- a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
@@ -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.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 @@ public class LocaleUtilsTest  {
                     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 @@ public class LocaleUtilsTest  {
                     break;
                 }
             }
-            if (!found) {
-                fail("Could not find language: " + country
-                        + " for country: " + language);
-            }
+            assertTrue(found, "Could not find language: " + country + " for country: " + language);
         }
         assertUnmodifiableCollection(list);
     }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
index 79d163d..dd7aff5 100644
--- a/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/RandomStringUtilsTest.java
@@ -200,10 +200,9 @@ public class RandomStringUtilsTest {
             }
         }
         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 class RandomStringUtilsTest {
             }
         }
         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 class RandomStringUtilsTest {
             }
         }
         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 class RandomStringUtilsTest {
             }
         }
         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");
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 802ed59..16a4f8f 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -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.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 class StringUtilsTest {
                 // 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");
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java b/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java
index 66ad0e2..281bdf0 100644
--- a/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java
+++ b/src/test/java/org/apache/commons/lang3/concurrent/TimedSemaphoreTest.java
@@ -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.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 class TimedSemaphoreTest {
         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();
     }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
index a935931..a819c3d 100644
--- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
@@ -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.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 class NumberUtilsTest {
     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 class NumberUtilsTest {
     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

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/time/FastDateParserSDFTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserSDFTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserSDFTest.java
index 0136ecd..e948a24 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateParserSDFTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserSDFTest.java
@@ -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 @@ public class FastDateParserSDFTest {
         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) {

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e00e871d/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
index 16efdc0..a7647c2 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
@@ -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.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 @@ public class FastDateParserTest {
             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 @@ public class FastDateParserTest {
         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) {