jdk/test/java/net/URLEncoder/SurrogatePairs.java
author jjg
Wed, 04 Jan 2017 18:33:20 -0800
changeset 43029 1cd1c816581e
parent 5506 202f599c92aa
permissions -rw-r--r--
8172260: remove tests from ProblemList Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4396708
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test URL encoder and decoder on a string that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * surrogate pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Surrogate pairs are two character Unicode sequences where the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * character lies in the range [d800, dbff] and the second character lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * in the range [dc00, dfff]. They are used as an escaping mechanism to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * 1M more characters to Unicode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class SurrogatePairs {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static String[] testStrings = {"\uD800\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                   "\uD800\uDFFF",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                                   "\uDBFF\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                   "\uDBFF\uDFFF",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                                   "1\uDBFF\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                   "@\uDBFF\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                   "\uDBFF\uDC001",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                   "\uDBFF\uDC00@",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                   "\u0101\uDBFF\uDC00",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                   "\uDBFF\uDC00\u0101"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static String[] correctEncodings = {"%F0%90%80%80",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                        "%F0%90%8F%BF",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                        "%F4%8F%B0%80",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                        "%F4%8F%BF%BF",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                        "1%F4%8F%B0%80",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                        "%40%F4%8F%B0%80",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                        "%F4%8F%B0%801",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                        "%F4%8F%B0%80%40",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                        "%C4%81%F4%8F%B0%80",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                        "%F4%8F%B0%80%C4%81"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (int i=0; i < testStrings.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            test(testStrings[i], correctEncodings[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static void test(String str, String correctEncoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println("Unicode bytes of test string are: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                           + getHexBytes(str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String encoded = URLEncoder.encode(str, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.out.println("URLEncoding is: " + encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (encoded.equals(correctEncoding))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            System.out.println("The encoding is correct!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new Exception("The encoding is incorrect!" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                " It should be " + correctEncoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        String decoded = URLDecoder.decode(encoded, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.out.println("Unicode bytes for URLDecoding are: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           + getHexBytes(decoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (str.equals(decoded))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            System.out.println("The decoding is correct");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new Exception("The decoded is not equal to the original");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.out.println("---");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static String getHexBytes(String s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int a = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            int b1 = (a >>8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int b2 = (byte)a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int b11 = (b1>>4) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int b12 = b1 & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int b21 = (b2 >>4) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int b22 = b2 & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            sb.append(Integer.toHexString(b11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            sb.append(Integer.toHexString(b12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            sb.append(Integer.toHexString(b21));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            sb.append(Integer.toHexString(b22));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}