0N/A/*
2362N/A * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4257115
0N/A * @summary Test URL encoder and decoder on a string that contains
0N/A * characters within and beyond the 8859-1 range.
0N/A *
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/A
0N/Apublic class URLEncodeDecode {
0N/A
0N/A static char chars[] = {'H', 'e', 'l', 'l', 'o',
0N/A ' ', '+', '%',
0N/A '-', '_', '.', '!', '~', '*', '\'', '(',
0N/A ')',
0N/A '@',
0N/A '\u00ae', '\u0101', '\u10a0'};
0N/A
0N/A static String str = new String(chars);
0N/A
0N/A static String correctEncodedUTF8 =
0N/A "Hello+%2B%25-_.%21%7E*%27%28%29%40%C2%AE%C4%81%E1%82%A0";
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A System.out.println("Constructed the string: " + str);
0N/A System.out.println("The Unicode bytes are: " +
0N/A getHexBytes(str));
0N/A System.out.println("");
0N/A test("UTF-8", correctEncodedUTF8);
0N/A }
0N/A
0N/A private static void test(String enc, String correctEncoded)
0N/A throws Exception{
0N/A
0N/A String encoded = null;
0N/A String outStr = null;
0N/A
0N/A if (enc == null) {
0N/A encoded = URLEncoder.encode(str);
0N/A outStr = "default";
0N/A }
0N/A else {
0N/A encoded = URLEncoder.encode(str, enc);
0N/A outStr = enc;
0N/A }
0N/A
0N/A System.out.println("URLEncode it ("
0N/A + outStr + ") : " + encoded);
0N/A System.out.println("The Unicode bytes are: " +
0N/A getHexBytes(encoded));
0N/A
0N/A if (encoded.equals(correctEncoded))
0N/A System.out.println("The encoding is correct!");
0N/A else {
0N/A throw new Exception("The encoding is incorrect!" +
0N/A " It should be " + correctEncoded);
0N/A }
0N/A System.out.println("");
0N/A
0N/A String decoded = null;
0N/A
0N/A if (enc == null)
0N/A decoded = URLDecoder.decode(encoded);
0N/A else
0N/A decoded = URLDecoder.decode(encoded, enc);
0N/A
0N/A System.out.println("URLDecode it ("
0N/A + outStr + ") : " + decoded);
0N/A System.out.println("The Unicode bytes are: " +
0N/A getHexBytes(decoded));
0N/A
0N/A if (str.equals(decoded))
0N/A System.out.println("The decoding is correct");
0N/A else {
0N/A throw new Exception("The decoded is not equal to the original");
0N/A }
0N/A
0N/A }
0N/A
0N/A private static String getHexBytes(String s) throws Exception {
0N/A StringBuffer sb = new StringBuffer();
0N/A for (int i = 0; i < s.length(); i++) {
0N/A
0N/A int a = s.charAt(i);
0N/A int b1 = (a >>8) & 0xff;
0N/A int b2 = (byte)a;
0N/A int b11 = (b1>>4) & 0x0f;
0N/A int b12 = b1 & 0x0f;
0N/A int b21 = (b2 >>4) & 0x0f;
0N/A int b22 = b2 & 0x0f;
0N/A
0N/A sb.append(Integer.toHexString(b11));
0N/A sb.append(Integer.toHexString(b12));
0N/A sb.append(Integer.toHexString(b21));
0N/A sb.append(Integer.toHexString(b22));
0N/A sb.append(' ');
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A}