test/jdk/java/net/httpclient/http2/java.net.http/java/net/http/internal/hpack/TestHelper.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56091 aedd6133e7a0
child 56093 22d94c4a3641
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
     1 /*
       
     2  * Copyright (c) 2014, 2017, 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 package java.net.http.internal.hpack;
       
    24 
       
    25 import org.testng.annotations.Test;
       
    26 
       
    27 import java.util.Objects;
       
    28 import java.util.Random;
       
    29 
       
    30 public final class TestHelper {
       
    31 
       
    32     public static Random newRandom() {
       
    33         long seed = Long.getLong("jdk.test.lib.random.seed", System.currentTimeMillis());
       
    34         System.out.println("new java.util.Random(" + seed + ")");
       
    35         return new Random(seed);
       
    36     }
       
    37 
       
    38     public static <T extends Throwable> T assertVoidThrows(Class<T> clazz, Block<?> code) {
       
    39         return assertThrows(clazz, () -> {
       
    40             code.run();
       
    41             return null;
       
    42         });
       
    43     }
       
    44 
       
    45     public static <T extends Throwable> T assertThrows(Class<T> clazz, ReturningBlock<?> code) {
       
    46         Objects.requireNonNull(clazz, "clazz == null");
       
    47         Objects.requireNonNull(code, "code == null");
       
    48         try {
       
    49             code.run();
       
    50         } catch (Throwable t) {
       
    51             if (clazz.isInstance(t)) {
       
    52                 return clazz.cast(t);
       
    53             }
       
    54             throw new AssertionError("Expected to catch exception of type "
       
    55                     + clazz.getCanonicalName() + ", instead caught "
       
    56                     + t.getClass().getCanonicalName(), t);
       
    57 
       
    58         }
       
    59         throw new AssertionError(
       
    60                 "Expected to catch exception of type " + clazz.getCanonicalName()
       
    61                         + ", but caught nothing");
       
    62     }
       
    63 
       
    64     public static <T> T assertDoesNotThrow(ReturningBlock<T> code) {
       
    65         Objects.requireNonNull(code, "code == null");
       
    66         try {
       
    67             return code.run();
       
    68         } catch (Throwable t) {
       
    69             throw new AssertionError(
       
    70                     "Expected code block to exit normally, instead " +
       
    71                             "caught " + t.getClass().getCanonicalName(), t);
       
    72         }
       
    73     }
       
    74 
       
    75     public static void assertVoidDoesNotThrow(Block<?> code) {
       
    76         Objects.requireNonNull(code, "code == null");
       
    77         try {
       
    78             code.run();
       
    79         } catch (Throwable t) {
       
    80             throw new AssertionError(
       
    81                     "Expected code block to exit normally, instead " +
       
    82                             "caught " + t.getClass().getCanonicalName(), t);
       
    83         }
       
    84     }
       
    85 
       
    86 
       
    87     public static void assertExceptionMessageContains(Throwable t,
       
    88                                                       CharSequence firstSubsequence,
       
    89                                                       CharSequence... others) {
       
    90         assertCharSequenceContains(t.getMessage(), firstSubsequence, others);
       
    91     }
       
    92 
       
    93     public static void assertCharSequenceContains(CharSequence s,
       
    94                                                   CharSequence firstSubsequence,
       
    95                                                   CharSequence... others) {
       
    96         if (s == null) {
       
    97             throw new NullPointerException("Exception message is null");
       
    98         }
       
    99         String str = s.toString();
       
   100         String missing = null;
       
   101         if (!str.contains(firstSubsequence.toString())) {
       
   102             missing = firstSubsequence.toString();
       
   103         } else {
       
   104             for (CharSequence o : others) {
       
   105                 if (!str.contains(o.toString())) {
       
   106                     missing = o.toString();
       
   107                     break;
       
   108                 }
       
   109             }
       
   110         }
       
   111         if (missing != null) {
       
   112             throw new AssertionError("CharSequence '" + s + "'" + " does not "
       
   113                     + "contain subsequence '" + missing + "'");
       
   114         }
       
   115     }
       
   116 
       
   117     public interface ReturningBlock<T> {
       
   118         T run() throws Throwable;
       
   119     }
       
   120 
       
   121     public interface Block<T> {
       
   122         void run() throws Throwable;
       
   123     }
       
   124 
       
   125     // tests
       
   126 
       
   127     @Test
       
   128     public void assertThrows() {
       
   129         assertThrows(NullPointerException.class, () -> ((Object) null).toString());
       
   130     }
       
   131 
       
   132     @Test
       
   133     public void assertThrowsWrongType() {
       
   134         try {
       
   135             assertThrows(IllegalArgumentException.class, () -> ((Object) null).toString());
       
   136         } catch (AssertionError e) {
       
   137             Throwable cause = e.getCause();
       
   138             String message = e.getMessage();
       
   139             if (cause != null
       
   140                     && cause instanceof NullPointerException
       
   141                     && message != null
       
   142                     && message.contains("instead caught")) {
       
   143                 return;
       
   144             }
       
   145         }
       
   146         throw new AssertionError();
       
   147     }
       
   148 
       
   149     @Test
       
   150     public void assertThrowsNoneCaught() {
       
   151         try {
       
   152             assertThrows(IllegalArgumentException.class, () -> null);
       
   153         } catch (AssertionError e) {
       
   154             Throwable cause = e.getCause();
       
   155             String message = e.getMessage();
       
   156             if (cause == null
       
   157                     && message != null
       
   158                     && message.contains("but caught nothing")) {
       
   159                 return;
       
   160             }
       
   161         }
       
   162         throw new AssertionError();
       
   163     }
       
   164 }