1 /* 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4821213 26 * @summary Unit test for CharsetEncoder.canEncode methods 27 */ 28 29 import java.io.*; 30 import java.nio.*; 31 import java.nio.charset.*; 32 33 34 public class CanEncode { 35 36 private static int errors = 0; 37 private static PrintStream out = System.err; 38 wrong(CharsetEncoder ce, boolean can, String what)39 private static void wrong(CharsetEncoder ce, boolean can, String what) { 40 out.println(ce.charset().name() 41 + ": Wrong answer for " + what 42 + ": " + !can); 43 errors++; 44 } 45 ck(CharsetEncoder ce, char c, boolean can)46 private static void ck(CharsetEncoder ce, char c, boolean can) 47 throws Exception 48 { 49 if (ce.canEncode(c) != can) 50 wrong(ce, can, 51 ("'" + c + "' (0x" 52 + Integer.toHexString(c & 0xffff) + ")")); 53 } 54 ck(CharsetEncoder ce, String s, boolean can)55 private static void ck(CharsetEncoder ce, String s, boolean can) 56 throws Exception 57 { 58 if (ce.canEncode(CharBuffer.wrap(s.toCharArray())) != can) 59 wrong(ce, can, "array \"" + s + "\""); 60 if (ce.canEncode(CharBuffer.wrap(s)) != can) 61 wrong(ce, can, "buffer \"" + s + "\""); 62 } 63 test(String csn)64 private static void test(String csn) throws Exception { 65 66 Charset cs = Charset.forName(csn); 67 CharsetEncoder ce = cs.newEncoder(); 68 69 if (cs.name().equals("US-ASCII")) { 70 ck(ce, 'x', true); 71 ck(ce, '\u00B6', false); 72 ck(ce, "x", true); 73 ck(ce, "\u00B6", false); 74 ck(ce, "xyzzy", true); 75 ck(ce, "xy\u00B6", false); 76 } 77 78 // Unpaired surrogates should never be encodable 79 ck(ce, '\ud800', false); 80 ck(ce, '\ud801', false); 81 ck(ce, '\udffe', false); 82 ck(ce, '\udfff', false); 83 ck(ce, "\ud800", false); 84 ck(ce, "\ud801", false); 85 ck(ce, "\udffe", false); 86 ck(ce, "\udfff", false); 87 88 } 89 main(String[] args)90 public static void main(String[] args) throws Exception { 91 test("US-ASCII"); 92 test("UTF-8"); 93 } 94 95 } 96