KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty > AbstractWebModuleTest


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty;
18
19 import java.io.File JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.security.PermissionCollection JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import junit.framework.TestCase;
31 import org.apache.geronimo.connector.outbound.connectiontracking.ConnectionTrackingCoordinator;
32 import org.apache.geronimo.gbean.GBeanData;
33 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContext;
34 import org.apache.geronimo.j2ee.j2eeobjectnames.J2eeContextImpl;
35 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
36 import org.apache.geronimo.jetty.connector.HTTPConnector;
37 import org.apache.geronimo.kernel.KernelFactory;
38 import org.apache.geronimo.kernel.Kernel;
39 import org.apache.geronimo.kernel.management.State;
40 import org.apache.geronimo.security.SecurityServiceImpl;
41 import org.apache.geronimo.security.jacc.ComponentPermissions;
42 import org.apache.geronimo.security.jacc.ApplicationPolicyConfigurationManager;
43 import org.apache.geronimo.security.deploy.Principal;
44 import org.apache.geronimo.security.deploy.DefaultPrincipal;
45 import org.apache.geronimo.security.jaas.GeronimoLoginConfiguration;
46 import org.apache.geronimo.security.jaas.JaasLoginService;
47 import org.apache.geronimo.security.jaas.LoginModuleGBean;
48 import org.apache.geronimo.security.jaas.JaasLoginModuleUse;
49 import org.apache.geronimo.security.realm.GenericSecurityRealm;
50 import org.apache.geronimo.system.serverinfo.ServerInfo;
51 import org.apache.geronimo.transaction.context.OnlineUserTransaction;
52 import org.apache.geronimo.transaction.context.TransactionContextManager;
53 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
54 import org.mortbay.jetty.servlet.FormAuthenticator;
55
56
57 /**
58  * @version $Rev: $ $Date: $
59  */

