KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > authentication > SubjectTemplateTest


1 /*
2  jGuard is a security framework based on top of jaas (java authentication and authorization security).
3  it is written for web applications, to resolve simply, access control problems.
4  version $Name$
5  http://sourceforge.net/projects/jguard/
6
7  Copyright (C) 2004 Charles GAY
8
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26
27  */

28 package net.sf.jguard.ext.authentication;
29
30 import java.net.URL JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.security.auth.Subject JavaDoc;
37
38 import junit.framework.TestCase;
39 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
40 import net.sf.jguard.ext.SecurityConstants;
41 import net.sf.jguard.ext.authentication.AuthenticationException;
42 import net.sf.jguard.ext.authentication.manager.AuthenticationManagerFactory;
43 import net.sf.jguard.ext.authentication.manager.XmlAuthenticationManager;
44 import net.sf.jguard.ext.registration.SubjectTemplate;
45
46 public class SubjectTemplateTest extends TestCase {
47     private SubjectTemplate st = null;
48
49     /*
50      * Test method for 'net.sf.jguard.ext.authentication.SubjectTemplate.validateUser(SubjectTemplate)'
51      */

52     public void testValidateUser() {
53         buildReferenceSubjectTemplate();
54         SubjectTemplate st2 = new SubjectTemplate();
55         Set JavaDoc privateRequiredCredentails = new HashSet JavaDoc();
56         JGuardCredential j1 = new JGuardCredential();
57         j1.setId("login");
58         j1.setValue("azerty");
59         privateRequiredCredentails.add(j1);
60         JGuardCredential j2 = new JGuardCredential();
61         j2.setId("password");
62         j2.setValue("azerty");
63         privateRequiredCredentails.add(j2);
64         st2.setPrivateRequiredCredentials(privateRequiredCredentails);
65         Set JavaDoc publicRequiredCredentials = new HashSet JavaDoc();
66         JGuardCredential j3 = new JGuardCredential();
67         j3.setId("location");
68         j3.setValue("brest");
69         publicRequiredCredentials.add(j3);
70         st2.setPublicRequiredCredentials(publicRequiredCredentials);
71         initAuthenticationManager();
72         try {
73             st.validateUser(st2);
74         } catch (AuthenticationException e) {
75             TestCase.fail(" user candidate does not validate against the reference userTemplate ");
76         }
77
78         SubjectTemplate st3 = new SubjectTemplate();
79         try {
80             st.validateUser(st3);
81         } catch (AuthenticationException e) {
82             e.printStackTrace();
83             System.out.println(" test success => an exception is the normal result");
84         }
85
86     }
87
88     public void initAuthenticationManager() {
89         Map JavaDoc settings = new HashMap JavaDoc();
90         URL JavaDoc url = getClass().getResource("/jGuardUsersPrincipals.xml");
91         settings.put(SecurityConstants.AUTHENTICATION_XML_FILE_LOCATION, url.toString());
92         try {
93             AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class.getName(), settings);
94         } catch (AuthenticationException e1) {
95             TestCase.fail(" authenticationDAoFactory not properly initialized ");
96         }
97     }
98
99     /*
100      * Test method for 'net.sf.jguard.ext.authentication.SubjectTemplate.buildSubject(SubjectTemplate, Set)'
101      */

102     public void testBuildSubject() {
103         buildReferenceSubjectTemplate();
104         SubjectTemplate st2 = new SubjectTemplate();
105         Set JavaDoc principals = new HashSet JavaDoc();
106         Subject JavaDoc subj = st.buildSubject(st2);
107         assertEquals(principals, subj.getPrincipals());
108     }
109
110     public void buildReferenceSubjectTemplate() {
111         st = new SubjectTemplate();
112         Set JavaDoc privateRequiredCred = new HashSet JavaDoc();
113         JGuardCredential j1 = new JGuardCredential();
114         j1.setId("login");
115         j1.setValue("");
116         privateRequiredCred.add(j1);
117         JGuardCredential j2 = new JGuardCredential();
118         j2.setId("password");
119         j2.setValue("");
120         privateRequiredCred.add(j2);
121         st.setPrivateRequiredCredentials(privateRequiredCred);
122         Set JavaDoc publicRequiredCred = new HashSet JavaDoc();
123         JGuardCredential j3 = new JGuardCredential();
124         j3.setId("location");
125         j3.setValue("");
126         publicRequiredCred.add(j3);
127         st.setPublicRequiredCredentials(publicRequiredCred);
128         Set JavaDoc publicOptionalCredentials = new HashSet JavaDoc();
129         JGuardCredential j4 = new JGuardCredential();
130         j4.setId("hobbies");
131         j4.setValue("");
132         publicOptionalCredentials.add(j4);
133         st.setPublicOptionalCredentials(publicOptionalCredentials);
134         Set JavaDoc privateOptionalCredentials = new HashSet JavaDoc();
135         st.setPrivateOptionalCredentials(privateOptionalCredentials);
136     }
137
138     public void testClone() {
139         SubjectTemplate st = new SubjectTemplate();
140         try {
141             SubjectTemplate st2 = (SubjectTemplate) st.clone();
142             System.out.println(st2);
143         } catch (CloneNotSupportedException JavaDoc e) {
144             TestCase.fail(e.getMessage());
145         }
146     }
147 }
148
Popular Tags