Path: blob/master/test/jdk/java/net/URLEncoder/URLEncodeDecode.java
41152 views
/*1* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 425711526* @summary Test URL encoder and decoder on a string that contains27* characters within and beyond the 8859-1 range.28*29*/3031import java.io.*;32import java.net.*;3334public class URLEncodeDecode {3536static char chars[] = {'H', 'e', 'l', 'l', 'o',37' ', '+', '%',38'-', '_', '.', '!', '~', '*', '\'', '(',39')',40'@',41'\u00ae', '\u0101', '\u10a0'};4243static String str = new String(chars);4445static String correctEncodedUTF8 =46"Hello+%2B%25-_.%21%7E*%27%28%29%40%C2%AE%C4%81%E1%82%A0";4748public static void main(String[] args) throws Exception {4950System.out.println("Constructed the string: " + str);51System.out.println("The Unicode bytes are: " +52getHexBytes(str));53System.out.println("");54test("UTF-8", correctEncodedUTF8);55}5657private static void test(String enc, String correctEncoded)58throws Exception{5960String encoded = null;61String outStr = null;6263if (enc == null) {64encoded = URLEncoder.encode(str);65outStr = "default";66}67else {68encoded = URLEncoder.encode(str, enc);69outStr = enc;70}7172System.out.println("URLEncode it ("73+ outStr + ") : " + encoded);74System.out.println("The Unicode bytes are: " +75getHexBytes(encoded));7677if (encoded.equals(correctEncoded))78System.out.println("The encoding is correct!");79else {80throw new Exception("The encoding is incorrect!" +81" It should be " + correctEncoded);82}83System.out.println("");8485String decoded = null;8687if (enc == null)88decoded = URLDecoder.decode(encoded);89else90decoded = URLDecoder.decode(encoded, enc);9192System.out.println("URLDecode it ("93+ outStr + ") : " + decoded);94System.out.println("The Unicode bytes are: " +95getHexBytes(decoded));9697if (str.equals(decoded))98System.out.println("The decoding is correct");99else {100throw new Exception("The decoded is not equal to the original");101}102103}104105private static String getHexBytes(String s) throws Exception {106StringBuffer sb = new StringBuffer();107for (int i = 0; i < s.length(); i++) {108109int a = s.charAt(i);110int b1 = (a >>8) & 0xff;111int b2 = (byte)a;112int b11 = (b1>>4) & 0x0f;113int b12 = b1 & 0x0f;114int b21 = (b2 >>4) & 0x0f;115int b22 = b2 & 0x0f;116117sb.append(Integer.toHexString(b11));118sb.append(Integer.toHexString(b12));119sb.append(Integer.toHexString(b21));120sb.append(Integer.toHexString(b22));121sb.append(' ');122}123return sb.toString();124}125126}127128129