KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > DynamicLoginConfigUnitTestCase


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.xml;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
31
32 import junit.framework.TestCase;
33 import org.jboss.security.auth.spi.Users;
34 import org.jboss.security.auth.container.config.AuthModuleEntry;
35 import org.jboss.security.auth.login.JASPIAuthenticationInfo;
36 import org.jboss.security.auth.login.LoginModuleStackHolder;
37 import org.jboss.security.config.PolicyConfig;
38 import org.jboss.security.auth.login.AuthenticationInfo;
39 import org.jboss.security.config.ApplicationPolicy;
40 import org.jboss.xb.binding.JBossXBException;
41 import org.jboss.xb.binding.Unmarshaller;
42 import org.jboss.xb.binding.UnmarshallerFactory;
43 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
44 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
45 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
46 import org.w3c.dom.ls.LSInput JavaDoc;
47 import org.xml.sax.EntityResolver JavaDoc;
48 import org.xml.sax.InputSource JavaDoc;
49
50 //$Id: DynamicLoginConfigUnitTestCase.java 45948 2006-06-28 02:30:30Z asaldhana $
51

52 /**
53  * Test unmarshalling xml documents conforming to mbean-service_1_0.xsd into
54  * the org.jboss.test.xml.mbeanserver.Services and related objects.
55  *
56  * @author Scott.Stark@jboss.org
57  * @author Anil.Saldhana@jboss.org
58  * @version $Revision: 45948 $
59  */

