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 package test.java.net.URLEncoder; 24 25 import java.net.URLDecoder; 26 import java.net.URLEncoder; 27 import java.nio.charset.StandardCharsets; 28 import org.testng.Assert; 29 import org.testng.annotations.DataProvider; 30 import org.testng.annotations.Test; 31 32 /** 33 * @test 34 * @bug 8183743 35 * @summary Test to verify the new overload method with Charset functions the same 36 * as the existing method that takes a charset name. 37 * @run testng EncodingTest 38 */ 39 public class EncodingTest { 40 41 @DataProvider(name = "encode") getDecodeParameters()42 public Object[][] getDecodeParameters() { 43 return new Object[][]{ 44 {"The string \u00FC@foo-bar"}, 45 // the string from javadoc example 46 47 {""}, // an empty string 48 49 {"x"}, // a string of length 1 50 51 {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"}, 52 // the string of characters should remain the same 53 54 {charactersRange('\u0000', '\u007F')}, 55 // a string of characters from 0 to 127 56 57 {charactersRange('\u0080', '\u00FF')}, 58 // a string of characters from 128 to 255 59 60 {"\u0100 \u0101 \u0555 \u07FD \u07FF"}, 61 // a string of Unicode values can be expressed as 2 bytes 62 63 {"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes 64 }; 65 } 66 67 /** 68 * Verifies that the new overload method returns the same result as the 69 * existing method. 70 * 71 * @param s the string to be encoded 72 * @throws Exception if the test fails 73 */ 74 @Test(dataProvider = "encode") encode(String s)75 public void encode(String s) throws Exception { 76 String encoded1 = URLEncoder.encode(s, StandardCharsets.UTF_8.name()); 77 String encoded2 = URLEncoder.encode(s, StandardCharsets.UTF_8); 78 Assert.assertEquals(encoded1, encoded2); 79 80 // cross check 81 String returned1 = URLDecoder.decode(encoded1, StandardCharsets.UTF_8.name()); 82 String returned2 = URLDecoder.decode(encoded2, StandardCharsets.UTF_8); 83 Assert.assertEquals(returned1, returned2); 84 } 85 charactersRange(char c1, char c2)86 String charactersRange(char c1, char c2) { 87 StringBuilder sb = new StringBuilder(c2 - c1); 88 for (char c = c1; c < c2; c++) { 89 sb.append(c); 90 } 91 92 return sb.toString(); 93 } 94 }