1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.net.URLDecoder; 25 import java.net.URLEncoder; 26 import java.nio.charset.StandardCharsets; 27 import org.testng.Assert; 28 import org.testng.annotations.DataProvider; 29 import org.testng.annotations.Test; 30 31 /** 32 * @test 33 * @bug 8183743 34 * @summary Test to verify the new overload method with Charset functions the same 35 * as the existing method that takes a charset name. 36 * @run testng EncodingTest 37 */ 38 public class EncodingTest { 39 public static enum ParameterType { 40 STRING, 41 CHARSET 42 } 43 44 @DataProvider(name = "encode") getDecodeParameters()45 public Object[][] getDecodeParameters() { 46 return new Object[][]{ 47 {"The string \u00FC@foo-bar"}, 48 // the string from javadoc example 49 50 {""}, // an empty string 51 52 {"x"}, // a string of length 1 53 54 {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"}, 55 // the string of characters should remain the same 56 57 {charactersRange('\u0000', '\u007F')}, 58 // a string of characters from 0 to 127 59 60 {charactersRange('\u0080', '\u00FF')}, 61 // a string of characters from 128 to 255 62 63 {"\u0100 \u0101 \u0555 \u07FD \u07FF"}, 64 // a string of Unicode values can be expressed as 2 bytes 65 66 {"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes 67 }; 68 } 69 70 /** 71 * Verifies that the new overload method returns the same result as the 72 * existing method. 73 * 74 * @param s the string to be encoded 75 * @throws Exception if the test fails 76 */ 77 @Test(dataProvider = "encode") encode(String s)78 public void encode(String s) throws Exception { 79 String encoded1 = URLEncoder.encode(s, StandardCharsets.UTF_8.name()); 80 String encoded2 = URLEncoder.encode(s, StandardCharsets.UTF_8); 81 Assert.assertEquals(encoded1, encoded2); 82 83 // cross check 84 String returned1 = URLDecoder.decode(encoded1, StandardCharsets.UTF_8.name()); 85 String returned2 = URLDecoder.decode(encoded2, StandardCharsets.UTF_8); 86 Assert.assertEquals(returned1, returned2); 87 } 88 charactersRange(char c1, char c2)89 String charactersRange(char c1, char c2) { 90 StringBuilder sb = new StringBuilder(c2 - c1); 91 for (char c = c1; c < c2; c++) { 92 sb.append(c); 93 } 94 95 return sb.toString(); 96 } 97 } 98