Path: blob/master/test/jdk/java/lang/StringBuilder/Exceptions.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 624850726* @summary Verify that exceptions are thrown as expected.27*/2829public class Exceptions {30private static boolean ok = true;3132private static void fail(Throwable ex, String s, Throwable got) {33ok = false;34System.err.println("expected "35+ ex.getClass().getName() + ": " + ex.getMessage()36+ " for " + s37+ " got "38+ got.getClass().getName() + ": " + got.getMessage()39+ " - FAILED");40}4142private static void pass(String s) {43System.out.println(s + " -- OK");44}4546private static void tryCatch(String s, Throwable ex, Runnable thunk) {47Throwable t = null;48try {49thunk.run();50} catch (Throwable x) {51// x.printStackTrace();52if (ex.getClass().isAssignableFrom(x.getClass()))53t = x;54else55x.printStackTrace();56}57if ((t == null) && (ex != null))58fail(ex, s, t);5960String msg = (ex == null ? null : ex.getMessage());61if ((msg != null) && !msg.equals(t.getMessage()))62fail(ex, s, t);63else64pass(s);65}6667public static void main(String [] args) {68System.out.println("StringBuilder()");69tryCatch(" no args", null, new Runnable() {70public void run() {71new StringBuilder();72}});7374System.out.println("StringBuilder(int length)");75tryCatch(" 1", null, new Runnable() {76public void run() {77new StringBuilder(1);78}});79tryCatch(" -1", new NegativeArraySizeException(), new Runnable() {80public void run() {81new StringBuilder(-1);82}});8384System.out.println("StringBuilder(String str)");85tryCatch(" null", new NullPointerException(), new Runnable() {86public void run() {87new StringBuilder(null);88}});89tryCatch(" foo", null, new Runnable() {90public void run() {91new StringBuilder("foo");92}});9394System.out.println("StringBuilder.replace(int start, int end, String str)");95tryCatch(" -1, 2, \" \"",96new StringIndexOutOfBoundsException("start -1, end 2, length 7"),97new Runnable() {98public void run() {99StringBuilder sb = new StringBuilder("hilbert");100sb.replace(-1, 2, " ");101}});102tryCatch(" 7, 8, \" \"",103new StringIndexOutOfBoundsException("start 7, end 6, length 6"),104new Runnable() {105public void run() {106StringBuilder sb = new StringBuilder("banach");107sb.replace(7, 8, " ");108}});109tryCatch(" 2, 1, \" \"",110new StringIndexOutOfBoundsException("start 2, end 1, length 7"),111new Runnable() {112public void run() {113StringBuilder sb = new StringBuilder("riemann");114sb.replace(2, 1, " ");115}});116117if (!ok)118throw new RuntimeException("Some tests FAILED");119else120System.out.println("All tests PASSED");121}122}123124125