Path: blob/master/test/jdk/java/net/URLDecoder/EncodingTest.java
41149 views
/*1* Copyright (c) 2017, 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*/2223import java.net.URLDecoder;24import java.net.URLEncoder;25import java.nio.charset.StandardCharsets;26import org.testng.Assert;27import org.testng.annotations.DataProvider;28import org.testng.annotations.Test;2930/**31* @test32* @bug 818374333* @summary Test to verify the new overload method with Charset functions the34* same as the existing method that takes a charset name.35* @run testng EncodingTest36*/37public class EncodingTest {38public static enum ParameterType {39STRING,40CHARSET41}4243@DataProvider(name = "illegalArgument")44public Object[][] getParameters() {45return new Object[][]{46{ParameterType.STRING},47{ParameterType.CHARSET}48};49}5051@DataProvider(name = "decode")52public Object[][] getDecodeParameters() {53return new Object[][]{54{"The string \u00FC@foo-bar"},55// the string from javadoc example5657{""}, // an empty string5859{"x"}, // a string of length 16061{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"},62// the string of characters should remain the same6364{charactersRange('\u0000', '\u007F')},65// a string of characters from 0 to 1276667{charactersRange('\u0080', '\u00FF')},68// a string of characters from 128 to 2556970{"\u0100 \u0101 \u0555 \u07FD \u07FF"},71// a string of Unicode values can be expressed as 2 bytes7273{"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes74};75}7677/**78* Verifies that IAE is thrown when decoding an invalid string using the79* existing method or the new overload method.80*81* @param type the type of the argument, e.g a String charset name or82* charset83*/84@Test(dataProvider = "illegalArgument", expectedExceptions = IllegalArgumentException.class)85public void testIllegalArgument(ParameterType type) throws Exception {86String encoded = URLEncoder.encode("http://www.xyz.com/find?key=\u0100\u0101",87StandardCharsets.UTF_8.name());88String illegal = "%" + encoded;89String returned;90if (type == ParameterType.STRING) {91returned = URLDecoder.decode(illegal, StandardCharsets.UTF_8.name());92} else {93returned = URLDecoder.decode(illegal, StandardCharsets.UTF_8);94}95}9697/**98* Verifies that the returned values of decoding with the existing99* and the overload methods match.100*101* @param s the string to be encoded and then decoded with both existing102* and the overload methods.103* @throws Exception104*/105@Test(dataProvider = "decode")106public void decode(String s) throws Exception {107String encoded = URLEncoder.encode(s, StandardCharsets.UTF_8.name());108String returned1 = URLDecoder.decode(encoded, StandardCharsets.UTF_8.name());109String returned2 = URLDecoder.decode(encoded, StandardCharsets.UTF_8);110Assert.assertEquals(returned1, returned2);111}112113String charactersRange(char c1, char c2) {114StringBuilder sb = new StringBuilder(c2 - c1);115for (char c = c1; c < c2; c++) {116sb.append(c);117}118119return sb.toString();120}121}122123124