1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/TestCredentials.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  /***
39   * Unit tests for {@link Credentials}.
40   *
41   * @author Rodney Waldhoff
42   * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
43   * @version $Id: TestCredentials.java 608014 2008-01-02 05:48:53Z rolandw $
44   */
45  public class TestCredentials extends TestCase {
46  
47      // ------------------------------------------------------------ Constructor
48      public TestCredentials(String testName) {
49          super(testName);
50      }
51  
52      // ------------------------------------------------------------------- Main
53      public static void main(String args[]) {
54          String[] testCaseName = { TestCredentials.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      // ------------------------------------------------------- TestCase Methods
59  
60      public static Test suite() {
61          return new TestSuite(TestCredentials.class);
62      }
63  
64      public void testCredentialConstructors() {
65          try {
66              new UsernamePasswordCredentials(null, null);
67              fail("IllegalArgumentException should have been thrown");
68          } catch (IllegalArgumentException e) {
69              // expected
70          }
71          try {
72              new NTCredentials("user", "password", null, null);
73              fail("IllegalArgumentException should have been thrown");
74          } catch (IllegalArgumentException e) {
75              // expected
76          }
77          try {
78              new NTCredentials("user", "password", "host", null);
79              fail("IllegalArgumentException should have been thrown");
80          } catch (IllegalArgumentException e) {
81              // expected
82          }
83          NTCredentials creds = new NTCredentials("user", null, "host", "domain");
84          assertNotNull(creds.getUserName());
85          assertNull(creds.getPassword());
86          assertNotNull(creds.getDomain());
87          assertNotNull(creds.getHost());
88      }
89  
90      /***
91       * Verifies that credentials report equal when they should.
92       */
93      public void testCredentialEquals() {
94  
95          Credentials creds1 = new UsernamePasswordCredentials("user1", "password1");
96          Credentials creds1Again = new UsernamePasswordCredentials("user1", "password1");
97          Credentials creds2 = new UsernamePasswordCredentials("user2", "password2");
98          Credentials creds3 = new UsernamePasswordCredentials("user3", null);
99          Credentials creds3Again = new UsernamePasswordCredentials("user3", null);
100 
101         assertEquals(creds1, creds1Again);
102         assertNotSame(creds1, creds2);
103         assertEquals(creds3, creds3Again);
104 
105         Credentials ntCreds1 = new NTCredentials("user1", "password1", "host1", "domain1");
106         Credentials ntCreds1Again = new NTCredentials("user1", "password1", "host1", "domain1");
107         Credentials ntCreds2 = new NTCredentials("user1", "password2", "host1", "domain1");
108         Credentials ntCreds3 = new NTCredentials("user1", "password1", "host2", "domain1");
109         Credentials ntCreds4 = new NTCredentials("user1", "password1", "host1", "domain2");
110 
111         assertEquals(ntCreds1, ntCreds1Again);
112         assertNotSame(ntCreds1, creds1);
113         assertNotSame(creds1, ntCreds1);
114         assertNotSame(ntCreds1, ntCreds2);
115         assertNotSame(ntCreds1, ntCreds3);
116         assertNotSame(ntCreds1, ntCreds4);
117     }
118 }