60 public class AbstractWebModuleTest extends TestCase {
61     protected Kernel kernel;
62     private GBeanData container;
63     private ObjectName JavaDoc containerName;
64     private ObjectName JavaDoc connectorName;
65     private GBeanData connector;
66     private ObjectName JavaDoc webModuleName;
67     private ObjectName JavaDoc tmName;
68     private ObjectName JavaDoc ctcName;
69     private GBeanData tm;
70     private GBeanData ctc;
71     private ObjectName JavaDoc tcmName;
72     private GBeanData tcm;
73     private ClassLoader JavaDoc cl;
74     private J2eeContext moduleContext = new J2eeContextImpl("jetty.test", "test", "null", NameFactory.WEB_MODULE, "jettyTest", null, null);
75     private GBeanData loginConfigurationGBean;
76     protected ObjectName JavaDoc loginConfigurationName;
77     private GBeanData securityServiceGBean;
78     protected ObjectName JavaDoc securityServiceName;
79     private ObjectName JavaDoc loginServiceName;
80     private GBeanData loginServiceGBean;
81     protected GBeanData propertiesLMGBean;
82     protected ObjectName JavaDoc propertiesLMName;
83     protected ObjectName JavaDoc propertiesRealmName;
84     private GBeanData propertiesRealmGBean;
85     private ObjectName JavaDoc serverInfoName;
86     private GBeanData serverInfoGBean;
87     protected final static String JavaDoc securityRealmName = "demo-properties-realm";
88
89     public void testDummy() throws Exception JavaDoc {
90     }
91
92     protected void setUpStaticContentServlet() throws Exception JavaDoc {
93         GBeanData staticContentServletGBeanData = new GBeanData(JettyServletHolder.GBEAN_INFO);
94         staticContentServletGBeanData.setAttribute("servletName", "default");
95         staticContentServletGBeanData.setAttribute("servletClass", "org.mortbay.jetty.servlet.Default");
96         Map JavaDoc staticContentServletInitParams = new HashMap JavaDoc();
97         staticContentServletInitParams.put("acceptRanges", "true");
98         staticContentServletInitParams.put("dirAllowed", "true");
99         staticContentServletInitParams.put("putAllowed", "false");
100         staticContentServletInitParams.put("delAllowed", "false");
101         staticContentServletInitParams.put("redirectWelcome", "false");
102         staticContentServletInitParams.put("minGzipLength", "8192");
103         staticContentServletGBeanData.setAttribute("initParams", staticContentServletInitParams);
104         staticContentServletGBeanData.setAttribute("loadOnStartup", new Integer JavaDoc(0));
105         staticContentServletGBeanData.setAttribute("servletMappings", Collections.singleton(new String JavaDoc("/")));
106         ObjectName JavaDoc staticContentServletObjectName = NameFactory.getComponentName(null, null, null, NameFactory.WEB_MODULE, null, (String JavaDoc) staticContentServletGBeanData.getAttribute("servletName"), NameFactory.SERVLET, moduleContext);
107         staticContentServletGBeanData.setName(staticContentServletObjectName);
108         staticContentServletGBeanData.setReferencePattern("JettyServletRegistration", webModuleName);
109
110         start(staticContentServletGBeanData);
111     }
112
113     protected void setUpInsecureAppContext() throws Exception JavaDoc {
114         GBeanData app = new GBeanData(webModuleName, JettyWebAppContext.GBEAN_INFO);
115         app.setAttribute("uri", URI.create("war1/"));
116         app.setAttribute("componentContext", Collections.EMPTY_MAP);
117         OnlineUserTransaction userTransaction = new OnlineUserTransaction();
118         app.setAttribute("userTransaction", userTransaction);
119         //we have no classes or libs.
120
app.setAttribute("webClassPath", new URI JavaDoc[]{});
121         app.setAttribute("contextPriorityClassLoader", Boolean.FALSE);
122         app.setAttribute("configurationBaseUrl", new File JavaDoc("src/test-resources/deployables/").toURL());
123         app.setReferencePattern("TransactionContextManager", tcmName);
124         app.setReferencePattern("TrackedConnectionAssociator", ctcName);
125         app.setReferencePattern("JettyContainer", containerName);
126
127         app.setAttribute("contextPath", "/test");
128
129         start(app);
130     }
131
132     protected void setUpSecureAppContext(Map JavaDoc roleDesignates, Map JavaDoc principalRoleMap, ComponentPermissions componentPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection JavaDoc checked, Set JavaDoc securityRoles) throws Exception JavaDoc {
133         ObjectName JavaDoc jaccBeanName = NameFactory.getComponentName(null, null, null, null, "foo", NameFactory.JACC_MANAGER, moduleContext);
134         GBeanData jaccBeanData = new GBeanData(jaccBeanName, ApplicationPolicyConfigurationManager.GBEAN_INFO);
135         Map JavaDoc contextIDToPermissionsMap = new HashMap JavaDoc();
136         contextIDToPermissionsMap.put("TEST", componentPermissions);
137         jaccBeanData.setAttribute("contextIdToPermissionsMap", contextIDToPermissionsMap);
138         jaccBeanData.setAttribute("principalRoleMap", principalRoleMap);
139         jaccBeanData.setAttribute("roleDesignates", roleDesignates);
140         start(jaccBeanData);
141
142         GBeanData app = new GBeanData(webModuleName, JettyWebAppContext.GBEAN_INFO);
143         app.setAttribute("securityRealmName", "demo-properties-realm");
144         app.setAttribute("defaultPrincipal", defaultPrincipal);
145         app.setAttribute("checkedPermissions", checked);
146         app.setAttribute("excludedPermissions", componentPermissions.getExcludedPermissions());
147         app.setReferencePattern("RoleDesignateSource", jaccBeanName);
148
149         FormAuthenticator formAuthenticator = new FormAuthenticator();
150         formAuthenticator.setLoginPage("/auth/logon.html?param=test");
151         formAuthenticator.setErrorPage("/auth/logonError.html?param=test");
152         app.setAttribute("realmName", "Test JAAS Realm");
153         app.setAttribute("authenticator", formAuthenticator);
154         app.setAttribute("policyContextID", "TEST");
155         app.setAttribute("uri", URI.create("war3/"));
156         app.setAttribute("componentContext", Collections.EMPTY_MAP);
157
158         OnlineUserTransaction userTransaction = new OnlineUserTransaction();
159         app.setAttribute("userTransaction", userTransaction);
160         //we have no classes or libs.
161
app.setAttribute("webClassPath", new URI JavaDoc[]{});
162         app.setAttribute("contextPriorityClassLoader", Boolean.FALSE);
163         app.setAttribute("configurationBaseUrl", new File JavaDoc("src/test-resources/deployables/").toURL());
164         app.setReferencePattern("TransactionContextManager", tcmName);
165         app.setReferencePattern("TrackedConnectionAssociator", ctcName);
166         app.setReferencePattern("JettyContainer", containerName);
167
168         app.setAttribute("contextPath", "/test");
169
170         start(app);
171     }
172
173     protected void setUpSecurity() throws Exception JavaDoc {
174
175         loginConfigurationName = new ObjectName JavaDoc("geronimo.security:type=LoginConfiguration");
176         loginConfigurationGBean = new GBeanData(loginConfigurationName, GeronimoLoginConfiguration.getGBeanInfo());
177         Set JavaDoc configurations = new HashSet JavaDoc();
178         configurations.add(new ObjectName JavaDoc("geronimo.server:j2eeType=SecurityRealm,*"));
179         configurations.add(new ObjectName JavaDoc("geronimo.server:j2eeType=ConfigurationEntry,*"));
180         loginConfigurationGBean.setReferencePatterns("Configurations", configurations);
181
182         serverInfoName = new ObjectName JavaDoc("geronimo.system:name=ServerInfo");
183         serverInfoGBean = new GBeanData(serverInfoName, ServerInfo.GBEAN_INFO);
184         serverInfoGBean.setAttribute("baseDirectory", ".");
185
186         securityServiceName = new ObjectName JavaDoc("geronimo.server:j2eeType=SecurityService");
187         securityServiceGBean = new GBeanData(securityServiceName, SecurityServiceImpl.GBEAN_INFO);
188         securityServiceGBean.setReferencePattern("ServerInfo", serverInfoName);
189         securityServiceGBean.setAttribute("policyConfigurationFactory", "org.apache.geronimo.security.jacc.GeronimoPolicyConfigurationFactory");
190         securityServiceGBean.setAttribute("policyProvider", "org.apache.geronimo.security.jacc.GeronimoPolicy");
191
192         loginServiceName = JaasLoginService.OBJECT_NAME;
193         loginServiceGBean = new GBeanData(loginServiceName, JaasLoginService.GBEAN_INFO);
194         loginServiceGBean.setReferencePattern("Realms", new ObjectName JavaDoc("geronimo.server:j2eeType=SecurityRealm,*"));
195 // loginServiceGBean.setAttribute("reclaimPeriod", new Long(1000 * 1000));
196
loginServiceGBean.setAttribute("algorithm", "HmacSHA1");
197         loginServiceGBean.setAttribute("password", "secret");
198
199         propertiesLMName = new ObjectName JavaDoc("geronimo.security:type=LoginModule,name=demo-properties-login");
200         propertiesLMGBean = new GBeanData(propertiesLMName, LoginModuleGBean.GBEAN_INFO);
201         propertiesLMGBean.setAttribute("loginModuleClass", "org.apache.geronimo.security.realm.providers.PropertiesFileLoginModule");
202         propertiesLMGBean.setAttribute("serverSide", Boolean.TRUE);
203         Properties JavaDoc options = new Properties JavaDoc();
204         options.setProperty("usersURI", "src/test-resources/data/users.properties");
205         options.setProperty("groupsURI", "src/test-resources/data/groups.properties");
206         propertiesLMGBean.setAttribute("options", options);
207         //TODO should this be called securityRealmName?
208
propertiesLMGBean.setAttribute("loginDomainName", "demo-properties-realm");
209
210         ObjectName JavaDoc testUseName = new ObjectName JavaDoc("geronimo.security:type=LoginModuleUse,name=properties");
211         GBeanData lmUseGBean = new GBeanData(testUseName, JaasLoginModuleUse.getGBeanInfo());
212         lmUseGBean.setAttribute("controlFlag", "REQUIRED");
213         lmUseGBean.setReferencePattern("LoginModule", propertiesLMName);
214
215         propertiesRealmName = new ObjectName JavaDoc("geronimo.server:j2eeType=SecurityRealm,name=demo-properties-realm");
216         propertiesRealmGBean = new GBeanData(propertiesRealmName, GenericSecurityRealm.GBEAN_INFO);
217         propertiesRealmGBean.setReferencePattern("ServerInfo", serverInfoName);
218         propertiesRealmGBean.setAttribute("realmName", "demo-properties-realm");
219 // Properties config = new Properties();
220
// config.setProperty("LoginModule.1.REQUIRED", propertiesLMName.getCanonicalName());
221
// propertiesRealmGBean.setAttribute("loginModuleConfiguration", config);
222
propertiesRealmGBean.setReferencePattern("LoginModuleConfiguration", testUseName);
223         Principal.PrincipalEditor principalEditor = new Principal.PrincipalEditor();
224         principalEditor.setAsText("metro=org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal");
225         propertiesRealmGBean.setAttribute("defaultPrincipal", principalEditor.getValue());
226
227         start(loginConfigurationGBean);
228         start(serverInfoGBean);
229         start(securityServiceGBean);
230         start(loginServiceGBean);
231         start(propertiesLMGBean);
232         start(lmUseGBean);
233         start(propertiesRealmGBean);
234
235     }
236
237     protected void tearDownSecurity() throws Exception JavaDoc {
238         stop(propertiesRealmName);
239         stop(propertiesLMName);
240         stop(loginServiceName);
241         stop(securityServiceName);
242         stop(serverInfoName);
243         stop(loginConfigurationName);
244     }
245
246     private void start(GBeanData gbeanData) throws Exception JavaDoc {
247         kernel.loadGBean(gbeanData, cl);
248         kernel.startGBean(gbeanData.getName());
249         if (kernel.getGBeanState(gbeanData.getName()) != State.RUNNING_INDEX) {
250             fail("gbean not started: " + gbeanData.getName());
251         }
252     }
253
254     private void stop(ObjectName JavaDoc name) throws Exception JavaDoc {
255         kernel.stopGBean(name);
256         kernel.unloadGBean(name);
257     }
258
259     protected void setUp() throws Exception JavaDoc {
260         cl = this.getClass().getClassLoader();
261         containerName = NameFactory.getWebComponentName(null, null, null, null, "jettyContainer", "WebResource", moduleContext);
262         connectorName = NameFactory.getWebComponentName(null, null, null, null, "jettyConnector", "WebResource", moduleContext);
263         webModuleName = NameFactory.getModuleName(null, null, null, null, "testModule", moduleContext);
264
265         tmName = NameFactory.getComponentName(null, null, null, null, null, "TransactionManager", NameFactory.JTA_RESOURCE, moduleContext);
266         tcmName = NameFactory.getComponentName(null, null, null, null, null, "TransactionContextManager", NameFactory.JTA_RESOURCE, moduleContext);
267         ctcName = new ObjectName JavaDoc("geronimo.test:role=ConnectionTrackingCoordinator");
268
269         kernel = KernelFactory.newInstance().createKernel("test.kernel");
270         kernel.boot();
271         container = new GBeanData(containerName, JettyContainerImpl.GBEAN_INFO);
272
273         connector = new GBeanData(connectorName, HTTPConnector.GBEAN_INFO);
274         connector.setAttribute("port", new Integer JavaDoc(5678));
275         connector.setReferencePattern("JettyContainer", containerName);
276
277         start(container);
278         start(connector);
279
280         tm = new GBeanData(tmName, TransactionManagerImpl.GBEAN_INFO);
281         Set JavaDoc patterns = new HashSet JavaDoc();
282         patterns.add(ObjectName.getInstance("geronimo.server:j2eeType=JCAManagedConnectionFactory,*"));
283         tm.setAttribute("defaultTransactionTimeoutSeconds", new Integer JavaDoc(10));
284         tm.setReferencePatterns("ResourceManagers", patterns);
285         start(tm);
286         tcm = new GBeanData(tcmName, TransactionContextManager.GBEAN_INFO);
287         tcm.setReferencePattern("TransactionManager", tmName);
288         start(tcm);
289         ctc = new GBeanData(ctcName, ConnectionTrackingCoordinator.GBEAN_INFO);
290         start(ctc);
291     }
292
293     protected void tearDown() throws Exception JavaDoc {
294         stop(ctcName);
295         stop(tmName);
296         stop(connectorName);
297         stop(containerName);
298         kernel.shutdown();
299     }
300 }
301
Popular Tags