1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestURIUtil.java $
3    * $Revision: 1425331 $
4    * $Date: 2012-12-22 19:29:41 +0100 (Sat, 22 Dec 2012) $
5    * ====================================================================
6    *
7    *  Licensed to the Apache Software Foundation (ASF) under one or more
8    *  contributor license agreements.  See the NOTICE file distributed with
9    *  this work for additional information regarding copyright ownership.
10   *  The ASF licenses this file to You under the Apache License, Version 2.0
11   *  (the "License"); you may not use this file except in compliance with
12   *  the License.  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.TestCase;
36  import junit.framework.TestSuite;
37  
38  import org.apache.commons.httpclient.util.URIUtil;
39  
40  /***
41   *
42   * Unit tests for {@link URIUtil}.  These tests care currently quite limited
43   * and should be expanded to test more functionality.
44   *
45   * @author Marc A. Saegesser
46   * @version $Id: TestURIUtil.java 608014 2008-01-02 05:48:53Z rolandw $
47   */
48  public class TestURIUtil extends TestCase {
49      // ----------------------------------------------------- Instance Variables
50      URITestCase pathTests[] = {new URITestCase("http://www.server.com/path1/path2", "/path1/path2"),
51                                 new URITestCase("http://www.server.com/path1/path2/", "/path1/path2/"),
52                                 new URITestCase("http://www.server.com/path1/path2?query=string", "/path1/path2"),
53                                 new URITestCase("http://www.server.com/path1/path2/?query=string", "/path1/path2/"),
54                                 new URITestCase("www.noscheme.com/path1/path2", "/path1/path2"),
55                                 new URITestCase("www.noscheme.com/path1/path2#anchor?query=string", "/path1/path2"),
56                                 new URITestCase("/noscheme/nohost/path", "/noscheme/nohost/path"),
57                                 new URITestCase("http://www.server.com", "/"),
58                                 new URITestCase("https://www.server.com:443/ssl/path", "/ssl/path"),
59                                 new URITestCase("http://www.server.com:8080/path/with/port", "/path/with/port"),
60                                 new URITestCase("http://www.server.com/path1/path2?query1=string?1&query2=string2", "/path1/path2")};
61  
62      URITestCase queryTests[] = {new URITestCase("http://www.server.com/path1/path2", null),
63                                  new URITestCase("http://www.server.com/path1/path2?query=string", "query=string"),
64                                  new URITestCase("http://www.server.com/path1/path2/?query=string", "query=string"),
65                                  new URITestCase("www.noscheme.com/path1/path2#anchor?query=string", "query=string"),
66                                  new URITestCase("/noscheme/nohost/path?query1=string1&query2=string2", "query1=string1&query2=string2"),
67                                  new URITestCase("https://www.server.com:443/ssl/path?query1=string1&query2=string2", "query1=string1&query2=string2"),
68                                  new URITestCase("http://www.server.com:8080/path/with/port?query1=string1&query2=string2", "query1=string1&query2=string2"),
69                                  new URITestCase("http://www.server.com/path1/path2?query1=string?1&query2=string2", "query1=string?1&query2=string2")};
70  
71  
72  
73      // ------------------------------------------------------------ Constructor
74      public TestURIUtil(String testName) {
75          super(testName);
76      }
77  
78      // ------------------------------------------------------------------- Main
79      public static void main(String args[]) {
80          String[] testCaseName = { TestURIUtil.class.getName() };
81          junit.textui.TestRunner.main(testCaseName);
82      }
83  
84      // ------------------------------------------------------- TestCase Methods
85  
86      public static Test suite() {
87          return new TestSuite(TestURIUtil.class);
88      }
89  
90  
91      // ----------------------------------------------------------- Test Methods
92      public void testGetPath()
93      {
94          String testValue = "";
95          String expectedResult = "";
96  
97          for(int i=0;i<pathTests.length;i++){
98              testValue = pathTests[i].getTestValue();
99              expectedResult = pathTests[i].getExpectedResult();
100             assertEquals("Path test", expectedResult, URIUtil.getPath(testValue));
101         }
102     }
103 
104     public void testGetQueryString()
105     {
106         String testValue = "";
107         String expectedResult = "";
108 
109         for(int i=0;i<queryTests.length;i++){
110             testValue = queryTests[i].getTestValue();
111             expectedResult = queryTests[i].getExpectedResult();
112             assertEquals("Path test", expectedResult, URIUtil.getQuery(testValue));
113         }
114     }
115 
116     private class URITestCase{
117         private String testValue;
118         private String expectedResult;
119 
120         public URITestCase(String testValue, String expectedResult){
121             this.testValue = testValue;
122             this.expectedResult = expectedResult;
123         }
124 
125         public String getTestValue(){
126             return testValue;
127         }
128 
129         public String getExpectedResult(){
130             return expectedResult;
131         }
132     }
133 }