KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > LoginContextTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test;
8
9 import java.util.Iterator JavaDoc;
10 import java.util.Set JavaDoc;
11 import javax.security.auth.login.Configuration JavaDoc;
12 import javax.security.auth.login.LoginContext JavaDoc;
13 import javax.security.auth.login.LoginException JavaDoc;
14 import javax.security.auth.Subject JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import org.jboss.security.auth.login.XMLLoginConfigImpl;
19 import org.jboss.security.SimplePrincipal;
20
21 public class LoginContextTestCase extends TestCase
22 {
23
24    public LoginContextTestCase(String JavaDoc name)
25    {
26       super(name);
27    }
28
29    protected void setUp() throws Exception JavaDoc
30    {
31       System.setOut(System.err);
32       XMLLoginConfigImpl config = new XMLLoginConfigImpl();
33       config.setConfigResource("login-config.xml");
34       config.loadConfig();
35       Configuration.setConfiguration(config);
36    }
37
38    private void validateSuccessfulLogin(LoginContext JavaDoc lc) throws LoginException JavaDoc
39    {
40       Subject JavaDoc subject = lc.getSubject();
41       assertTrue("case5 subject != null", subject != null);
42       boolean hasGuest = subject.getPrincipals().contains(new SimplePrincipal("guest"));
43       assertTrue("subject has guest principal", hasGuest);
44       lc.logout();
45       hasGuest = subject.getPrincipals().contains(new SimplePrincipal("guest"));
46       assertTrue("subject has guest principal", hasGuest == false);
47       Set JavaDoc publicCreds = subject.getPublicCredentials();
48       assertTrue("public creds has 'A public credential'",
49          publicCreds.contains("A public credential"));
50       Set JavaDoc privateCreds = subject.getPrivateCredentials();
51       assertTrue("private creds has 'A private credential'",
52          privateCreds.contains("A private credential"));
53       Iterator JavaDoc iter = privateCreds.iterator();
54       int count = 0;
55       while( iter.hasNext() )
56       {
57          iter.next();
58          count ++;
59       }
60       assertTrue("private creds has 1 entry", count == 1);
61    }
62
63    public void testCase1() throws Exception JavaDoc
64    {
65       LoginContext JavaDoc lc = new LoginContext JavaDoc("case1");
66       lc.login();
67       validateSuccessfulLogin(lc);
68    }
69
70    public void testCase2() throws Exception JavaDoc
71    {
72       LoginContext JavaDoc lc = new LoginContext JavaDoc("case2");
73       lc.login();
74       validateSuccessfulLogin(lc);
75    }
76
77    public void testCase3() throws Exception JavaDoc
78    {
79       LoginContext JavaDoc lc = new LoginContext JavaDoc("case3");
80       try
81       {
82          lc.login();
83          fail("LoginContext.login3 did not thrown an exception");
84       }
85       catch(LoginException JavaDoc e)
86       {
87          e.printStackTrace();
88       }
89    }
90
91    /** This should fail because no login module succeeds
92     *
93     * @throws Exception
94     */

95    public void testCase4() throws Exception JavaDoc
96    {
97       LoginContext JavaDoc lc = new LoginContext JavaDoc("case4");
98       try
99       {
100          lc.login();
101          fail("LoginContext.login4 did not thrown an exception");
102       }
103       catch(LoginException JavaDoc e)
104       {
105          e.printStackTrace();
106       }
107    }
108
109    public void testCase5() throws Exception JavaDoc
110    {
111       LoginContext JavaDoc lc = new LoginContext JavaDoc("case5");
112       lc.login();
113       validateSuccessfulLogin(lc);
114    }
115    public void testCase6() throws Exception JavaDoc
116    {
117       LoginContext JavaDoc lc = new LoginContext JavaDoc("case6");
118       lc.login();
119       validateSuccessfulLogin(lc);
120    }
121    public void testCase7() throws Exception JavaDoc
122    {
123       LoginContext JavaDoc lc = new LoginContext JavaDoc("case7");
124       lc.login();
125       validateSuccessfulLogin(lc);
126    }
127
128    public void testCase8() throws Exception JavaDoc
129    {
130       LoginContext JavaDoc lc = new LoginContext JavaDoc("case8");
131       try
132       {
133          lc.login();
134          fail("LoginContext.login8 did not thrown an exception");
135       }
136       catch(LoginException JavaDoc e)
137       {
138          e.printStackTrace();
139       }
140    }
141
142    public void testCase9() throws Exception JavaDoc
143    {
144       LoginContext JavaDoc lc = new LoginContext JavaDoc("case9");
145       lc.login();
146       validateSuccessfulLogin(lc);
147    }
148
149    public void testCase10() throws Exception JavaDoc
150    {
151       LoginContext JavaDoc lc = new LoginContext JavaDoc("case10");
152       try
153       {
154          lc.login();
155          fail("LoginContext.login10 did not thrown an exception");
156       }
157       catch(LoginException JavaDoc e)
158       {
159          e.printStackTrace();
160       }
161    }
162
163    public void testCase11() throws Exception JavaDoc
164    {
165       LoginContext JavaDoc lc = new LoginContext JavaDoc("case11");
166       lc.login();
167       validateSuccessfulLogin(lc);
168    }
169    public void testCase12() throws Exception JavaDoc
170    {
171       LoginContext JavaDoc lc = new LoginContext JavaDoc("case12");
172       lc.login();
173       validateSuccessfulLogin(lc);
174    }
175
176    public void testCase13() throws Exception JavaDoc
177    {
178       LoginContext JavaDoc lc = new LoginContext JavaDoc("case13");
179       try
180       {
181          lc.login();
182          fail("LoginContext.login13 did not thrown an exception");
183       }
184       catch(LoginException JavaDoc e)
185       {
186          e.printStackTrace();
187       }
188    }
189
190    public void testCase14() throws Exception JavaDoc
191    {
192       LoginContext JavaDoc lc = new LoginContext JavaDoc("case14");
193       try
194       {
195          lc.login();
196          fail("LoginContext.login14 did not thrown an exception");
197       }
198       catch(LoginException JavaDoc e)
199       {
200          e.printStackTrace();
201       }
202    }
203
204    public void testCase15() throws Exception JavaDoc
205    {
206       LoginContext JavaDoc lc = new LoginContext JavaDoc("case15");
207       try
208       {
209          lc.login();
210          fail("LoginContext.login15 did not thrown an exception");
211       }
212       catch(LoginException JavaDoc e)
213       {
214          e.printStackTrace();
215       }
216    }
217
218    public void testCase16() throws Exception JavaDoc
219    {
220       LoginContext JavaDoc lc = new LoginContext JavaDoc("case16");
221       lc.login();
222       validateSuccessfulLogin(lc);
223    }
224
225    public void testCase17() throws Exception JavaDoc
226    {
227       LoginContext JavaDoc lc = new LoginContext JavaDoc("case17");
228       lc.login();
229       validateSuccessfulLogin(lc);
230    }
231
232    public void testCase18() throws Exception JavaDoc
233    {
234       LoginContext JavaDoc lc = new LoginContext JavaDoc("case18");
235       try
236       {
237          lc.login();
238          fail("LoginContext.login18 did not thrown an exception");
239       }
240       catch(LoginException JavaDoc e)
241       {
242          e.printStackTrace();
243       }
244    }
245
246    public void testCase19() throws Exception JavaDoc
247    {
248       LoginContext JavaDoc lc = new LoginContext JavaDoc("case19");
249       try
250       {
251          lc.login();
252          fail("LoginContext.login19 did not thrown an exception");
253       }
254       catch(LoginException JavaDoc e)
255       {
256          e.printStackTrace();
257       }
258    }
259
260    public void testCase20() throws Exception JavaDoc
261    {
262       LoginContext JavaDoc lc = new LoginContext JavaDoc("case20");
263       try
264       {
265          lc.login();
266          fail("LoginContext.login20 did not thrown an exception");
267       }
268       catch(LoginException JavaDoc e)
269       {
270          e.printStackTrace();
271       }
272    }
273
274    public void testCase21() throws Exception JavaDoc
275    {
276       LoginContext JavaDoc lc = new LoginContext JavaDoc("case21");
277       try
278       {
279          lc.login();
280       }
281       catch(LoginException JavaDoc e)
282       {
283          e.printStackTrace();
284       }
285       Subject JavaDoc subject = lc.getSubject();
286       assertTrue("case21 subject == null", subject == null);
287    }
288
289    public void testCase22() throws Exception JavaDoc
290    {
291       LoginContext JavaDoc lc = new LoginContext JavaDoc("case22");
292       try
293       {
294          lc.login();
295          fail("LoginContext.login22 did not thrown an exception");
296       }
297       catch(LoginException JavaDoc e)
298       {
299          e.printStackTrace();
300       }
301    }
302
303 }
304
Popular Tags