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 org.apache.harmony.luni.tests.java.net; 19 20import java.io.UnsupportedEncodingException; 21import java.net.URLDecoder; 22import java.net.URLEncoder; 23 24import junit.framework.TestCase; 25import tests.support.Support_Configuration; 26 27public class URLEncoderTest extends TestCase { 28 29 /** 30 * @tests java.net.URLEncoder#encode(java.lang.String) 31 */ 32 @SuppressWarnings("deprecation") 33 public void test_encodeLjava_lang_String() { 34 final String URL = "http://" + Support_Configuration.HomeAddress; 35 final String URL2 = "telnet://justWantToHaveFun.com:400"; 36 final String URL3 = "file://myServer.org/a file with spaces.jpg"; 37 38 assertTrue("1. Incorrect encoding/decoding", URLDecoder.decode( 39 URLEncoder.encode(URL)).equals(URL)); 40 assertTrue("2. Incorrect encoding/decoding", URLDecoder.decode( 41 URLEncoder.encode(URL2)).equals(URL2)); 42 assertTrue("3. Incorrect encoding/decoding", URLDecoder.decode( 43 URLEncoder.encode(URL3)).equals(URL3)); 44 } 45 46 /** 47 * @tests URLEncoder#encode(String, String) 48 */ 49 public void test_encodeLjava_lang_StringLjava_lang_String() 50 throws Exception { 51 // Regression for HARMONY-24 52 try { 53 URLEncoder.encode("str", "unknown_enc"); 54 fail("Assert 0: Should throw UEE for invalid encoding"); 55 } catch (UnsupportedEncodingException e) { 56 // expected 57 } 58 59 // Regression for HARMONY-1233 60 try { 61 URLEncoder.encode(null, "harmony"); 62 fail("NullPointerException expected"); 63 } catch (NullPointerException e) { 64 // expected 65 } 66 } 67} 68