KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > auth > AppCallbackHandlerUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.security.test.auth;
23
24 import java.util.HashMap JavaDoc;
25
26 import javax.security.auth.callback.Callback JavaDoc;
27 import javax.security.auth.callback.NameCallback JavaDoc;
28 import javax.security.auth.callback.PasswordCallback JavaDoc;
29
30 import org.jboss.security.auth.callback.AppCallbackHandler;
31 import org.jboss.security.auth.callback.ByteArrayCallback;
32 import org.jboss.security.auth.callback.MapCallback;
33 import org.jboss.test.JBossTestCase;
34
35 //$Id: AppCallbackHandlerUnitTestCase.java 43793 2006-04-14 15:37:16Z asaldhana $
36

37 /**
38  * Unit Tests the AppCallbackHandler
39  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
40  * @since Apr 13, 2006
41  * @version $Revision: 43793 $
42  */

43 public class AppCallbackHandlerUnitTestCase extends JBossTestCase
44 {
45    private NameCallback JavaDoc ncb = null;
46    private PasswordCallback JavaDoc pcb = null;
47    private MapCallback mcb = null;
48    private ByteArrayCallback bacb = null;
49    
50    public AppCallbackHandlerUnitTestCase(String JavaDoc name)
51    {
52       super(name);
53    }
54    
55    protected void setUp() throws Exception JavaDoc
56    {
57       ncb = new NameCallback JavaDoc("Enter Username:");
58       pcb = new PasswordCallback JavaDoc("Enter Password:", false);
59       mcb = new MapCallback();
60       bacb = new ByteArrayCallback("Enter data");
61    }
62    
63    protected void tearDown() throws Exception JavaDoc
64    {
65       ncb = null;
66       pcb = null;
67       mcb = null;
68       bacb = null;
69    }
70    
71    public void testUserNamePassword() throws Exception JavaDoc
72    {
73       AppCallbackHandler apc = new AppCallbackHandler("jduke","theduke".toCharArray());
74       //Create the Callbacks
75
Callback JavaDoc[] cb = new Callback JavaDoc[]{ncb,pcb};
76       apc.handle(cb);
77       assertTrue("jduke", "jduke".equals(ncb.getName()));
78       assertTrue("theduke","theduke".equals(new String JavaDoc(pcb.getPassword())) );
79    }
80    
81    public void testMapCallback() throws Exception JavaDoc
82    {
83       HashMap JavaDoc hm = new HashMap JavaDoc();
84       hm.put("jduke","theduke");
85       hm.put("scott","echoman");
86       AppCallbackHandler apc = new AppCallbackHandler(hm);
87       Callback JavaDoc[] cb = new Callback JavaDoc[]{mcb};
88       apc.handle(cb);
89       assertTrue("jduke=theduke", "theduke".equals(mcb.getInfo("jduke")));
90       assertTrue("scott=echoman", "echoman".equals(mcb.getInfo("scott")));
91    }
92    
93    public void testByteArrayCallback() throws Exception JavaDoc
94    {
95       AppCallbackHandler apc = new AppCallbackHandler("scott",
96                       "echoman".toCharArray(), "Loves Skiing!!!".getBytes());
97       //Create the Callbacks
98
Callback JavaDoc[] cb = new Callback JavaDoc[]{ncb,pcb,bacb};
99       apc.handle(cb);
100       assertTrue("scott", "scott".equals(ncb.getName()));
101       assertTrue("echoman","echoman".equals(new String JavaDoc(pcb.getPassword())) );
102       assertTrue("Loves Skiing!!!",
103             "Loves Skiing!!!".equals(new String JavaDoc(bacb.getByteArray())));
104    }
105 }
106
Popular Tags