KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestURI


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestURI.java,v 1.4.2.6 2004/05/14 09:47:36 oglueck Exp $
3  * $Revision: 1.4.2.6 $
4  * $Date: 2004/05/14 09:47:36 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2003-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 /**
38  * Simple tests for the URI class.
39  *
40  * @author Michael Becke
41  */

42 public class TestURI extends TestNoHost {
43
44     /**
45      * Constructor for TestURI.
46      * @param testName
47      */

48     public TestURI(String JavaDoc testName) {
49         super(testName);
50     }
51     
52     public static Test suite() {
53         return new TestSuite(TestURI.class);
54     }
55     
56     public void testIPv4Address() throws URIException {
57
58         URI base = new URI("http://10.0.1.10:8830");
59         
60         URI uri = base;
61         assertTrue("Should be an IPv4 address", uri.isIPv4address());
62            
63         uri = new URI(base, "/04-1.html");
64         assertTrue("Should be an IPv4 address", uri.isIPv4address());
65
66         uri = new URI("/04-1.html");
67         assertFalse("Should NOT be an IPv4 address", uri.isIPv4address());
68
69         uri = new URI(base, "http://10.0.1.10:8830/04-1.html");
70         assertTrue("Should be an IPv4 address", uri.isIPv4address());
71
72         uri = new URI("http://10.0.1.10:8830/04-1.html");
73         assertTrue("Should be an IPv4 address", uri.isIPv4address());
74
75         uri = new URI(base, "http://host.org/04-1.html");
76         assertFalse("Should NOT be an IPv4 address", uri.isIPv4address());
77
78         uri = new URI("http://host.org/04-1.html");
79         assertFalse("Should NOT be an IPv4 address", uri.isIPv4address());
80         
81     }
82     
83     public void testUrl() throws URIException {
84         URI url = new HttpURL("http://jakarta.apache.org");
85         assertEquals(80, url.getPort());
86         assertEquals("http", url.getScheme());
87         
88         url = new HttpsURL("https://jakarta.apache.org");
89         assertEquals(443, url.getPort());
90         assertEquals("https", url.getScheme());
91     }
92     
93     /**
94      * Tests the URI(URI, String) constructor. This tests URIs ability to
95      * resolve relative URIs.
96      *
97      * @see URI#URI(URI, String)
98      */

99     public void testRelativeURIConstructor() {
100         
101         URI baseURI = null;
102         
103         try {
104             baseURI = new URI( "http://a/b/c/d;p?q" );
105         } catch ( URIException e ) {
106             fail( "unable to create base URI: " + e );
107         }
108         
109         // the following is an array of arrays in the following order
110
// relative URI and resolved( scheme, host, path, query, fragment URI )
111
//
112
// these examples were taken from rfc 2396
113
String JavaDoc[][] testRelativeURIs = {
114             { "g:h", "g", null, "h", null, null, "g:h" },
115             { "g", "http", "a", "/b/c/g", null, null, "http://a/b/c/g" },
116             { "./g", "http", "a", "/b/c/g", null, null, "http://a/b/c/g" },
117             { "g/", "http", "a", "/b/c/g/", null, null, "http://a/b/c/g/" },
118             { "/g", "http", "a", "/g", null, null, "http://a/g" },
119             { "//g", "http", "g", null, null, null, "http://g" },
120             { "?y", "http", "a", "/b/c/", "y", null, "http://a/b/c/?y" },
121             { "g?y", "http", "a", "/b/c/g", "y", null, "http://a/b/c/g?y" },
122             { "#s", "http", "a", "/b/c/d;p", "q", "s", "http://a/b/c/d;p?q#s" },
123             { "#", "http", "a", "/b/c/d;p", "q", "", "http://a/b/c/d;p?q#" },
124             { "", "http", "a", "/b/c/d;p", "q", null, "http://a/b/c/d;p?q" },
125             { "g#s", "http", "a", "/b/c/g", null, "s", "http://a/b/c/g#s" },
126             { "g?y#s","http", "a", "/b/c/g", "y", "s", "http://a/b/c/g?y#s" },
127             { ";x", "http", "a", "/b/c/;x", null, null, "http://a/b/c/;x" },
128             { "g;x", "http", "a", "/b/c/g;x", null, null, "http://a/b/c/g;x" },
129             { "g;x?y#s", "http", "a", "/b/c/g;x", "y", "s", "http://a/b/c/g;x?y#s" },
130             { ".", "http", "a", "/b/c/", null, null, "http://a/b/c/" },
131             { "./", "http", "a", "/b/c/", null, null, "http://a/b/c/" },
132             { "..", "http", "a", "/b/", null, null, "http://a/b/" },
133             { "../", "http", "a", "/b/", null, null, "http://a/b/" },
134             { "../g", "http", "a", "/b/g", null, null, "http://a/b/g" },
135             { "../..", "http", "a", "/", null, null, "http://a/" },
136             { "../../", "http", "a", "/", null, null, "http://a/" },
137             { "../../g", "http", "a", "/g", null, null, "http://a/g" },
138             { "../../../g", "http", "a", "/g", null, null, "http://a/g" },
139             { "../../../../g", "http", "a", "/g", null, null, "http://a/g" },
140             { "/./g", "http", "a", "/g", null, null, "http://a/g" },
141             { "/../g", "http", "a", "/g", null, null, "http://a/g" },
142             { "g.", "http", "a", "/b/c/g.", null, null, "http://a/b/c/g." },
143             { ".g", "http", "a", "/b/c/.g", null, null, "http://a/b/c/.g" },
144             { "g..", "http", "a", "/b/c/g..", null, null, "http://a/b/c/g.." },
145             { "..g", "http", "a", "/b/c/..g", null, null, "http://a/b/c/..g" },
146             { "./../g", "http", "a", "/b/g", null, null, "http://a/b/g" },
147             { "./g/.", "http", "a", "/b/c/g/", null, null, "http://a/b/c/g/" },
148             { "g/./h", "http", "a", "/b/c/g/h", null, null, "http://a/b/c/g/h" },
149             { "g/../h", "http", "a", "/b/c/h", null, null, "http://a/b/c/h" },
150             { "g;x=1/./y", "http", "a", "/b/c/g;x=1/y", null, null, "http://a/b/c/g;x=1/y" },
151             { "g;x=1/../y", "http", "a", "/b/c/y", null, null, "http://a/b/c/y" },
152             { "g?y/./x", "http", "a", "/b/c/g", "y/./x", null, "http://a/b/c/g?y/./x" },
153             { "g?y/../x", "http", "a", "/b/c/g", "y/../x", null, "http://a/b/c/g?y/../x" },
154             { "g#s/./x", "http", "a", "/b/c/g", null, "s/./x", "http://a/b/c/g#s/./x" },
155             { "g#s/../x", "http", "a", "/b/c/g", null, "s/../x", "http://a/b/c/g#s/../x" }
156         };
157         for (int i = 0; i < testRelativeURIs.length; i++) {
158             URI testURI = null;
159             
160             try {
161                 testURI = new URI( baseURI, testRelativeURIs[i][0] );
162             } catch ( URIException e ) {
163                 e.printStackTrace();
164                 fail(
165                     "unable to create URI with relative value("
166                     + testRelativeURIs[i][0] + "): " + e
167                 );
168             }
169             
170             try {
171                 assertEquals( testURI.getScheme(), testRelativeURIs[i][1] );
172                 assertEquals( testURI.getAuthority(), testRelativeURIs[i][2] );
173                 assertEquals( testURI.getPath(), testRelativeURIs[i][3] );
174                 assertEquals( testURI.getQuery(), testRelativeURIs[i][4] );
175                 assertEquals( testURI.getFragment(), testRelativeURIs[i][5] );
176                 assertEquals( testURI.getURIReference(), testRelativeURIs[i][6] );
177             } catch ( URIException e ) {
178                 fail( "error getting URI property: " + e );
179             }
180         }
181         
182     }
183
184     public void testTestHttpUrlAuthorityString() throws Exception JavaDoc {
185         HttpURL url = new HttpURL("localhost", -1, "/");
186         assertEquals("http://localhost/", url.toString());
187         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
188         assertEquals("http://localhost/", url.toString());
189         assertEquals("user:password@localhost", url.getAuthority());
190
191         url = new HttpURL("user", "pass#", "localhost", 8080, "/");
192         assertEquals("http://localhost:8080/", url.toString());
193         assertEquals("user:pass#", url.getUserinfo());
194         assertEquals("user:pass%23", url.getEscapedUserinfo());
195
196         url = new HttpURL("localhost", 8080, "/");
197         assertEquals("http://localhost:8080/", url.toString());
198         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
199         assertEquals("http://localhost:8080/", url.toString());
200         assertEquals("user:password@localhost:8080", url.getAuthority());
201     }
202     
203     public void testTestHttpsUrlAuthorityString() throws Exception JavaDoc {
204         HttpsURL url = new HttpsURL("localhost", -1, "/");
205         assertEquals("https://localhost/", url.toString());
206         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
207         assertEquals("https://localhost/", url.toString());
208         assertEquals("user:password@localhost", url.getAuthority());
209
210         url = new HttpsURL("user", "pass#", "localhost", 8080, "/");
211         assertEquals("https://localhost:8080/", url.toString());
212         assertEquals("user:pass#", url.getUserinfo());
213         assertEquals("user:pass%23", url.getEscapedUserinfo());
214         
215         url = new HttpsURL("localhost", 8080, "/");
216         assertEquals("https://localhost:8080/", url.toString());
217         url.setRawUserinfo("user".toCharArray(), "password".toCharArray());
218         assertEquals("https://localhost:8080/", url.toString());
219         assertEquals("user:password@localhost:8080", url.getAuthority());
220         
221     }
222
223 }
224
Popular Tags