1 /*
   2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /* @test
  25    @bug 5066863 5066867 5066874 5066879 5066884 5066887 5065777
  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 {
  36     static boolean encodable1(CharsetEncoder enc, char c) {
  37         enc.reset();
  38         return enc.canEncode(c);
  39     }
  40 
  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 
  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() ||
  56                 csn.matches("x-COMPOUND_TEXT")   ||
  57                 csn.matches("x-ISO-2022-CN-CNS"))  // ISO2022_CN_CNS supports less
  58                 continue;
  59 
  60             //System.out.println(csn);
  61 
  62             CharsetEncoder enc = cs.newEncoder();
  63 
  64             for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
  65                 boolean encodable1 = encodable1(enc, (char)i);
  66                 boolean encodable2 = encodable2(enc, (char)i);
  67                 if (encodable1 != encodable2) {
  68                     int start = i;
  69                     int end = i;
  70                     for (int j = i;
  71                          j <= '\uffff' &&
  72                              encodable1(enc, (char)j) == encodable1 &&
  73                              encodable2(enc, (char)j) == encodable2;
  74                          j++)
  75                         end = j;
  76                     System.out.printf("charset=%-18s canEncode=%-5b ",
  77                                       csn, encodable1);
  78                     if (start == end)
  79                         System.out.printf("\'\\u%04x\'%n", start);
  80                     else
  81                         System.out.printf("\'\\u%04x\' - \'\\u%04x\'%n",
  82                                           start, end);
  83                     i = end;
  84                     failures++;
  85                 }
  86             }
  87         }
  88 
  89         if (failures > 0)
  90             throw new Exception(failures + " failures");
  91     }
  92 }