1 /*
2  * Copyright (c) 2008, 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 5066863 5066867 5066874 5066879 5066884 5066887 5065777 6730652
26    @summary canEncode() false iff encode() throws CharacterCodingException
27    @run main/timeout=1200 FindCanEncodeBugs
28    @author Martin Buchholz
29  */
30 
31 import java.util.*;
32 import java.nio.charset.*;
33 import java.nio.*;
34 
35 public class FindCanEncodeBugs {
encodable1(CharsetEncoder enc, char c)36     static boolean encodable1(CharsetEncoder enc, char c) {
37         enc.reset();
38         return enc.canEncode(c);
39     }
40 
encodable2(CharsetEncoder enc, char c)41     static boolean encodable2(CharsetEncoder enc, char c) {
42         enc.reset();
43         try { enc.encode(CharBuffer.wrap(new char[]{c})); return true; }
44         catch (CharacterCodingException e) { return false; }
45     }
46 
main(String[] args)47     public static void main(String[] args) throws Exception {
48         int failures = 0;
49 
50         for (Map.Entry<String,Charset> e
51                  : Charset.availableCharsets().entrySet()) {
52             String csn = e.getKey();
53             Charset cs = e.getValue();
54 
55             if (! cs.canEncode() || csn.matches("x-COMPOUND_TEXT"))
56                 continue;
57 
58             //System.out.println(csn);
59 
60             CharsetEncoder enc = cs.newEncoder();
61 
62             for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
63                 boolean encodable1 = encodable1(enc, (char)i);
64                 boolean encodable2 = encodable2(enc, (char)i);
65                 if (encodable1 != encodable2) {
66                     int start = i;
67                     int end = i;
68                     for (int j = i;
69                          j <= '\uffff' &&
70                              encodable1(enc, (char)j) == encodable1 &&
71                              encodable2(enc, (char)j) == encodable2;
72                          j++)
73                         end = j;
74                     System.out.printf("charset=%-18s canEncode=%-5b ",
75                                       csn, encodable1);
76                     if (start == end)
77                         System.out.printf("\'\\u%04x\'%n", start);
78                     else
79                         System.out.printf("\'\\u%04x\' - \'\\u%04x\'%n",
80                                           start, end);
81                     i = end;
82                     failures++;
83                 }
84             }
85         }
86 
87         if (failures > 0)
88             throw new Exception(failures + " failures");
89     }
90 }
91