jdk/test/java/net/httpclient/TestKit.java
changeset 37720 45cd7cc65382
child 42460 7133f144981a
equal deleted inserted replaced
37719:add11bc0e6e2 37720:45cd7cc65382
       
     1 /*
       
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.util.regex.Pattern;
       
    25 
       
    26 import static java.util.Objects.requireNonNull;
       
    27 
       
    28 //
       
    29 // A set of testing utility functions
       
    30 //
       
    31 public final class TestKit {
       
    32 
       
    33     private TestKit() { }
       
    34 
       
    35     public static void assertNotThrows(ThrowingProcedure code) {
       
    36         requireNonNull(code, "code");
       
    37         assertNotThrows(() -> {
       
    38             code.run();
       
    39             return null;
       
    40         });
       
    41     }
       
    42 
       
    43     public static <V> V assertNotThrows(ThrowingFunction<V> code) {
       
    44         requireNonNull(code, "code");
       
    45         try {
       
    46             return code.run();
       
    47         } catch (Throwable t) {
       
    48             throw new RuntimeException("Expected to run normally, but threw "
       
    49                     + t.getClass().getCanonicalName(), t);
       
    50         }
       
    51     }
       
    52 
       
    53     public static <T extends Throwable> T assertThrows(Class<? extends T> clazz,
       
    54                                                        ThrowingProcedure code) {
       
    55         requireNonNull(clazz, "clazz");
       
    56         requireNonNull(code, "code");
       
    57         try {
       
    58             code.run();
       
    59         } catch (Throwable t) {
       
    60             if (clazz.isInstance(t)) {
       
    61                 return clazz.cast(t);
       
    62             }
       
    63             throw new RuntimeException("Expected to catch an exception of type "
       
    64                     + clazz.getCanonicalName() + ", but caught "
       
    65                     + t.getClass().getCanonicalName(), t);
       
    66 
       
    67         }
       
    68         throw new RuntimeException("Expected to catch an exception of type "
       
    69                 + clazz.getCanonicalName() + ", but caught nothing");
       
    70     }
       
    71 
       
    72     public interface ThrowingProcedure {
       
    73         void run() throws Throwable;
       
    74     }
       
    75 
       
    76     public interface ThrowingFunction<V> {
       
    77         V run() throws Throwable;
       
    78     }
       
    79 
       
    80     // The rationale behind asking for a regex is to not pollute variable names
       
    81     // space in the scope of assertion: if it's something as simple as checking
       
    82     // a message, we can do it inside
       
    83     public static <T extends Throwable> T assertThrows(Class<? extends T> clazz,
       
    84                                                        String messageRegex,
       
    85                                                        ThrowingProcedure code) {
       
    86         requireNonNull(messageRegex, "messagePattern");
       
    87         T t = assertThrows(clazz, code);
       
    88         String m = t.getMessage();
       
    89         if (m == null) {
       
    90             throw new RuntimeException(String.format(
       
    91                     "Expected exception message to match the regex '%s', " +
       
    92                             "but the message was null", messageRegex), t);
       
    93         }
       
    94         if (!Pattern.matches(messageRegex, m)) {
       
    95             throw new RuntimeException(String.format(
       
    96                     "Expected exception message to match the regex '%s', " +
       
    97                             "actual message: %s", messageRegex, m), t);
       
    98         }
       
    99         return t;
       
   100     }
       
   101 }