HADOOP-12472. Make GenericTestUtils.assertExceptionContains robust. Contributed 
by Steve Loughran.

(cherry picked from commit a01a209fbed33b2ecaf9e736631e64abefae01aa)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/00f7d729
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/00f7d729
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/00f7d729

Branch: refs/heads/branch-2.7
Commit: 00f7d729c68bd27d0d2d2f635e8c25c60fe0bd73
Parents: 98f9d6f
Author: Jing Zhao <ji...@apache.org>
Authored: Mon Oct 26 14:03:15 2015 -0700
Committer: Konstantin V Shvachko <s...@apache.org>
Committed: Tue Mar 6 18:20:25 2018 -0800

----------------------------------------------------------------------
 hadoop-common-project/hadoop-common/CHANGES.txt |  6 ++
 .../apache/hadoop/test/GenericTestUtils.java    | 30 ++++++--
 .../hadoop/test/TestGenericTestUtils.java       | 78 ++++++++++++++++++++
 3 files changed, 109 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/00f7d729/hadoop-common-project/hadoop-common/CHANGES.txt
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt 
b/hadoop-common-project/hadoop-common/CHANGES.txt
index 3007016..1082bf6 100644
--- a/hadoop-common-project/hadoop-common/CHANGES.txt
+++ b/hadoop-common-project/hadoop-common/CHANGES.txt
@@ -30,6 +30,9 @@ Release 2.7.6 - UNRELEASED
     HADOOP-15279. Increase maven heap size recommendations.
     (Allen Wittenauer via aajisaka)
 
+    HADOOP-12472. Make GenericTestUtils.assertExceptionContains robust.
+    (Steve Loughran via jing9)
+
   OPTIMIZATIONS
 
   BUG FIXES
@@ -132,6 +135,9 @@ Release 2.7.4 - 2017-08-04
 
     HADOOP-14440. Add metrics for connections dropped. (Eric Badger via kihwal)
 
+    HADOOP-12472. Make GenericTestUtils.assertExceptionContains robust.
+    (Steve Loughran via jing9)
+
   OPTIMIZATIONS
 
     HADOOP-14138. Remove S3A ref from META-INF service discovery, rely on

http://git-wip-us.apache.org/repos/asf/hadoop/blob/00f7d729/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
index 84f79e6..39d8402 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
@@ -141,12 +141,32 @@ public abstract class GenericTestUtils {
         Joiner.on(",").join(expectedSet),
         Joiner.on(",").join(found));
   }
-  
+
+  protected static String E_NULL_THROWABLE = "Null Throwable";
+  protected static String E_NULL_THROWABLE_STRING =
+      "Null Throwable.toString() value";
+  protected static String E_UNEXPECTED_EXCEPTION =
+      "but got unexpected exception";
+
+  /**
+   * Assert that an exception's <code>toString()</code> value
+   * contained the expected text.
+   * @param string expected string
+   * @param t thrown exception
+   * @throws AssertionError if the expected string is not found
+   */
   public static void assertExceptionContains(String string, Throwable t) {
-    String msg = t.getMessage();
-    Assert.assertTrue(
-        "Expected to find '" + string + "' but got unexpected exception:"
-        + StringUtils.stringifyException(t), msg.contains(string));
+    Assert.assertNotNull(E_NULL_THROWABLE, t);
+    String msg = t.toString();
+    if (msg == null) {
+      throw new AssertionError(E_NULL_THROWABLE_STRING, t);
+    }
+    if (!msg.contains(string)) {
+      throw new AssertionError("Expected to find '" + string + "' "
+          + E_UNEXPECTED_EXCEPTION + ":"
+          + StringUtils.stringifyException(t),
+          t);
+    }
   }  
 
   public static void waitFor(Supplier<Boolean> check,

http://git-wip-us.apache.org/repos/asf/hadoop/blob/00f7d729/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
new file mode 100644
index 0000000..8a7b5f6
--- /dev/null
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.test;
+
+import org.junit.Test;
+
+public class TestGenericTestUtils extends GenericTestUtils {
+
+  @Test
+  public void testAssertExceptionContainsNullEx() throws Throwable {
+    try {
+      assertExceptionContains("", null);
+    } catch (AssertionError e) {
+      if (!e.toString().contains(E_NULL_THROWABLE)) {
+        throw e;
+      }
+    }
+  }
+
+  @Test
+  public void testAssertExceptionContainsNullString() throws Throwable {
+    try {
+      assertExceptionContains("", new BrokenException());
+    } catch (AssertionError e) {
+      if (!e.toString().contains(E_NULL_THROWABLE_STRING)) {
+        throw e;
+      }
+    }
+  }
+
+  @Test
+  public void testAssertExceptionContainsWrongText() throws Throwable {
+    try {
+      assertExceptionContains("Expected", new Exception("(actual)"));
+    } catch (AssertionError e) {
+      String s = e.toString();
+      if (!s.contains(E_UNEXPECTED_EXCEPTION)
+          || !s.contains("(actual)") ) {
+        throw e;
+      }
+      if (e.getCause() == null) {
+        throw new AssertionError("No nested cause in assertion", e);
+      }
+    }
+  }
+
+  @Test
+  public void testAssertExceptionContainsWorking() throws Throwable {
+    assertExceptionContains("Expected", new Exception("Expected"));
+  }
+
+  private static class BrokenException extends Exception {
+    public BrokenException() {
+    }
+
+    @Override
+    public String toString() {
+      return null;
+    }
+  }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to