1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: HTTPRequestInfoTest.java,v 1.1 2005/05/03 13:46:00 tanderson Exp $
44   */
45  package org.exolab.jms.net.http.connector;
46  
47  import junit.framework.TestCase;
48  
49  import org.exolab.jms.net.http.HTTPRequestInfo;
50  import org.exolab.jms.net.uri.URI;
51  import org.exolab.jms.net.util.Properties;
52  import org.exolab.jms.net.util.SSLProperties;
53  
54  
55  /***
56   * Tests the {@link HTTPRequestInfo} class.
57   *
58   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
59   * @version $Revision: 1.1 $ $Date: 2005/05/03 13:46:00 $
60   */
61  public class HTTPRequestInfoTest extends TestCase {
62  
63      /***
64       * Construct a new <code>HTTPRequestInfoTest</code>.
65       *
66       * @param name the name of the test to run
67       */
68      public HTTPRequestInfoTest(String name) {
69          super(name);
70      }
71  
72      /***
73       * Tests accessors.
74       *
75       * @throws Exception for any error
76       */
77      public void testAccessors() throws Exception {
78          final String uri = "http://localhost:80";
79          final String proxyHost = "foo";
80          final int proxyPort = 1030;
81          final String proxyUser = "bar";
82          final String proxyPassword = "fly";
83          final SSLProperties ssl = new SSLProperties();
84  
85          HTTPRequestInfo info = populate(uri, proxyHost, proxyPort, proxyUser,
86                                          proxyPassword, ssl);
87  
88          assertEquals(uri, info.getURI().toString());
89          assertEquals(proxyHost, info.getProxyHost());
90          assertEquals(proxyPort, info.getProxyPort());
91          assertEquals(proxyUser, info.getProxyUser());
92          assertEquals(proxyPassword, info.getProxyPassword());
93          assertEquals(ssl, info.getSSLProperties());
94      }
95  
96      /***
97       * Tests {@link HTTPRequestInfo#equals}.
98       *
99       * @throws Exception for any error
100      */
101     public void testEquals() throws Exception {
102         final String uri = "http://exolab.org:80";
103         final String proxyHost = "boo";
104         final int proxyPort = 9090;
105         final String proxyUser = "hoo";
106         final String proxyPassword = "shoo";
107         final SSLProperties ssl = new SSLProperties();
108 
109         HTTPRequestInfo info1 = populate(uri, proxyHost, proxyPort, proxyUser,
110                                          proxyPassword, null);
111         HTTPRequestInfo info2 = populate(uri, proxyHost, proxyPort, proxyUser,
112                                          proxyPassword, null);
113 
114         assertEquals(info1, info2);
115 
116         info2.setSSLProperties(ssl);
117         assertFalse(info1.equals(info2));
118     }
119 
120     /***
121      * Tests properties.
122      *
123      * @throws Exception for any error
124      */
125     public void testProperties() throws Exception {
126         final String prefix = "org.exolab.jms.net.https.";
127         final String uri = "https://exolab.org";
128         final String proxyHost = "binky";
129         final int proxyPort = 1032;
130         final String proxyUser = "gum";
131         final String proxyPassword = "ball";
132         final SSLProperties ssl = new SSLProperties();
133         ssl.setKeyStore("keyStore");
134         ssl.setKeyStorePassword("keyStorePassword");
135         ssl.setKeyStoreType("JKS");
136         ssl.setTrustStore("trustStore");
137         ssl.setTrustStorePassword("trustStorePassword");
138         ssl.setTrustStoreType("PCKS12");
139 
140         Properties properties = new Properties(prefix);
141         HTTPRequestInfo info1 = populate(uri, proxyHost, proxyPort, proxyUser,
142                                          proxyPassword, ssl);
143         info1.export(properties);
144         HTTPRequestInfo info2 = new HTTPRequestInfo(new URI(uri), properties);
145         assertEquals(info1, info2);
146 
147         assertEquals(uri, info2.getURI().toString());
148         assertEquals(proxyHost, info2.getProxyHost());
149         assertEquals(proxyPort, info2.getProxyPort());
150         assertEquals(proxyUser, info2.getProxyUser());
151         assertEquals(proxyPassword, info2.getProxyPassword());
152         assertEquals(ssl, info2.getSSLProperties());
153     }
154 
155     /***
156      * Helper to populate an {@link HTTPRequestInfo}.
157      *
158      * @param uri      the URI
159      * @param host     the proxy host
160      * @param port     the proxy port
161      * @param user     the proxy user
162      * @param password the proxy password
163      * @param ssl      SSL properties
164      * @return a new <code>HTTPRequestInfo</code>
165      * @throws Exception for any error
166      */
167     private HTTPRequestInfo populate(String uri, String host, int port,
168                                      String user, String password,
169                                      SSLProperties ssl)
170             throws Exception {
171         HTTPRequestInfo info = new HTTPRequestInfo(new URI(uri));
172         info.setProxyHost(host);
173         info.setProxyPort(port);
174         info.setProxyUser(user);
175         info.setProxyPassword(password);
176         info.setSSLProperties(ssl);
177         return info;
178     }
179 
180 }