60 public class DynamicLoginConfigUnitTestCase
61    extends TestCase
62 {
63    /**
64     * A test of unmarshalling an element from a document without any knowledge
65     * of the associated schema.
66     *
67     * @throws Exception
68     */

69    public void testConfig() throws Exception JavaDoc
70    {
71       // Set the jboss url protocol handler path
72
System.setProperty("java.protocol.handler.pkgs", "org.jboss.net.protocol");
73       InputStream JavaDoc is = getResource("xml/loginconfig/config.xml");
74
75       Object JavaDoc root = getParsedRoot(is);
76
77       PolicyConfig config = (PolicyConfig) root;
78       is.close();
79
80       // Validate the bindings
81
ApplicationPolicy aPolicy = (ApplicationPolicy)config.get("conf1");
82       AuthenticationInfo info = (AuthenticationInfo)aPolicy.getAuthenticationInfo();
83       validateJaasBindings(info);
84    }
85    
86    /**
87     * A test of unmarshalling an element from a document without any knowledge
88     * of the associated schema. (JASPI Version based on the security-config_5_0.xsd
89     *
90     * @throws Exception
91     */

92    public void testJASPIConfig() throws Exception JavaDoc
93    {
94       // Set the jboss url protocol handler path
95
System.setProperty("java.protocol.handler.pkgs", "org.jboss.net.protocol");
96       InputStream JavaDoc is = getResource("xml/loginconfig/jaspi-config.xml");
97
98       Object JavaDoc root = getParsedRoot(is);
99
100       PolicyConfig config = (PolicyConfig) root;
101       is.close();
102
103       // Validate the bindings
104
ApplicationPolicy aPolicy = (ApplicationPolicy)config.get("conf1");
105       AuthenticationInfo info = (AuthenticationInfo)aPolicy.getAuthenticationInfo();
106       validateJaasBindings(info);
107       
108       //Validate the JASPI bindings
109
aPolicy = (ApplicationPolicy)config.get("conf-jaspi");
110       JASPIAuthenticationInfo jaspiInfo = (JASPIAuthenticationInfo)aPolicy.getAuthenticationInfo();
111       validateJASPIBindings( jaspiInfo );
112    }
113
114    // Private
115
private Object JavaDoc getParsedRoot(InputStream JavaDoc is) throws JBossXBException
116    {
117       /* Parse the element content using the Unmarshaller starting with an
118       empty schema since we don't know anything about it. This is not quite
119       true as we set the schema baseURI to the resources/xml/loginconfig/ directory
120       so that the xsds can be found, but this baseURI
121       can be easily specified to the SARDeployer, or the schema can be made
122       available to the entity resolver via some other configuration.
123       */

124       final URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource("xml/loginconfig/");
125       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
126       unmarshaller.setEntityResolver(new EntityResolver JavaDoc(){
127          public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
128          {
129             return null;
130          }
131       });
132       Object JavaDoc root = unmarshaller.unmarshal(is, new SchemaBindingResolver(){
133          public String JavaDoc getBaseURI()
134          {
135             throw new UnsupportedOperationException JavaDoc("getBaseURI is not implemented.");
136          }
137
138          public void setBaseURI(String JavaDoc baseURI)
139          {
140             throw new UnsupportedOperationException JavaDoc("setBaseURI is not implemented.");
141          }
142
143          public SchemaBinding resolve(String JavaDoc nsUri, String JavaDoc baseURI, String JavaDoc schemaLocation)
144          {
145             return XsdBinder.bind(url.toExternalForm() + schemaLocation, this);
146          }
147
148          public LSInput JavaDoc resolveAsLSInput(String JavaDoc nsUri, String JavaDoc baseUri, String JavaDoc schemaLocation)
149          {
150             throw new UnsupportedOperationException JavaDoc("resolveAsLSInput is not implemented.");
151          }
152       });
153       return root;
154    }
155    
156    
157    private void validateJaasBindings( AuthenticationInfo info )
158    {
159       assertNotNull("conf1", info);
160       AppConfigurationEntry JavaDoc[] entry = info.getAppConfigurationEntry();
161       assertTrue("entry.length == 1", entry.length == 1);
162       assertTrue("entry[0].getLoginModuleName() == XMLLoginModule",
163          entry[0].getLoginModuleName().equals("org.jboss.security.auth.spi.XMLLoginModule"));
164       Map JavaDoc options = entry[0].getOptions();
165       assertTrue("There are two options", options.size() == 2);
166       String JavaDoc unauthenticatedIdentity = (String JavaDoc) options.get("unauthenticatedIdentity");
167       assertNotNull("options.unauthenticatedIdentity exists", unauthenticatedIdentity);
168       assertTrue("options.unauthenticatedIdentity == guest",
169          unauthenticatedIdentity.equals("guest"));
170
171       Users users = (Users) options.get("userInfo");
172       assertNotNull("options.userInfo is a Users", users);
173       assertTrue("Users.size("+users.size()+") is 6", users.size() == 6);
174       Users.User jduke = users.getUser("jduke");
175       assertNotNull("jduke is a user", jduke);
176       assertTrue("jduke.password == theduke", jduke.getPassword().equals("theduke"));
177       String JavaDoc[] roleNames = jduke.getRoleNames("Roles");
178       HashSet JavaDoc roles = new HashSet JavaDoc(Arrays.asList(roleNames));
179       assertTrue("jduke has roles", roles.size() == 3);
180       assertTrue("Role1 is a role", roles.contains("Role1"));
181       assertTrue("Role2 is a role", roles.contains("Role2"));
182       assertTrue("Echo is a role", roles.contains("Echo"));
183    }
184    
185    private void validateJASPIBindings( JASPIAuthenticationInfo jaspiInfo )
186    {
187       assertNotNull("conf-jaspi", jaspiInfo);
188       AuthModuleEntry[] authEntry = jaspiInfo.getAuthModuleEntry();
189       assertTrue("entry.length == 2", authEntry.length == 2);
190       //Get the first AuthModule
191
AuthModuleEntry aEntry1 = authEntry[0];
192       validateAuthModule1(aEntry1);
193       //Get the second AuthModule
194
AuthModuleEntry aEntry2 = authEntry[1];
195       validateAuthModule2(aEntry2);
196    }
197    
198    private void validateAuthModule1(AuthModuleEntry aEntry1)
199    {
200       assertEquals("auth.module1.class.name", aEntry1.getAuthModuleName());
201       Map JavaDoc aEntry1Options = aEntry1.getOptions();
202       assertNotNull("Options in the first AuthModule != null", aEntry1Options);
203       assertTrue( "Length of options == 3", aEntry1Options.size() == 3);
204       String JavaDoc usersProperties = (String JavaDoc) aEntry1Options.get("usersProperties");
205       assertNotNull("options.usersProperties exists", usersProperties);
206       assertTrue("options.usersProperties == props/jbossws-users.properties",
207             usersProperties.equals("props/jbossws-users.properties"));
208       String JavaDoc rolesProperties = (String JavaDoc) aEntry1Options.get("rolesProperties");
209       assertNotNull("options.rolesProperties exists", rolesProperties);
210       assertTrue("options.rolesProperties == props/jbossws-roles.properties",
211             rolesProperties.equals("props/jbossws-roles.properties"));
212    }
213    
214    private void validateAuthModule2(AuthModuleEntry aEntry2)
215    {
216       assertEquals("auth.module2.class.name", aEntry2.getAuthModuleName());
217       LoginModuleStackHolder lmsh = aEntry2.getLoginModuleStackHolder();
218       assertNotNull("LoginModuleStackHolder != null", lmsh);
219       assertEquals("lm-stack", lmsh.getName());
220    }
221
222    private InputStream JavaDoc getResource(String JavaDoc path)
223       throws IOException JavaDoc
224    {
225       URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(path);
226       if(url == null)
227       {
228          fail("URL not found: " + path);
229       }
230       return url.openStream();
231    }
232 }
233
Popular Tags