1 /*
   2  * Copyright (c) 2016, 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 
  24 import org.testng.annotations.Test;
  25 
  26 import java.io.IOException;
  27 import java.util.ArrayList;
  28 import java.util.Arrays;
  29 import java.util.Collections;
  30 import java.util.HashMap;
  31 import java.util.IllegalFormatException;
  32 import java.util.LinkedList;
  33 import java.util.Map;
  34 import java.util.Set;
  35 
  36 import static org.testng.Assert.assertEquals;
  37 import static org.testng.Assert.assertNotNull;
  38 import static org.testng.Assert.assertNull;
  39 import static org.testng.Assert.assertTrue;
  40 
  41 /*
  42  * @test
  43  * @compile TestKit.java
  44  * @run testng TestKitTest
  45  */
  46 public final class TestKitTest {
  47 
  48     @Test
  49     public void testAssertNotThrows() {
  50         Integer integer = TestKit.assertNotThrows(
  51                 () -> TestKit.assertNotThrows(() -> 1)
  52         );
  53         assertEquals(integer, Integer.valueOf(1));
  54 
  55         RuntimeException re = TestKit.assertThrows(
  56                 RuntimeException.class,
  57                 () -> TestKit.assertNotThrows(() -> { throw new IOException(); })
  58         );
  59         assertEquals(re.getMessage(),
  60                 "Expected to run normally, but threw "
  61                         + "java.io.IOException");
  62 
  63         TestKit.assertNotThrows(
  64                 () -> TestKit.assertNotThrows(() -> { })
  65         );
  66 
  67         re = TestKit.assertThrows(
  68                 RuntimeException.class,
  69                 () ->  TestKit.assertNotThrows((TestKit.ThrowingProcedure) () -> { throw new IOException(); })
  70         );
  71         assertEquals(re.getMessage(),
  72                 "Expected to run normally, but threw "
  73                         + "java.io.IOException");
  74     }
  75 
  76     @Test
  77     public void testAssertThrows() {
  78         NullPointerException npe = TestKit.assertThrows(
  79                 NullPointerException.class,
  80                 () -> TestKit.assertThrows(null, null)
  81         );
  82         assertNotNull(npe);
  83         assertTrue(Set.of("clazz", "code").contains(npe.getMessage()), npe.getMessage());
  84 
  85         npe = TestKit.assertThrows(
  86                 NullPointerException.class,
  87                 () -> TestKit.assertThrows(IOException.class, null)
  88         );
  89         assertNotNull(npe);
  90         assertEquals(npe.getMessage(), "code");
  91 
  92         npe = TestKit.assertThrows(
  93                 NullPointerException.class,
  94                 () -> TestKit.assertThrows(null, () -> { })
  95         );
  96         assertEquals(npe.getMessage(), "clazz");
  97 
  98         npe = TestKit.assertThrows(
  99                 NullPointerException.class,
 100                 () -> { throw new NullPointerException(); }
 101         );
 102         assertNotNull(npe);
 103         assertNull(npe.getMessage());
 104         assertEquals(npe.getClass(), NullPointerException.class);
 105 
 106         RuntimeException re = TestKit.assertThrows(
 107                 RuntimeException.class,
 108                 () -> TestKit.assertThrows(NullPointerException.class, () -> { })
 109         );
 110         assertEquals(re.getClass(), RuntimeException.class);
 111         assertEquals(re.getMessage(),
 112                 "Expected to catch an exception of type "
 113                         + "java.lang.NullPointerException, but caught nothing");
 114 
 115         re = TestKit.assertThrows(
 116                 RuntimeException.class,
 117                 () -> { throw new NullPointerException(); }
 118         );
 119         assertNotNull(re);
 120         assertNull(re.getMessage());
 121         assertEquals(re.getClass(), NullPointerException.class);
 122 
 123         re = TestKit.assertThrows(
 124                 RuntimeException.class,
 125                 () -> TestKit.assertThrows(
 126                         IllegalFormatException.class,
 127                         () -> { throw new IndexOutOfBoundsException(); }
 128                 ));
 129         assertNotNull(re);
 130         assertEquals(re.getClass(), RuntimeException.class);
 131         assertEquals(re.getMessage(),
 132                 "Expected to catch an exception of type java.util.IllegalFormatException"
 133                         + ", but caught java.lang.IndexOutOfBoundsException");
 134     }
 135 
 136     @Test
 137     public void testAssertUnmodifiable() {
 138         TestKit.assertUnmodifiableList(
 139                 Collections.unmodifiableList(
 140                         new ArrayList<>(Arrays.asList(1, 2, 3))));
 141         TestKit.assertThrows(RuntimeException.class,
 142                 () -> TestKit.assertUnmodifiableList(new ArrayList<>()));
 143         TestKit.assertThrows(RuntimeException.class,
 144                 () -> TestKit.assertUnmodifiableList(new LinkedList<>()));
 145         TestKit.assertThrows(RuntimeException.class,
 146                 () -> TestKit.assertUnmodifiableList(
 147                         new ArrayList<>(Arrays.asList(1, 2, 3))));
 148         TestKit.assertUnmodifiableMap(Collections.unmodifiableMap(Map.of()));
 149         TestKit.assertThrows(RuntimeException.class,
 150                 () -> TestKit.assertUnmodifiableMap(new HashMap<>()));
 151     }
 152 }