Path: blob/master/test/jdk/java/net/URLEncoder/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 the same34* 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 = "encode")44public Object[][] getDecodeParameters() {45return new Object[][]{46{"The string \u00FC@foo-bar"},47// the string from javadoc example4849{""}, // an empty string5051{"x"}, // a string of length 15253{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.*"},54// the string of characters should remain the same5556{charactersRange('\u0000', '\u007F')},57// a string of characters from 0 to 1275859{charactersRange('\u0080', '\u00FF')},60// a string of characters from 128 to 2556162{"\u0100 \u0101 \u0555 \u07FD \u07FF"},63// a string of Unicode values can be expressed as 2 bytes6465{"\u8000 \u8001 \uA000 \uFFFD \uFFFF"}, // a string of Unicode values can be expressed as 3 bytes66};67}6869/**70* Verifies that the new overload method returns the same result as the71* existing method.72*73* @param s the string to be encoded74* @throws Exception if the test fails75*/76@Test(dataProvider = "encode")77public void encode(String s) throws Exception {78String encoded1 = URLEncoder.encode(s, StandardCharsets.UTF_8.name());79String encoded2 = URLEncoder.encode(s, StandardCharsets.UTF_8);80Assert.assertEquals(encoded1, encoded2);8182// cross check83String returned1 = URLDecoder.decode(encoded1, StandardCharsets.UTF_8.name());84String returned2 = URLDecoder.decode(encoded2, StandardCharsets.UTF_8);85Assert.assertEquals(returned1, returned2);86}8788String charactersRange(char c1, char c2) {89StringBuilder sb = new StringBuilder(c2 - c1);90for (char c = c1; c < c2; c++) {91sb.append(c);92}9394return sb.toString();95}96}979899