Path: blob/master/test/jdk/sun/nio/cs/FindOneCharEncoderBugs.java
41149 views
/*1* Copyright (c) 2008, 2014, 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 5058133 6233345 6381699 6381702 6381705 638170626* @summary Check that all one-char sequences can be encoded by all charsets27* @run main/timeout=1200 FindOneCharEncoderBugs28* @author Martin Buchholz29*/3031import java.util.*;32import java.nio.*;33import java.nio.charset.*;3435public class FindOneCharEncoderBugs {36final static String[] brokenCharsets = {37// Delete the following lines when these charsets are fixed!38"x-IBM970",39"x-COMPOUND_TEXT", // Direct buffers not supported40};4142private static boolean equals(byte[] ba, ByteBuffer bb) {43if (ba.length != bb.limit())44return false;45for (int i = 0; i < ba.length; i++)46if (ba[i] != bb.get(i))47return false;48return true;49}5051private static String toString(byte[] bytes) {52final StringBuilder sb = new StringBuilder();53for (byte b : bytes) {54if (sb.length() != 0) sb.append(' ');55sb.append(String.format("%02x", (int)b));56}57return sb.toString();58}5960private static String toString(ByteBuffer bb) {61final StringBuilder sb = new StringBuilder();62for (int i = 0; i < bb.limit(); i++) {63if (sb.length() != 0) sb.append(' ');64sb.append(String.format("%02x", (int)bb.get(i)));65}66return sb.toString();67}6869private static ByteBuffer convert(Charset cs, char c, CharBuffer cb) throws Throwable {70cb.clear(); cb.put(c); cb.flip();71return cs.newEncoder()72.onUnmappableCharacter(CodingErrorAction.REPLACE)73.onMalformedInput(CodingErrorAction.REPLACE)74.encode(cb);75}7677/** Returns a direct CharBuffer with the same capacity as ordinary CharBuffer ocb */78private static CharBuffer directCharBuffer(CharBuffer ocb) {79final CharBuffer dcb =80ByteBuffer.allocateDirect(ocb.capacity() * Character.SIZE / Byte.SIZE)81.asCharBuffer();82check(! ocb.isDirect());83check( dcb.isDirect());84equal(ocb.capacity(), dcb.capacity());85return dcb;86}8788private static void testChar(byte[] expected, CharBuffer cb, Charset cs, char c) {89try {90final ByteBuffer bb = convert(cs, c, cb);91if (! equals(expected, bb))92fail("bytes differ charset=%s direct=%s char=\\u%04x%n%s%n%s",93cs, cb.isDirect(), (int)c,94toString(expected), toString(bb));95} catch (Throwable t) {96System.out.printf("Unexpected exception charset=%s direct=%s char=\\u%04x%n",97cs, cb.isDirect(), (int)c);98unexpected(t);99failed++;100}101}102103private static void testCharset(Charset cs) throws Throwable {104if (! cs.canEncode())105return;106107final String csn = cs.name();108109for (String n : brokenCharsets)110if (csn.equals(n)) {111System.out.printf("Skipping possibly broken charset %s%n", csn);112return;113}114System.out.println(csn);115116final char[] theChar = new char[1];117final CharBuffer ocb = CharBuffer.allocate(1);118final CharBuffer dcb = directCharBuffer(ocb);119final int maxFailuresPerCharset = 5;120final int failed0 = failed;121122for (char c = '\u0000';123(c+1 != 0x10000) && (failed - failed0 < maxFailuresPerCharset);124c++) {125theChar[0] = c;126byte[] bytes = new String(theChar).getBytes(csn);127if (bytes.length == 0)128fail("Empty output?! charset=%s char=\\u%04x", cs, (int)c);129testChar(bytes, ocb, cs, c);130testChar(bytes, dcb, cs, c);131}132}133134private static void realMain(String[] args) {135for (Charset cs : Charset.availableCharsets().values()) {136try { testCharset(cs); }137catch (Throwable t) { unexpected(t); }138}139}140141//--------------------- Infrastructure ---------------------------142static volatile int passed = 0, failed = 0;143static void pass() {passed++;}144static void fail() {failed++; Thread.dumpStack();}145static void fail(String format, Object... args) {146System.out.println(String.format(format, args)); failed++;}147static void fail(String msg) {System.out.println(msg); fail();}148static void unexpected(Throwable t) {failed++; t.printStackTrace();}149static void check(boolean cond) {if (cond) pass(); else fail();}150static void equal(Object x, Object y) {151if (x == null ? y == null : x.equals(y)) pass();152else fail(x + " not equal to " + y);}153public static void main(String[] args) throws Throwable {154try {realMain(args);} catch (Throwable t) {unexpected(t);}155System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);156if (failed > 0) throw new AssertionError("Some tests failed");}157private static abstract class CheckedThread extends Thread {158abstract void realRun() throws Throwable;159public void run() {160try {realRun();} catch (Throwable t) {unexpected(t);}}}161}162163164