1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestURIUtil.java,v 1.6 2004/02/22 18:08:50 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import junit.framework.Test;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  
37  import org.apache.commons.httpclient.util.URIUtil;
38  
39  /***
40   *
41   * Unit tests for {@link URIUtil}.  These tests care currently quite limited
42   * and should be expanded to test more functionality.
43   *
44   * @author Marc A. Saegesser
45   * @version $Id: TestURIUtil.java 155418 2005-02-26 13:01:52Z dirkv $
46   */
47  public class TestURIUtil extends TestCase {
48      // ----------------------------------------------------- Instance Variables
49      URITestCase pathTests[] = {new URITestCase("http://www.server.com/path1/path2", "/path1/path2"),
50                                 new URITestCase("http://www.server.com/path1/path2/", "/path1/path2/"),
51                                 new URITestCase("http://www.server.com/path1/path2?query=string", "/path1/path2"),
52                                 new URITestCase("http://www.server.com/path1/path2/?query=string", "/path1/path2/"),
53                                 new URITestCase("www.noscheme.com/path1/path2", "/path1/path2"),
54                                 new URITestCase("www.noscheme.com/path1/path2#anchor?query=string", "/path1/path2"),
55                                 new URITestCase("/noscheme/nohost/path", "/noscheme/nohost/path"),
56                                 new URITestCase("http://www.server.com", "/"),
57                                 new URITestCase("https://www.server.com:443/ssl/path", "/ssl/path"),
58                                 new URITestCase("http://www.server.com:8080/path/with/port", "/path/with/port"),
59                                 new URITestCase("http://www.server.com/path1/path2?query1=string?1&query2=string2", "/path1/path2")};
60  
61      URITestCase queryTests[] = {new URITestCase("http://www.server.com/path1/path2", null),
62                                  new URITestCase("http://www.server.com/path1/path2?query=string", "query=string"),
63                                  new URITestCase("http://www.server.com/path1/path2/?query=string", "query=string"),
64                                  new URITestCase("www.noscheme.com/path1/path2#anchor?query=string", "query=string"),
65                                  new URITestCase("/noscheme/nohost/path?query1=string1&query2=string2", "query1=string1&query2=string2"),
66                                  new URITestCase("https://www.server.com:443/ssl/path?query1=string1&query2=string2", "query1=string1&query2=string2"),
67                                  new URITestCase("http://www.server.com:8080/path/with/port?query1=string1&query2=string2", "query1=string1&query2=string2"),
68                                  new URITestCase("http://www.server.com/path1/path2?query1=string?1&query2=string2", "query1=string?1&query2=string2")};
69  
70  
71  
72      // ------------------------------------------------------------ Constructor
73      public TestURIUtil(String testName) {
74          super(testName);
75      }
76  
77      // ------------------------------------------------------------------- Main
78      public static void main(String args[]) {
79          String[] testCaseName = { TestURIUtil.class.getName() };
80          junit.textui.TestRunner.main(testCaseName);
81      }
82  
83      // ------------------------------------------------------- TestCase Methods
84  
85      public static Test suite() {
86          return new TestSuite(TestURIUtil.class);
87      }
88  
89  
90      // ----------------------------------------------------------- Test Methods
91      public void testGetPath()
92      {
93          String testValue = "";
94          String expectedResult = "";
95  
96          for(int i=0;i<pathTests.length;i++){
97              testValue = pathTests[i].getTestValue();
98              expectedResult = pathTests[i].getExpectedResult();
99              assertEquals("Path test", expectedResult, URIUtil.getPath(testValue));
100         }
101     }
102 
103     public void testGetQueryString()
104     {
105         String testValue = "";
106         String expectedResult = "";
107 
108         for(int i=0;i<queryTests.length;i++){
109             testValue = queryTests[i].getTestValue();
110             expectedResult = queryTests[i].getExpectedResult();
111             assertEquals("Path test", expectedResult, URIUtil.getQuery(testValue));
112         }
113     }
114 
115     private class URITestCase{
116         private String testValue;
117         private String expectedResult;
118 
119         public URITestCase(String testValue, String expectedResult){
120             this.testValue = testValue;
121             this.expectedResult = expectedResult;
122         }
123 
124         public String getTestValue(){
125             return testValue;
126         }
127 
128         public String getExpectedResult(){
129             return expectedResult;
130         }
131     }
132 }