TestStringCoding.java 6.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
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 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
 */

/* @test
   @bug 6636323 6636319
   @summary Test if StringCoding and NIO result have the same de/encoding result
 * @run main/timeout=2000 TestStringCoding
 */

import java.util.*;
import java.nio.*;
import java.nio.charset.*;

public class TestStringCoding {
    public static void main(String[] args) throws Throwable {

        for (Boolean hasSM: new boolean[] { false, true }) {
            if (hasSM)
                System.setSecurityManager(new PermissiveSecurityManger());
            for (Charset cs:  Charset.availableCharsets().values()) {
                if ("ISO-2022-CN".equals(cs.name()) ||
                    "x-COMPOUND_TEXT".equals(cs.name()) ||
                    "x-JISAutoDetect".equals(cs.name()))
                    continue;
                System.out.printf("Testing(sm=%b) " + cs.name() + "....", hasSM);
                // full bmp first
                char[] bmpCA = new char[0x10000];
                for (int i = 0; i < 0x10000; i++) {
                     bmpCA[i] = (char)i;
                }
                byte[] sbBA = new byte[0x100];
                for (int i = 0; i < 0x100; i++) {
                    sbBA[i] = (byte)i;
                }
                test(cs, bmpCA, sbBA);
                // "randomed" sizes
                Random rnd = new Random();
                for (int i = 0; i < 10; i++) {
                    int clen = rnd.nextInt(0x10000);
                    int blen = rnd.nextInt(0x100);
                    //System.out.printf("    blen=%d, clen=%d%n", blen, clen);
                    test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
                    //add a pair of surrogates
                    int pos = clen / 2;
                    if ((pos + 1) < blen) {
                        bmpCA[pos] = '\uD800';
                        bmpCA[pos+1] = '\uDC00';
                    }
                    test(cs, Arrays.copyOf(bmpCA, clen), Arrays.copyOf(sbBA, blen));
                }
                System.out.println("done!");
            }
        }
    }

    static void test(Charset cs, char[] bmpCA, byte[] sbBA) throws Throwable {
        String bmpStr = new String(bmpCA);
        CharsetDecoder dec = cs.newDecoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
        CharsetEncoder enc = cs.newEncoder()
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);

        //getBytes(csn);
        byte[] baSC = bmpStr.getBytes(cs.name());
        ByteBuffer bf = enc.reset().encode(CharBuffer.wrap(bmpCA));
        byte[] baNIO = new byte[bf.limit()];
        bf.get(baNIO, 0, baNIO.length);
        if (!Arrays.equals(baSC, baNIO))
            throw new RuntimeException("getBytes(csn) failed  -> " + cs.name());

        //getBytes(cs);
        baSC = bmpStr.getBytes(cs);
        if (!Arrays.equals(baSC, baNIO))
            throw new RuntimeException("getBytes(cs) failed  -> " + cs.name());

        //new String(csn);
        String strSC = new String(sbBA, cs.name());
        String strNIO = dec.reset().decode(ByteBuffer.wrap(sbBA)).toString();
        if(!strNIO.equals(strSC))
            throw new RuntimeException("new String(csn) failed  -> " + cs.name());

        //new String(cs);
        strSC = new String(sbBA, cs);
        if (!strNIO.equals(strSC))
            throw new RuntimeException("new String(cs) failed  -> " + cs.name());

        //encode unmappable surrogates
        if (enc instanceof sun.nio.cs.ArrayEncoder &&
            cs.contains(Charset.forName("ASCII"))) {
            enc.replaceWith(new byte[] { (byte)'A'});
            sun.nio.cs.ArrayEncoder cae = (sun.nio.cs.ArrayEncoder)enc;

            String str = "ab\uD800\uDC00\uD800\uDC00cd";
            byte[] ba = new byte[str.length() - 2];
            int n = cae.encode(str.toCharArray(), 0, str.length(), ba);
            if (n != 6 || !"abAAcd".equals(new String(ba, cs.name())))
                throw new RuntimeException("encode1(surrogates) failed  -> "
                                           + cs.name());

            ba = new byte[str.length()];
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
            if (n != 6 || !"abAAcd".equals(new String(ba, 0, n,
                                                     cs.name())))
                throw new RuntimeException("encode2(surrogates) failed  -> "
                                           + cs.name());
            str = "ab\uD800B\uDC00Bcd";
            ba = new byte[str.length()];
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
            if (n != 8 || !"abABABcd".equals(new String(ba, 0, n,
                                                       cs.name())))
                throw new RuntimeException("encode3(surrogates) failed  -> "
                                           + cs.name());

            ba = new byte[str.length() - 1];
            n = cae.encode(str.toCharArray(), 0, str.length(), ba);
            if (n != 7 || !"abABABc".equals(new String(ba, 0, n,
                                                      cs.name())))
                throw new RuntimeException("encode4(surrogates) failed  -> "
                                           + cs.name());
        }

    }

    static class PermissiveSecurityManger extends SecurityManager {
        @Override public void checkPermission(java.security.Permission p) {}
    }
}