Path: blob/master/test/jdk/java/nio/charset/coders/Errors.java
41153 views
/*1* Copyright (c) 2010, 2011, 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/* @test24* @summary Check that error cases are replaced correctly in String/ISR/OSW25* @bug 4457851 709608026*27* @build Errors Util28* @run main Errors29*/3031import java.io.*;32import java.nio.*;333435public class Errors {3637static PrintStream log = System.err;38static int failures = 0;3940static final byte Q = (byte)'?';41static final byte X = (byte)'x';42static final byte Y = (byte)'y';43static final byte Z = (byte)'z';4445static abstract class Test {4647protected final String csn;48protected final String what;4950Test(String csn, String what) {51this.csn = csn;52this.what = what;53}5455abstract byte[] enc(String s) throws IOException;5657Test test(String s, byte[] ref) {58log.print(" " + Util.toString(s.toCharArray()));59byte[] ba = null;60try {61ba = enc(s);62} catch (IOException x) {63log.println(" -e-> ERROR: " + x.getClass().getName());64failures++;65return this;66}67log.println(" -e-> " + Util.toString(ba));68int i = Util.cmp(ba, ref);69if (i >= 0) {70log.println(" ERROR: Mismatch at index " + i71+ ", expected: " + Util.toString(ref));72failures++;73}74return this;75}7677abstract String dec(byte[] ba) throws IOException;7879Test test(byte[] ba, String ref) {80log.print(" " + Util.toString(ba));81String s = null;82try {83s = dec(ba);84} catch (IOException x) {85log.println(" -d-> ERROR: " + x.getClass().getName());86failures++;87return this;88}89log.println(" -d-> " + Util.toString(s.toCharArray()));90char[] ca = s.toCharArray();91char[] refa = ref.toCharArray();92int i = Util.cmp(ca, refa);93if (i >= 0) {94log.println(" ERROR: Mismatch at index " + i95+ ", expected: " + Util.toString(refa));96failures++;97}98return this;99}100101Test run() {102log.println(csn + ", " + what);103104test("xyzzy", new byte[] { X, Y, Z, Z, Y });105106// Malformed surrogates107test("\uD800x", new byte[] { Q, X });108test("\uDC00x", new byte[] { Q, X });109test("\uD800\uDB00x", new byte[] { Q, Q, X });110111return this;112}113114}115116static class TestStream extends Test {117118TestStream(String csn) {119super(csn, "I/O streams");120}121122byte[] enc(String s) throws IOException {123ByteArrayOutputStream bos = new ByteArrayOutputStream();124Writer wr = new OutputStreamWriter(bos, csn);125wr.write(s);126wr.close();127return bos.toByteArray();128}129130String dec(byte[] ba) throws IOException {131ByteArrayInputStream bis = new ByteArrayInputStream(ba);132Reader rd = new InputStreamReader(bis, csn);133char[] ca = new char[1024];134int n = rd.read(ca);135String s = new String(ca, 0, n);136rd.close();137return s;138}139140}141142static class TestString extends Test {143144TestString(String csn) {145super(csn, "strings");146}147148byte[] enc(String s) throws IOException {149return s.getBytes(csn);150}151152String dec(byte[] ba) throws IOException {153return new String(ba, 0, ba.length, csn);154}155156}157158static void test_US_ASCII(Test t) {159t.run();160t.test("\u0080", new byte[] { Q });161t.test("\u0100", new byte[] { Q });162t.test("\uD800\uDC00", new byte[] { Q });163t.test("\uF000", new byte[] { Q });164t.test("\uFFFE", new byte[] { Q });165t.test("\uFFFF", new byte[] { Q });166t.test(new byte[] { X, (byte)0x7f, Y }, "x\u007Fy");167t.test(new byte[] { X, (byte)0x80, Y }, "x\uFFFDy");168t.test(new byte[] { (byte)0xf0, (byte)0xf0 }, "\uFFFD\uFFFD");169}170171static void test_ISO_8859_1(Test t) {172t.run();173t.test("\u0080", new byte[] { (byte)0x80 });174t.test("\u0100", new byte[] { Q });175t.test("\uD800\uDC00x", new byte[] { Q, X });176t.test("\uF000", new byte[] { Q });177t.test("\uFFFE", new byte[] { Q });178t.test("\uFFFF", new byte[] { Q });179t.test(new byte[] { X, (byte)0x7f, Y }, "x\u007Fy");180t.test(new byte[] { X, (byte)0x80, Y }, "x\u0080y");181t.test(new byte[] { (byte)0xf0, (byte)0xf0 }, "\u00F0\u00F0");182}183184static void test_UTF_8(Test t) {185t.run();186t.test("\u0080", new byte[] { (byte)0xC2, (byte)0x80 });187t.test("\u0100", new byte[] { (byte)0xC4, (byte)0x80 });188t.test("\uD800\uDC00",189new byte[] { (byte)0xF0, (byte)0x90, (byte)0x80, (byte)0x80 });190t.test("\uF000", new byte[] { (byte)0xEF, (byte)0x80, (byte)0x80 });191t.test("\uFFFE", new byte[] { (byte)0xEF, (byte)0xBF, (byte)0xBE });192t.test("\uFFFF", new byte[] { (byte)0xEF, (byte)0xBF, (byte)0xBF });193t.test(new byte[] { X, (byte)0x7f, Y }, "x\u007Fy");194t.test(new byte[] { X, (byte)0x80, Y }, "x\uFFFDy");195}196197public static void main(String[] args) throws Exception {198test_US_ASCII(new TestString("US-ASCII"));199test_US_ASCII(new TestStream("US-ASCII"));200201test_ISO_8859_1(new TestString("ISO-8859-1"));202test_ISO_8859_1(new TestStream("ISO-8859-1"));203204test_ISO_8859_1(new TestString("ISO-8859-15"));205test_ISO_8859_1(new TestStream("ISO-8859-15"));206207test_UTF_8(new TestString("UTF-8"));208test_UTF_8(new TestStream("UTF-8"));209210if (failures > 0) {211log.println();212throw new Exception("Tests failed: " + failures);213}214215}216217}218219220