FileDocCategorySizeDatePackage
URLEncoderTest.javaAPI DocAndroid 1.5 API4561Wed May 06 22:41:04 BST 2009tests.api.java.net

URLEncoderTest

public class URLEncoderTest extends TestCase

Fields Summary
Constructors Summary
Methods Summary
protected voidsetUp()
Sets up the fixture, for example, open a network connection. This method is called before a test is executed.

    
protected voidtearDown()
Tears down the fixture, for example, close a network connection. This method is called after a test is executed.

    
public voidtest_encodeLjava_lang_String()

tests
java.net.URLEncoder#encode(java.lang.String)

        // Test for method java.lang.String
        // java.net.URLEncoder.encode(java.lang.String)
        final String URL = "http://" + Support_Configuration.HomeAddress;
        final String URL2 = "telnet://justWantToHaveFun.com:400";
        final String URL3 = "file://myServer.org/a file with spaces.jpg";
        try {
            assertTrue("1. Incorrect encoding/decoding", URLDecoder.decode(
                    URLEncoder.encode(URL)).equals(URL));
            assertTrue("2. Incorrect encoding/decoding", URLDecoder.decode(
                    URLEncoder.encode(URL2)).equals(URL2));
            assertTrue("3. Incorrect encoding/decoding", URLDecoder.decode(
                    URLEncoder.encode(URL3)).equals(URL3));
        } catch (Exception e) {
            fail("Exception during test : " + e.getMessage());
        }
    
public voidtest_encodeLjava_lang_StringLjava_lang_String()

       
        String enc = "UTF-8";
        
        String [] urls = {"http://" + Support_Configuration.HomeAddress + 
                              "/test?hl=en&q=te st",
                              "file://a b/c/d.e-f*g_ l",
                              "jar:file://a.jar !/b.c/\u1052",
                              "ftp://test:pwd@localhost:2121/%D0%9C"};
        
        String [] expected = { "http%3A%2F%2Fjcltest.apache.org%2Ftest%3Fhl%" +
                "3Den%26q%3Dte+st", 
                "file%3A%2F%2Fa+b%2Fc%2Fd.e-f*g_+l",
                "jar%3Afile%3A%2F%2Fa.jar+%21%2Fb.c%2F%E1%81%92"};        

        for(int i = 0; i < urls.length-1; i++) {
            try {
                String encodedString = URLEncoder.encode(urls[i], enc);
                assertEquals(expected[i], encodedString);
                assertEquals(urls[i], URLDecoder.decode(encodedString, enc));
            } catch (UnsupportedEncodingException e) {
                fail("UnsupportedEncodingException: " + e.getMessage());
            }
        }
        
        try {
            String encodedString = URLEncoder.encode(urls[urls.length - 1], enc);
            assertEquals(urls[urls.length - 1], URLDecoder.decode(encodedString, enc));
        } catch (UnsupportedEncodingException e) {
            fail("UnsupportedEncodingException: " + e.getMessage());
        }
        
        try {
            URLDecoder.decode("", "");
            fail("UnsupportedEncodingException expected");
        } catch (UnsupportedEncodingException e) {
            //expected
        }