Assertions.java revision 3170:dc017a37aac5
1/* 2 * Copyright (c) 2013, 2014, 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 24package test; 25 26import java.util.Arrays; 27import java.util.HashSet; 28import java.util.Set; 29 30public class Assertions { 31 interface Inner { 32 default void testInner() { 33 assert false; 34 } 35 } 36 37 static class InnerImpl implements Inner {} 38 39 static class OuterImpl implements Outer {} 40 41 static class AnotherInnerImpl implements Another.Inner {} 42 43 public static void main(String... args) { 44 Set<String> shouldThrowAssert = new HashSet<String>(Arrays.asList(args)); 45 try { 46 new InnerImpl().testInner(); 47 if (shouldThrowAssert.contains("Inner")) { 48 throw new IllegalStateException("AssertionError expected, but not thrown."); 49 } 50 } catch (AssertionError e) { 51 if (!shouldThrowAssert.contains("Inner")) { 52 throw new IllegalStateException("AssertionError not expected, but thrown."); 53 } 54 } 55 try { 56 new OuterImpl().testOuter(); 57 if (shouldThrowAssert.contains("Outer")) { 58 throw new IllegalStateException("AssertionError expected, but not thrown."); 59 } 60 } catch (AssertionError e) { 61 if (!shouldThrowAssert.contains("Outer")) { 62 throw new IllegalStateException("AssertionError not expected, but thrown."); 63 } 64 } 65 try { 66 new AnotherInnerImpl().testAnotherInner(); 67 if (shouldThrowAssert.contains("Another.Inner")) { 68 throw new IllegalStateException("AssertionError expected, but not thrown."); 69 } 70 } catch (AssertionError e) { 71 if (!shouldThrowAssert.contains("Another.Inner")) { 72 throw new IllegalStateException("AssertionError not expected, but thrown."); 73 } 74 } 75 } 76} 77 78interface Outer { 79 default void testOuter() { 80 assert false; 81 } 82} 83 84@interface Another { 85 interface Inner { 86 default void testAnotherInner() { 87 assert false; 88 } 89 } 90} 91