URLEncoderTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.net; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24 25import java.io.UnsupportedEncodingException; 26import java.net.URLDecoder; 27import java.net.URLEncoder; 28 29import tests.support.Support_Configuration; 30 31@TestTargetClass(URLEncoder.class) 32public class URLEncoderTest extends junit.framework.TestCase { 33 34 /** 35 * @tests java.net.URLEncoder#encode(java.lang.String) 36 */ 37 @TestTargetNew( 38 level = TestLevel.COMPLETE, 39 notes = "", 40 method = "encode", 41 args = {java.lang.String.class} 42 ) 43 public void test_encodeLjava_lang_String() { 44 // Test for method java.lang.String 45 // java.net.URLEncoder.encode(java.lang.String) 46 final String URL = "http://" + Support_Configuration.HomeAddress; 47 final String URL2 = "telnet://justWantToHaveFun.com:400"; 48 final String URL3 = "file://myServer.org/a file with spaces.jpg"; 49 try { 50 assertTrue("1. Incorrect encoding/decoding", URLDecoder.decode( 51 URLEncoder.encode(URL)).equals(URL)); 52 assertTrue("2. Incorrect encoding/decoding", URLDecoder.decode( 53 URLEncoder.encode(URL2)).equals(URL2)); 54 assertTrue("3. Incorrect encoding/decoding", URLDecoder.decode( 55 URLEncoder.encode(URL3)).equals(URL3)); 56 } catch (Exception e) { 57 fail("Exception during test : " + e.getMessage()); 58 } 59 } 60 61 @TestTargetNew( 62 level = TestLevel.COMPLETE, 63 notes = "", 64 method = "encode", 65 args = {java.lang.String.class, java.lang.String.class} 66 ) 67 public void test_encodeLjava_lang_StringLjava_lang_String() { 68 69 String enc = "UTF-8"; 70 71 String [] urls = {"http://" + Support_Configuration.HomeAddress + 72 "/test?hl=en&q=te st", 73 "file://a b/c/d.e-f*g_ l", 74 "jar:file://a.jar !/b.c/\u1052", 75 "ftp://test:pwd@localhost:2121/%D0%9C"}; 76 77 String [] expected = { "http%3A%2F%2Fjcltest.apache.org%2Ftest%3Fhl%" + 78 "3Den%26q%3Dte+st", 79 "file%3A%2F%2Fa+b%2Fc%2Fd.e-f*g_+l", 80 "jar%3Afile%3A%2F%2Fa.jar+%21%2Fb.c%2F%E1%81%92"}; 81 82 for(int i = 0; i < urls.length-1; i++) { 83 try { 84 String encodedString = URLEncoder.encode(urls[i], enc); 85 assertEquals(expected[i], encodedString); 86 assertEquals(urls[i], URLDecoder.decode(encodedString, enc)); 87 } catch (UnsupportedEncodingException e) { 88 fail("UnsupportedEncodingException: " + e.getMessage()); 89 } 90 } 91 92 try { 93 String encodedString = URLEncoder.encode(urls[urls.length - 1], enc); 94 assertEquals(urls[urls.length - 1], URLDecoder.decode(encodedString, enc)); 95 } catch (UnsupportedEncodingException e) { 96 fail("UnsupportedEncodingException: " + e.getMessage()); 97 } 98 99 try { 100 URLDecoder.decode("", ""); 101 fail("UnsupportedEncodingException expected"); 102 } catch (UnsupportedEncodingException e) { 103 //expected 104 } 105 106 } 107 108 /** 109 * Sets up the fixture, for example, open a network connection. This method 110 * is called before a test is executed. 111 */ 112 protected void setUp() { 113 } 114 115 /** 116 * Tears down the fixture, for example, close a network connection. This 117 * method is called after a test is executed. 118 */ 119 protected void tearDown() { 120 } 121} 122