17ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes/*
27ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  Licensed to the Apache Software Foundation (ASF) under one or more
37ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  contributor license agreements.  See the NOTICE file distributed with
47ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  this work for additional information regarding copyright ownership.
57ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  The ASF licenses this file to You under the Apache License, Version 2.0
67ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  (the "License"); you may not use this file except in compliance with
77ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  the License.  You may obtain a copy of the License at
87ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
107ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *
117ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  Unless required by applicable law or agreed to in writing, software
127ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
137ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
147ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  See the License for the specific language governing permissions and
157ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes *  limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage org.apache.harmony.luni.tests.java.net;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.UnsupportedEncodingException;
217ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughesimport java.net.URLDecoder;
22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.net.URLEncoder;
2332559028b14b9b321b10eede050afd554a376569Jesse Wilsonimport java.nio.charset.UnsupportedCharsetException;
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport junit.framework.TestCase;
257ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughesimport tests.support.Support_Configuration;
26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic class URLEncoderTest extends TestCase {
287ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes
297ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes    /**
30229e34b182b98e1dba15d3dc6341954986ae2b7aBrian Carlstrom     * java.net.URLEncoder#encode(java.lang.String)
317ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes     */
327ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes    @SuppressWarnings("deprecation")
337ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes    public void test_encodeLjava_lang_String() {
347ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        final String URL = "http://" + Support_Configuration.HomeAddress;
357ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        final String URL2 = "telnet://justWantToHaveFun.com:400";
367ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        final String URL3 = "file://myServer.org/a file with spaces.jpg";
377ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes
387ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        assertTrue("1. Incorrect encoding/decoding", URLDecoder.decode(
397ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes                URLEncoder.encode(URL)).equals(URL));
407ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        assertTrue("2. Incorrect encoding/decoding", URLDecoder.decode(
417ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes                URLEncoder.encode(URL2)).equals(URL2));
427ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        assertTrue("3. Incorrect encoding/decoding", URLDecoder.decode(
437ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes                URLEncoder.encode(URL3)).equals(URL3));
447ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes    }
457ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes
46adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
47229e34b182b98e1dba15d3dc6341954986ae2b7aBrian Carlstrom     * URLEncoder#encode(String, String)
48adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
497ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes    public void test_encodeLjava_lang_StringLjava_lang_String()
507ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes            throws Exception {
51adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        // Regression for HARMONY-24
52adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
537ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes            URLEncoder.encode("str", "unknown_enc");
54adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            fail("Assert 0: Should throw UEE for invalid encoding");
55adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } catch (UnsupportedEncodingException e) {
5632559028b14b9b321b10eede050afd554a376569Jesse Wilson        } catch (UnsupportedCharsetException e) {
57adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            // expected
587ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        }
597ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes
607ee3a061452c5a7e5c8e661219a1f08a14171858Elliott Hughes        // Regression for HARMONY-1233
61adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
62adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            URLEncoder.encode(null, "harmony");
63adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            fail("NullPointerException expected");
64b22ef1e880d100200fdf7f995f58b8227363c8b6Jesse Wilson        } catch (NullPointerException expected) {
65b22ef1e880d100200fdf7f995f58b8227363c8b6Jesse Wilson        } catch (UnsupportedCharsetException expected) {
66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
67adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
687d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes
697d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes    // http://b/11571917
707d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes    public void test11571917() throws Exception {
717d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes        assertEquals("%82%A0", URLEncoder.encode("あ", "Shift_JIS"));
727d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes        assertEquals("%82%A9", URLEncoder.encode("か", "Shift_JIS"));
737d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes        assertEquals("%97%43", URLEncoder.encode("佑", "Shift_JIS"));
747d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes        assertEquals("%24", URLEncoder.encode("$", "Shift_JIS"));
757d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes        assertEquals("%E3%81%8B", URLEncoder.encode("か", "UTF-8"));
767d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes
777d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes        assertEquals("%82%A0%82%A9%97%43%24%E3%81%8B", URLEncoder.encode("あ", "Shift_JIS") +
787d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes            URLEncoder.encode("か", "Shift_JIS") +
797d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes            URLEncoder.encode("佑", "Shift_JIS") +
807d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes            URLEncoder.encode("$", "Shift_JIS") +
817d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes            URLEncoder.encode("か", "UTF-8"));
827d0998187acb4f0e0ddc030943936c36429efaf1Elliott Hughes    }
83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
84