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