1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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 480424 2006-11-29 05:56:49Z bayard $
44   */
45  public class TestCredentials extends TestCase {
46  
47      
48      public TestCredentials(String testName) {
49          super(testName);
50      }
51  
52      
53      public static void main(String args[]) {
54          String[] testCaseName = { TestCredentials.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      
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              
70          }
71          try {
72              new NTCredentials("user", "password", null, null);
73              fail("IllegalArgumentException should have been thrown");
74          } catch (IllegalArgumentException e) {
75              
76          }
77          try {
78              new NTCredentials("user", "password", "host", null);
79              fail("IllegalArgumentException should have been thrown");
80          } catch (IllegalArgumentException e) {
81              
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 }