jdk/test/java/net/URLEncoder/URLEncodeDecode.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) 2000, 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 4257115
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
 * characters within and beyond the 8859-1 range.
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
public class URLEncodeDecode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static char chars[] = {'H', 'e', 'l', 'l', 'o',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                           ' ', '+', '%',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                           '-', '_', '.',       '!', '~', '*', '\'', '(',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                           ')',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                           '@',
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                           '\u00ae', '\u0101', '\u10a0'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static String str = new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static String correctEncodedUTF8 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        "Hello+%2B%25-_.%21%7E*%27%28%29%40%C2%AE%C4%81%E1%82%A0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println("Constructed the string: " + str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        System.out.println("The Unicode bytes are: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                           getHexBytes(str));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        test("UTF-8", correctEncodedUTF8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static void test(String enc, String correctEncoded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        throws Exception{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        String encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        String outStr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (enc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            encoded = URLEncoder.encode(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            outStr = "default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            encoded = URLEncoder.encode(str, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            outStr = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println("URLEncode it ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                           + outStr + ") : " + encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.println("The Unicode bytes are: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                           getHexBytes(encoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (encoded.equals(correctEncoded))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("The encoding is correct!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new Exception("The encoding is incorrect!" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                " It should be " + correctEncoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String decoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (enc == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            decoded = URLDecoder.decode(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            decoded = URLDecoder.decode(encoded, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.out.println("URLDecode it ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                           + outStr + ") : " + decoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.out.println("The Unicode bytes are: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                           getHexBytes(decoded));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (str.equals(decoded))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            System.out.println("The decoding is correct");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new Exception("The decoded is not equal to the original");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static String getHexBytes(String s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        for (int i = 0; i < s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int a = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int b1 = (a >>8) & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int b2 = (byte)a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int b11 = (b1>>4) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int b12 = b1 & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            int b21 = (b2 >>4) & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int b22 = b2 & 0x0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            sb.append(Integer.toHexString(b11));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            sb.append(Integer.toHexString(b12));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            sb.append(Integer.toHexString(b21));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            sb.append(Integer.toHexString(b22));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}