Path: blob/master/test/jdk/java/lang/StringBuffer/Exceptions.java
41149 views
/*1* Copyright (c) 2002, 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 4414306 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("StringBuffer()");69tryCatch(" no args", null, new Runnable() {70public void run() {71new StringBuffer();72}});7374System.out.println("StringBuffer(int length)");75tryCatch(" 1", null, new Runnable() {76public void run() {77new StringBuffer(1);78}});79tryCatch(" -1", new NegativeArraySizeException(), new Runnable() {80public void run() {81new StringBuffer(-1);82}});8384System.out.println("StringBuffer(String str)");85tryCatch(" null", new NullPointerException(), new Runnable() {86public void run() {87new StringBuffer(null);88}});89tryCatch(" foo", null, new Runnable() {90public void run() {91new StringBuffer("foo");92}});9394System.out.println("StringBuffer.replace(int start, int end, String str)");95tryCatch(" -1, 2, \" \"",96new StringIndexOutOfBoundsException("start -1, end 2, length 7"),97new Runnable() {98public void run() {99StringBuffer sb = new StringBuffer("hilbert");100sb.replace(-1, 2, " ");101}});102103tryCatch(" 7, 8, \" \"",104new StringIndexOutOfBoundsException("start 7, end 6, length 6"),105new Runnable() {106public void run() {107StringBuffer sb = new StringBuffer("banach");108sb.replace(7, 8, " ");109}});110tryCatch(" 2, 1, \" \"",111new StringIndexOutOfBoundsException("start 2, end 1, length 7"),112new Runnable() {113public void run() {114StringBuffer sb = new StringBuffer("riemann");115sb.replace(2, 1, " ");116}});117118if (!ok)119throw new RuntimeException("Some tests FAILED");120else121System.out.println("All tests PASSED");122}123}124125126