Path: blob/master/test/jdk/java/lang/String/Exceptions.java
41149 views
/*1* Copyright (c) 2002, 2006, 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 4472841 4703640 4705681 4705683 4833095 500583126* @summary Verify that constructor exceptions are thrown as expected.27*/2829import java.io.UnsupportedEncodingException;30import java.nio.charset.Charset;3132public class Exceptions {33private static final byte [] b = { 0x48, 0x69, 0x2c, 0x20,340x44, 0x75, 0x6b, 0x65, 0x21 };3536private static final char [] c37= "Attack of the Killer Tomatoes!".toCharArray();3839private static boolean ok = true;4041private static void fail(Class ex, String s) {42ok = false;43System.err.println("expected " + ex.getName() + " for " + s44+ " - FAILED");45}4647private static void pass(String s) {48System.out.println(s + " -- OK");49}5051private static void tryCatch(String s, Class ex, Runnable thunk) {52Throwable t = null;53try {54thunk.run();55} catch (Throwable x) {56if (ex.isAssignableFrom(x.getClass()))57t = x;58else59x.printStackTrace();60}61if ((t == null) && (ex != null))62fail(ex, s);63else64pass(s);65}6667// -- Constructors --6869private static void noArgs() {70System.out.println("String()");71tryCatch(" default ctor", null, new Runnable() {72public void run() {73new String();74}});75}7677private static void string() {78System.out.println("String(String original)");79tryCatch(" \"foo\"", null, new Runnable() {80public void run() {81new String("foo");82}});83tryCatch(" null", NullPointerException.class, new Runnable() {84public void run() {85new String((String) null);86}});87}8889private static void charArray() {90System.out.println("String(char value[])");91tryCatch(" char [] = \"Duke says \"Hi!\"\"", null, new Runnable() {92public void run() {93new String("Duke says \"Hi!\"".toCharArray());94}});95tryCatch(" null", NullPointerException.class, new Runnable() {96public void run() {97new String((char []) null);98}});99}100101private static void charArrayOffCount() {102System.out.println("String(char value[], int offset, int count)");103tryCatch(" c, 0, 3", null, new Runnable() {104public void run() {105new String(c, 0, 3);106}});107tryCatch(" null, 1, 2", NullPointerException.class, new Runnable() {108public void run() {109new String((char []) null, 1, 2);110}});111tryCatch(" c, -1, 4", IndexOutOfBoundsException.class,112new Runnable() {113public void run() {114new String(c, -1, 4);115}});116tryCatch(" c, 1, -1", IndexOutOfBoundsException.class,117new Runnable() {118public void run() {119new String(c, 1, -1);120}});121tryCatch(" c, c.lengh + 1, 1", IndexOutOfBoundsException.class,122new Runnable() {123public void run() {124new String(c, c.length + 1, 1);125}});126tryCatch(" c, 0, c.length + 1", IndexOutOfBoundsException.class,127new Runnable() {128public void run() {129new String(c, 0, c.length + 1);130}});131}132133private static void byteArrayHiOffCount() {134System.out.println("String(byte ascii[], int hibyte, int offset, "135+ "int count)");136tryCatch(" b, 0, 0, b.length", null, new Runnable() {137public void run() {138System.out.println(new String(b, 0, 0, b.length));139}});140141tryCatch(" b, -1, 4, 4", null, new Runnable() {142public void run() {143new String(b, -1, 4, 4);144}});145tryCatch(" null, 0, 0, 0", NullPointerException.class,146new Runnable() {147public void run() {148new String((byte[]) null, 0, 0, 0);149}});150tryCatch(" b, 0, -1, r", IndexOutOfBoundsException.class,151new Runnable() {152public void run() {153new String(b, 0, -1, 4);154}});155tryCatch(" b, 0, 4, -1", IndexOutOfBoundsException.class,156new Runnable() {157public void run() {158new String(b, 0, 4, -1);159}});160tryCatch(" b, 0, b.length + 1, 1", IndexOutOfBoundsException.class,161new Runnable() {162public void run() {163new String(b, 0, b.length + 1, 1);164}});165tryCatch(" b, 0, 0, b.length + 1", IndexOutOfBoundsException.class,166new Runnable() {167public void run() {168new String(b, 0, 0, b.length + 1);169}});170}171172private static void byteArrayHi() {173System.out.println("String(byte ascii[], int hibyte)");174tryCatch(" b, 0", null, new Runnable() {175public void run() {176new String(b, 0);177}});178tryCatch(" null, 0", NullPointerException.class, new Runnable() {179public void run() {180new String((byte []) null, 0);181}});182}183184private static void byteArrayOffLengthCharset0(String s, Class ex,185byte [] b, int off,186int len, Object cs)187{188Throwable t = null;189try {190if (cs instanceof String)191new String(b, off, len, (String)cs);192else // (cs instanceof Charset)193new String(b, off, len, (Charset)cs);194} catch (Throwable x) {195if (ex.isAssignableFrom(x.getClass()))196t = x;197else198x.printStackTrace();199}200if ((t == null) && (ex != null))201fail(ex, s);202else203pass(s);204}205206private static void byteArrayOffLengthCharsetName() {207System.out.println("String(byte bytes[], int offset, int length, "208+ "String charsetName)");209System.out.println(" throws UnsupportedEncodingException");210String enc = "UTF-8";211byteArrayOffLengthCharset0(" b, 0, 0," + enc, null, b, 0, 0, enc);212byteArrayOffLengthCharset0(" null, 0, 0," + enc,213NullPointerException.class,214(byte []) null, 0, 0, enc);215byteArrayOffLengthCharset0(" b, -1, 0, " + enc,216IndexOutOfBoundsException.class,217b, -1, 0, enc);218byteArrayOffLengthCharset0(" b, 0, -1, " + enc,219IndexOutOfBoundsException.class,220b, 0, -1, enc);221byteArrayOffLengthCharset0(" b, b.length + 1, 1, " + enc,222IndexOutOfBoundsException.class,223b, b.length + 1, 1, enc);224byteArrayOffLengthCharset0(" b, 0, b.length + 1 " + enc,225IndexOutOfBoundsException.class,226b, 0, b.length + 1, enc);227byteArrayOffLengthCharset0(" b, -1, 0, null",228NullPointerException.class,229b, -1, 0, null);230byteArrayOffLengthCharset0(" b, 0, b.length, foo",231UnsupportedEncodingException.class,232b, 0, b.length, "foo");233}234235private static void byteArrayOffLengthCharset() {236System.out.println("String(byte bytes[], int offset, int length, "237+ "Charset charset)");238Charset cs = Charset.forName("UTF-16BE");239byteArrayOffLengthCharset0(" b, 0, 0," + cs, null, b, 0, 0, cs);240byteArrayOffLengthCharset0(" null, 0, 0," + cs,241NullPointerException.class,242(byte []) null, 0, 0, cs);243byteArrayOffLengthCharset0(" b, -1, 0, " + cs,244IndexOutOfBoundsException.class,245b, -1, 0, cs);246byteArrayOffLengthCharset0(" b, 0, -1, " + cs,247IndexOutOfBoundsException.class,248b, 0, -1, cs);249byteArrayOffLengthCharset0(" b, b.length + 1, 1, " + cs,250IndexOutOfBoundsException.class,251b, b.length + 1, 1, cs);252byteArrayOffLengthCharset0(" b, 0, b.length + 1 " + cs,253IndexOutOfBoundsException.class,254b, 0, b.length + 1, cs);255byteArrayOffLengthCharset0(" b, -1, 0, null",256NullPointerException.class,257b, -1, 0, null);258}259260private static void byteArrayCharset0(String s, Class ex, byte [] b,261Object cs)262{263Throwable t = null;264try {265if (cs instanceof String)266new String(b, (String)cs);267else // (cs instanceof Charset)268new String(b, (Charset)cs);269} catch (Throwable x) {270if (ex.isAssignableFrom(x.getClass()))271t = x;272else273x.printStackTrace();274}275if ((t == null) && (ex != null))276fail(ex, s);277else278pass(s);279}280281private static void byteArrayCharsetName() {282System.out.println("String(byte bytes[], String charsetName)");283System.out.println(" throws UnsupportedEncodingException");284String enc = "US-ASCII";285byteArrayCharset0(" b, " + enc, null, b, enc);286byteArrayCharset0(" null, " + enc, NullPointerException.class,287(byte []) null, enc);288byteArrayCharset0(" b, null", NullPointerException.class, b, null);289byteArrayCharset0(" null, null", NullPointerException.class,290(byte []) null, null);291byteArrayCharset0(" b, bar", UnsupportedEncodingException.class,292b, "bar");293}294295private static void byteArrayCharset() {296System.out.println("String(byte bytes[], Charset charset)");297Charset cs = Charset.forName("ISO-8859-1");298byteArrayCharset0(" b, " + cs, null, b, cs);299byteArrayCharset0(" null, " + cs, NullPointerException.class,300(byte []) null, cs);301byteArrayCharset0(" b, null", NullPointerException.class, b, null);302byteArrayCharset0(" null, null", NullPointerException.class,303(byte []) null, null);304}305306private static void byteArrayOffLength() {307System.out.println("String(byte bytes[], int offset, int length)");308tryCatch(" b, 0, b.length", null, new Runnable() {309public void run() {310new String(b, 0, b.length);311}});312tryCatch(" null, 0, 0", NullPointerException.class, new Runnable() {313public void run() {314new String((byte []) null, 0, 0);315}});316tryCatch(" b, -1, b.length", IndexOutOfBoundsException.class,317new Runnable() {318public void run() {319new String(b, -1, b.length);320}});321tryCatch(" b, 0, -1", IndexOutOfBoundsException.class,322new Runnable() {323public void run() {324new String(b, 0, -1);325}});326tryCatch(" b, b.length + 1, 1", IndexOutOfBoundsException.class,327new Runnable() {328public void run() {329new String(b, b.length + 1, 1);330}});331tryCatch(" b, 0, b.length", IndexOutOfBoundsException.class,332new Runnable() {333public void run() {334new String(b, 0, b.length + 1);335}});336}337338private static void byteArray() {339System.out.println("String(byte bytes[])");340tryCatch(" b", null, new Runnable() {341public void run() {342new String(b);343}});344tryCatch(" null", NullPointerException.class, new Runnable() {345public void run() {346new String((byte []) null);347}});348}349350private static void stringBuffer() {351System.out.println("String(StringBuffer buffer)");352tryCatch(" \"bar\"", null, new Runnable() {353public void run() {354new String(new StringBuffer("bar"));355}});356tryCatch(" null", NullPointerException.class, new Runnable() {357public void run() {358new String((StringBuffer) null);359}});360}361362// -- Methods --363364private static void getChars() {365System.out.println("getChars.(int srcBegin, int srcEnd, char dst[], "366+ " int dstBegin");367tryCatch(" null", NullPointerException.class, new Runnable() {368public void run() {369"foo".getChars(1, 2, null, 1);370}});371}372373private static void getBytes() {374System.out.println("getChars.(int srcBegin, int srcEnd, char dst[], "375+ " int dstBegin");376tryCatch(" 1, 2, null, 1", NullPointerException.class, new Runnable() {377public void run() {378"foo".getBytes(1, 2, null, 1);379}});380381System.out.println("getBytes.(String charsetName)"382+ " throws UnsupportedEncodingException");383tryCatch(" null", NullPointerException.class, new Runnable() {384public void run() {385try {386"foo".getBytes((String)null);387} catch (UnsupportedEncodingException x) {388throw new RuntimeException(x);389}390}});391392System.out.println("getBytes.(Charset charset)");393tryCatch(" null", NullPointerException.class, new Runnable() {394public void run() {395"foo".getBytes((Charset)null);396}});397}398399private static void contentEquals() {400System.out.println("contentEquals(StringBuffer sb)");401tryCatch(" null", NullPointerException.class, new Runnable() {402public void run() {403"foo".contentEquals(null);404}});405}406407private static void compareTo() {408System.out.println("compareTo(String anotherString)");409tryCatch(" (String) null", NullPointerException.class, new Runnable() {410public void run() {411"foo".compareTo((String) null);412}});413414/* 4830291 (javac generics bug) causes this test to fail415System.out.println("compareTo(Object o)");416tryCatch(" (Object) null", NullPointerException.class, new Runnable() {417public void run() {418"foo".compareTo((Object) null);419}});420*/421}422423private static void compareToIgnoreCase() {424System.out.println("compareToIgnoreCase(String anotherString)");425tryCatch(" null", NullPointerException.class, new Runnable() {426public void run() {427"foo".compareToIgnoreCase((String) null);428}});429}430431private static void regionMatches() {432System.out.println("regionMatches(int toffset, String other,"433+ " int ooffset, int len)");434tryCatch(" 1, null, 1, 1", NullPointerException.class, new Runnable() {435public void run() {436"foo".regionMatches(1, null, 1, 1);437}});438439System.out.println("regionMatches(boolean ignore, int toffset,"440+ " String other, int ooffset, int len)");441tryCatch(" true, 1, null, 1, 1", NullPointerException.class,442new Runnable() {443public void run() {444"foo".regionMatches(true, 1, null, 1, 1);445}});446}447448private static void startsWith() {449System.out.println("startsWith(String prefix, int toffset)");450tryCatch(" null, 1", NullPointerException.class, new Runnable() {451public void run() {452"foo".startsWith(null, 1);453}});454455System.out.println("startsWith(String prefix)");456tryCatch(" null", NullPointerException.class, new Runnable() {457public void run() {458"foo".startsWith(null);459}});460}461462private static void endsWith() {463System.out.println("endsWith(String suffix)");464tryCatch(" null", NullPointerException.class, new Runnable() {465public void run() {466"foo".endsWith(null);467}});468}469470private static void indexOf() {471System.out.println("indexOf(String str)");472tryCatch(" null", NullPointerException.class, new Runnable() {473public void run() {474"foo".indexOf(null);475}});476477System.out.println("indexOf(String str, int fromIndex)");478tryCatch(" null, 1", NullPointerException.class, new Runnable() {479public void run() {480"foo".indexOf(null, 1);481}});482}483484private static void lastIndexOf() {485System.out.println("lastIndexOf(String str)");486tryCatch(" null", NullPointerException.class, new Runnable() {487public void run() {488"foo".lastIndexOf(null);489}});490491System.out.println("lastIndexOf(String str, int fromIndex)");492tryCatch(" null, 1", NullPointerException.class, new Runnable() {493public void run() {494"foo".lastIndexOf(null, 1);495}});496}497498private static void concat() {499System.out.println("concat(String str)");500tryCatch(" null", NullPointerException.class, new Runnable() {501public void run() {502"foo".concat(null);503}});504}505506private static void matches() {507System.out.println("matches(String regex)");508tryCatch(" null", NullPointerException.class, new Runnable() {509public void run() {510"foo".matches(null);511}});512}513514private static void replaceFirst() {515System.out.println("replaceFirst(String regex, String replacement)");516tryCatch(" \".\", null", NullPointerException.class, new Runnable() {517public void run() {518"foo".replaceFirst(".", null);519}});520tryCatch(" null, \"-\"", NullPointerException.class, new Runnable() {521public void run() {522"foo".replaceFirst(null, "-");523}});524}525526private static void replaceAll() {527System.out.println("replaceAll(String regex, String replacement)");528tryCatch(" \".\", null", NullPointerException.class, new Runnable() {529public void run() {530"foo".replaceAll(".", null);531}});532tryCatch(" null, \"-\"", NullPointerException.class, new Runnable() {533public void run() {534"foo".replaceAll(null, "-");535}});536}537538private static void split() {539System.out.println("split(String regex, int limit)");540tryCatch(" null, 1", NullPointerException.class, new Runnable() {541public void run() {542"foo".split(null, 1);543}});544545System.out.println("split(String regex, int limit)");546tryCatch(" null", NullPointerException.class, new Runnable() {547public void run() {548"foo".split(null);549}});550}551552private static void toLowerCase() {553System.out.println("toLowerCase(Locale locale)");554tryCatch(" null", NullPointerException.class, new Runnable() {555public void run() {556"foo".toLowerCase(null);557}});558}559560private static void toUpperCase() {561System.out.println("toUpperCase(Locale locale)");562tryCatch(" null", NullPointerException.class, new Runnable() {563public void run() {564"foo".toUpperCase(null);565}});566}567568private static void valueOf() {569System.out.println("valueOf(Object obj)");570tryCatch(" null", null, new Runnable() {571public void run() {572String.valueOf((Object) null);573}});574575System.out.println("valueOf(char data[])");576tryCatch(" null", NullPointerException.class, new Runnable() {577public void run() {578String.valueOf((char []) null);579}});580581System.out.println("valueOf(char data[], int offset, int count)");582tryCatch(" null, 1, 2", NullPointerException.class, new Runnable() {583public void run() {584String.valueOf((char []) null, 1, 2);585}});586587}588589private static void copyValueOf() {590System.out.println("copyValueOf(char data[], int offset, int count)");591tryCatch(" null, 1, 2", NullPointerException.class, new Runnable() {592public void run() {593"foo".copyValueOf((char []) null, 1, 2);594}});595596System.out.println("copyVlueOf(char data[])");597tryCatch(" null", NullPointerException.class, new Runnable() {598public void run() {599String.copyValueOf((char []) null);600}});601}602603public static void main(String [] args) {604605// -- Constructors --606607noArgs(); // String()608string(); // String(String original)609charArray(); // String(char value[])610charArrayOffCount(); // String(char value[], int offset, int count)611612// String(byte ascii[], int hibyte, int offset, int count)613byteArrayHiOffCount();614615byteArrayHi(); // String(byte ascii[], int hibyte)616617// String(byte bytes[], int offset, int length, String charsetName)618// throws UnsupportedEncodingException619byteArrayOffLengthCharsetName();620621// String(byte bytes[], int offset, int length, Charset charset)622byteArrayOffLengthCharset();623624// String(byte bytes[], String charsetName)625// throws UnsupportedEncodingException626byteArrayCharsetName();627628// String(byte bytes[], Charset charset)629byteArrayCharset();630631byteArrayOffLength(); // String(byte bytes[], int offset, int length)632byteArray(); // String(byte bytes[])633stringBuffer(); // String(StringBuffer buffer)634635// -- Methods --636637getChars(); // getChars(int, int. char [], int)638getBytes(); // getBytes(int, int, byte [], int),639// getBytes(Locale)640// getBytes(String)641// getBytes(Charset)642contentEquals(); // contentEquals(StringBuffer)643compareTo(); // compareTo(String), compareTo(Object)644compareToIgnoreCase();// compareToIgnoreCase(String)645regionMatches(); // regionMatches(int, String, int, int)646// regionMatches(boolean, int, String, int, int)647startsWith(); // startsWith(String, int), startsWith(String)648endsWith(); // endsWith(String)649indexOf(); // indexOf(String), indexOf(String, int),650lastIndexOf(); // lastIndexOf(String), lastIndexOf(String, int)651concat(); // concat(String)652matches(); // matches(String)653replaceFirst(); // replaceFirst(String, String)654replaceAll(); // replaceAll(String, String)655split(); // split(String, int), split(String)656toLowerCase(); // toLowerCase(Locale)657toUpperCase(); // toUpperCase(Locale)658valueOf(); // valueOf(Object), valueOf(char []),659// valueOf(char [], int, int)660copyValueOf(); // copyValueOf(char [], int, int),661// copyValueOf(char [])662663if (!ok)664throw new RuntimeException("Some tests FAILED");665else666System.out.println("All tests PASSED");667}668}669670671