Path: blob/master/test/jdk/sun/nio/cs/FindASCIICodingBugs.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/* @test24* @bug 619699125* @summary Roundtrip Encoding/Decoding of just one ASCII char26* @author Martin Buchholz27*/2829import java.util.*;30import java.nio.*;31import java.nio.charset.*;3233public class FindASCIICodingBugs {34private static int failures = 0;3536private static void check(boolean condition) {37if (! condition) {38new Error("test failed").printStackTrace();39failures++;40}41}4243private static boolean equals(byte[] ba, ByteBuffer bb) {44if (ba.length != bb.limit())45return false;46for (int i = 0; i < ba.length; i++)47if (ba[i] != bb.get(i))48return false;49return true;50}5152public static void main(String[] args) throws Exception {53for (Map.Entry<String,Charset> e54: Charset.availableCharsets().entrySet()) {55String csn = e.getKey();56Charset cs = e.getValue();5758// Delete the following lines when these charsets are fixed!59if (csn.equals("x-JIS0208")) continue; // MalformedInput60if (csn.equals("JIS_X0212-1990")) continue; // MalformedInput6162if (! cs.canEncode()) continue;6364CharsetEncoder enc = cs.newEncoder();65CharsetDecoder dec = cs.newDecoder();6667if (! enc.canEncode('A')) continue;6869System.out.println(csn);7071try {72byte[] bytes1 = "A".getBytes(csn);73ByteBuffer bb = enc.encode(CharBuffer.wrap(new char[]{'A'}));7475check(equals(bytes1, bb));76check(new String(bytes1, csn).equals("A"));7778CharBuffer cb = dec.decode(bb);79check(cb.toString().equals("A"));80} catch (Throwable t) {81t.printStackTrace();82failures++;83}84}8586if (failures > 0)87throw new Exception(failures + "tests failed");88}89}909192