KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > container > auth > AuthConfigFactoryTestCase


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.container.auth;
23
24 import java.util.HashMap JavaDoc;
25
26 import javax.security.auth.message.config.AuthConfigFactory;
27 import javax.security.auth.message.config.AuthConfigProvider;
28 import javax.security.auth.message.config.AuthConfigFactory.RegistrationListener;
29
30 import org.apache.commons.httpclient.HttpClient;
31 import org.apache.commons.httpclient.methods.GetMethod;
32 import org.jboss.security.SecurityConstants;
33 import org.jboss.security.auth.message.config.JBossAuthConfigFactory;
34 import org.jboss.test.JBossTestCase;
35
36 /**
37  * JSR-196 Tests
38  * Test the static factory class AuthConfigFactory
39  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
40  * @since Dec 6, 2005
41  */

42 public class AuthConfigFactoryTestCase extends JBossTestCase
43 {
44    private String JavaDoc baseURLNoAuth = "http://" + getServerHost() + ":" + Integer.getInteger("web.port", 8080) + "/";
45    private HttpClient httpConn = new HttpClient();
46    String JavaDoc allLayerProvider = "org.jboss.test.security.container.auth.AllLayerAuthConfigProvider";
47    String JavaDoc allACProvider = "org.jboss.test.security.container.auth.AllAppContextAuthConfigProvider";
48    
49    public AuthConfigFactoryTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    protected AuthConfigFactory factory = null;
55    
56    protected void setUp() throws Exception JavaDoc
57    {
58       factory = AuthConfigFactory.getFactory();
59       assertNotNull("AuthContextFactory is null?", factory);
60    }
61    
62    protected void tearDown() throws Exception JavaDoc
63    {
64       factory = null;
65    }
66    
67    public void testGetFactory()
68    {
69       assertTrue("AuthConfigFactory instance of JBossAuthConfigFactory",
70             factory instanceof JBossAuthConfigFactory);
71    }
72    
73    public void testSetFactory()
74    {
75       AuthConfigFactory.setFactory(new TestAuthConfigFactory());
76       factory = AuthConfigFactory.getFactory();
77       assertNotNull("AuthConfigFactory is null?", factory);
78       assertTrue("AuthConfigFactory instance of TestAuthConfigFactory",
79             factory instanceof TestAuthConfigFactory);
80       //Lets remove the test factory
81
AuthConfigFactory.setFactory(null);
82       assertTrue("AuthConfigFactory instance of JBossAuthConfigFactory",
83             AuthConfigFactory.getFactory() instanceof JBossAuthConfigFactory);
84    }
85     
86    
87    public void testServerAuthContext() throws Exception JavaDoc
88    {
89       //Key tester is the JASPISecurityFilter that does programmatic jaspi security
90
this.deploy("jbosssx-jaspi-web.war");
91       try
92       {
93          //Check successful validation
94
String JavaDoc str = "jbosssx-jaspi-web/DebugServlet?user=jduke&pass=theduke";
95          GetMethod indexGet = new GetMethod(baseURLNoAuth+str);
96          int responseCode = httpConn.executeMethod(indexGet);
97          assertTrue("Response code == 200?", responseCode == 200);
98          //Test unsuccessful validation
99
str = "jbosssx-jaspi-web/DebugServlet?user=jduke&pass=bad";
100          indexGet = new GetMethod(baseURLNoAuth+str);
101          responseCode = httpConn.executeMethod(indexGet);
102          assertTrue("Response code == 500?", responseCode == 500);
103       }
104       catch(Throwable JavaDoc t)
105       {
106          throw new Exception JavaDoc(t.getMessage());
107       }
108       finally
109       {
110          this.undeploy("jbosssx-jaspi-web.war");
111       }
112    }
113    
114    public void testConfigProviderRegistration() throws Exception JavaDoc
115    {
116       String JavaDoc registrationID = null;
117       String JavaDoc layer = null;
118       String JavaDoc appContext = "testAppContext";
119       factory = AuthConfigFactory.getFactory();
120       //Register an AuthConfigProvider for all layers
121
registrationID = factory.registerConfigProvider(allLayerProvider,
122                               new HashMap JavaDoc(),layer,appContext, "This is a test provider");
123       AuthConfigProvider acp = factory.getConfigProvider("TestLayer",appContext,
124                       null);
125       assertTrue("ACP instanceof AllLayerAuthConfigProvider",
126                        acp instanceof AllLayerAuthConfigProvider);
127       acp = factory.getConfigProvider(layer,appContext, null);
128       assertTrue("ACP instanceof AllLayerAuthConfigProvider",
129                        acp instanceof AllLayerAuthConfigProvider);
130       
131       assertTrue("Registration removed", factory.removeRegistration(registrationID));
132       layer = SecurityConstants.SERVLET_LAYER;
133       //Register an AuthConfigProvider for all appcontext in a layer
134
registrationID = factory.registerConfigProvider(allACProvider, new HashMap JavaDoc(),
135                                 layer, null, "This is a test provider");
136       acp = factory.getConfigProvider(layer,"testAppContext", null);
137       assertTrue("ACP instanceof AllAppContextAuthConfigProvider",
138                        acp instanceof AllAppContextAuthConfigProvider);
139       acp = factory.getConfigProvider(layer,"testAppContext", null);
140       assertTrue("ACP instanceof AllAppContextAuthConfigProvider",
141                        acp instanceof AllAppContextAuthConfigProvider);
142       acp = factory.getConfigProvider(layer,"testOtherAppContext", null);
143       assertTrue("ACP instanceof AllAppContextAuthConfigProvider",
144                        acp instanceof AllAppContextAuthConfigProvider);
145    }
146    
147    public void testRegistrationListener() throws Exception JavaDoc
148    {
149       RegistrationListener rl = new RegistrationListener()
150       {
151          public void notify(String JavaDoc layer, String JavaDoc appContext)
152          {
153          }
154       };
155       
156       String JavaDoc layer = SecurityConstants.SERVLET_LAYER;
157       //Register an AuthConfigProvider for all appcontext in a layer
158
factory.registerConfigProvider( allACProvider, new HashMap JavaDoc(),
159                                   layer, null, "This is a test provider");
160       AuthConfigProvider acp = factory.getConfigProvider(layer,"testAppContext", rl);
161       String JavaDoc[] ids = factory.getRegistrationIDs(acp);
162       String JavaDoc[] detachedIds = factory.detachListener(rl, layer, "testAppContext");
163       checkStringArrayEquals(ids, detachedIds);
164    }
165    
166    private void checkStringArrayEquals(String JavaDoc[] a, String JavaDoc[] b)
167    {
168      if(a == null && b == null)
169         return;
170      assertEquals("Length should be equal",a.length,b.length);
171      int len = a.length;
172      for(int i=0; i < len; i++)
173         assertEquals(a[i],b[i]);
174    }
175 }
176
Popular Tags