Path: blob/master/test/jdk/sun/nio/cs/ISO8859x.java
41149 views
/*1* Copyright (c) 2008, 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 641979126* @summary27* @author Martin Buchholz28*/2930import java.io.*;31import java.util.*;32import java.nio.charset.*;33import java.nio.*;3435public class ISO8859x {36final static byte[] lowBytes = new byte[0xa0];37final static char[] lowChars = new char[0xa0];38final static String lowString;39static {40for (int i = 0; i < 0xa0; i++) {41lowBytes[i] = (byte) i;42lowChars[i] = (char) i;43}44lowString = new String(lowChars);45}4647private static void testCharset(Charset cs) throws Throwable {48String csn = cs.name();49System.out.println(csn);5051check(cs.canEncode());52check(Arrays.equals(lowString.getBytes(csn), lowBytes));53check(new String(lowBytes, csn).equals(lowString));5455CharsetEncoder encoder = cs.newEncoder();56CharsetDecoder decoder = cs.newDecoder();57decoder.onUnmappableCharacter(CodingErrorAction.REPORT)58.onMalformedInput(CodingErrorAction.REPORT);59encoder.onUnmappableCharacter(CodingErrorAction.REPORT)60.onMalformedInput(CodingErrorAction.REPORT);6162byte[] bytes = new byte[1];63for (int c = 0xa0; c < 0x100; c++) {64try {65bytes[0] = (byte) c;66char[] chars;67try { chars = decoder.decode(ByteBuffer.wrap(bytes)).array(); }68catch (UnmappableCharacterException x) { continue; }69equal(chars.length, 1);70byte[] bytes2 = encoder.encode(CharBuffer.wrap(chars)).array();71check(Arrays.equals(bytes2, bytes));72} catch (Throwable t) {73System.out.printf("cs=%s c=%02x%n", cs, c);74unexpected(t);75}76}77}7879private static void realMain(String[] args) throws Throwable {80for (Map.Entry<String,Charset> e81: Charset.availableCharsets().entrySet()) {82String csn = e.getKey();83Charset cs = e.getValue();84if (csn.matches(".*(8859).*"))85try { testCharset(cs); }86catch (Throwable t) { unexpected(t); }87}88}8990//--------------------- Infrastructure ---------------------------91static volatile int passed = 0, failed = 0;92static void pass() {passed++;}93static void fail() {failed++; Thread.dumpStack();}94static void fail(String msg) {System.out.println(msg); fail();}95static void unexpected(Throwable t) {failed++; t.printStackTrace();}96static void check(boolean cond) {if (cond) pass(); else fail();}97static void equal(Object x, Object y) {98if (x == null ? y == null : x.equals(y)) pass();99else fail(x + " not equal to " + y);}100public static void main(String[] args) throws Throwable {101try {realMain(args);} catch (Throwable t) {unexpected(t);}102System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);103if (failed > 0) throw new AssertionError("Some tests failed");}104}105106107