test/sun/nio/cs/TestStringCoding.java

Print this page




   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* @test
  27    @bug 6636323 6636319
  28    @summary Test if StringCoding and NIO result have the same de/encoding result
  29  * @run main/othervm/timeout=2000 TestStringCoding
  30  */
  31 
  32 import java.util.*;
  33 import java.nio.*;
  34 import java.nio.charset.*;
  35 
  36 public class TestStringCoding {
  37     public static void main(String[] args) throws Throwable {
  38 
  39         for (Boolean hasSM: new boolean[] { false, true }) {
  40             if (hasSM)
  41                 System.setSecurityManager(new PermissiveSecurityManger());
  42             for (Charset cs:  Charset.availableCharsets().values()) {
  43                 if ("ISO-2022-CN".equals(cs.name()) ||
  44                     "x-COMPOUND_TEXT".equals(cs.name()) ||
  45                     "x-JISAutoDetect".equals(cs.name()))
  46                     continue;
  47                 System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);


  94 
  95         //getBytes(cs);
  96         baSC = bmpStr.getBytes(cs);
  97         if (!Arrays.equals(baSC, baNIO))
  98             throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
  99 
 100         //new String(csn);
 101         String strSC = new String(sbBA, cs.name());
 102         String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString();
 103         if(!strNIO.equals(strSC))
 104             throw new RuntimeException("new String(csn) failed  -> " + cs.name());
 105 
 106         //new String(cs);
 107         strSC = new String(sbBA, cs);
 108         if (!strNIO.equals(strSC))
 109             throw new RuntimeException("new String(cs) failed  -> " + cs.name());
 110 
 111         //encode unmappable surrogates
 112         if (enc instanceof sun.nio.cs.ArrayEncoder &&
 113             cs.contains(Charset.forName("ASCII"))) {


 114             enc.replaceWith(new byte[] { (byte)'A'});
 115             sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;
 116 
 117             String str = "ab\uD800\uDC00\uD800\uDC00cd";
 118             byte[] ba = new byte[str.length() - 2];
 119             int n = cae.encode(str.toCharArray(), 0, str.length(), ba);
 120             if (n != 6 || !"abAAcd".equals(new String(ba, cs.name())))
 121                 throw new RuntimeException("encode1(surrogates) failed  -> "
 122                                            + cs.name());
 123 
 124             ba = new byte[str.length()];
 125             n = cae.encode(str.toCharArray(), 0, str.length(), ba);
 126             if (n != 6 || !"abAAcd".equals(new String(ba, 0, n,
 127                                                      cs.name())))
 128                 throw new RuntimeException("encode2(surrogates) failed  -> "
 129                                            + cs.name());
 130             str = "ab\uD800B\uDC00Bcd";
 131             ba = new byte[str.length()];
 132             n = cae.encode(str.toCharArray(), 0, str.length(), ba);
 133             if (n != 8 || !"abABABcd".equals(new String(ba, 0, n,


   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /* @test
  27    @bug 6636323 6636319 7040220
  28    @summary Test if StringCoding and NIO result have the same de/encoding result
  29  * @run main/othervm/timeout=2000 TestStringCoding
  30  */
  31 
  32 import java.util.*;
  33 import java.nio.*;
  34 import java.nio.charset.*;
  35 
  36 public class TestStringCoding {
  37     public static void main(String[] args) throws Throwable {
  38 
  39         for (Boolean hasSM: new boolean[] { false, true }) {
  40             if (hasSM)
  41                 System.setSecurityManager(new PermissiveSecurityManger());
  42             for (Charset cs:  Charset.availableCharsets().values()) {
  43                 if ("ISO-2022-CN".equals(cs.name()) ||
  44                     "x-COMPOUND_TEXT".equals(cs.name()) ||
  45                     "x-JISAutoDetect".equals(cs.name()))
  46                     continue;
  47                 System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);


  94 
  95         //getBytes(cs);
  96         baSC = bmpStr.getBytes(cs);
  97         if (!Arrays.equals(baSC, baNIO))
  98             throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());
  99 
 100         //new String(csn);
 101         String strSC = new String(sbBA, cs.name());
 102         String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString();
 103         if(!strNIO.equals(strSC))
 104             throw new RuntimeException("new String(csn) failed  -> " + cs.name());
 105 
 106         //new String(cs);
 107         strSC = new String(sbBA, cs);
 108         if (!strNIO.equals(strSC))
 109             throw new RuntimeException("new String(cs) failed  -> " + cs.name());
 110 
 111         //encode unmappable surrogates
 112         if (enc instanceof sun.nio.cs.ArrayEncoder &&
 113             cs.contains(Charset.forName("ASCII"))) {
 114             if (cs.name().equals("UTF-8"))    // utf8 handles surrogates
 115                 return;
 116             enc.replaceWith(new byte[] { (byte)'A'});
 117             sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;
 118 
 119             String str = "ab\uD800\uDC00\uD800\uDC00cd";
 120             byte[] ba = new byte[str.length() - 2];
 121             int n = cae.encode(str.toCharArray(), 0, str.length(), ba);
 122             if (n != 6 || !"abAAcd".equals(new String(ba, cs.name())))
 123                 throw new RuntimeException("encode1(surrogates) failed  -> "
 124                                            + cs.name());
 125 
 126             ba = new byte[str.length()];
 127             n = cae.encode(str.toCharArray(), 0, str.length(), ba);
 128             if (n != 6 || !"abAAcd".equals(new String(ba, 0, n,
 129                                                      cs.name())))
 130                 throw new RuntimeException("encode2(surrogates) failed  -> "
 131                                            + cs.name());
 132             str = "ab\uD800B\uDC00Bcd";
 133             ba = new byte[str.length()];
 134             n = cae.encode(str.toCharArray(), 0, str.length(), ba);
 135             if (n != 8 || !"abABABcd".equals(new String(ba, 0, n,