001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.tools.pamanager;
018:
019: import java.io.File;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023: import junit.textui.TestRunner;
024:
025: import org.apache.jetspeed.AbstractRequestContextTestCase;
026: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
027: import org.apache.jetspeed.om.common.servlet.MutableWebApplication;
028: import org.apache.jetspeed.om.servlet.impl.SecurityRoleImpl;
029: import org.apache.jetspeed.util.DirectoryHelper;
030: import org.apache.jetspeed.util.descriptor.PortletApplicationWar;
031: import org.apache.pluto.om.common.SecurityRole;
032: import org.apache.pluto.om.common.SecurityRoleRef;
033: import org.apache.pluto.om.common.SecurityRoleRefSet;
034: import org.apache.pluto.om.common.SecurityRoleSet;
035: import org.apache.pluto.om.portlet.PortletDefinition;
036:
037: /**
038: * TestPortletDescriptorSecurityRoles - test and validate security roles and
039: * security role references from portlet.xml and web.xml deployment descriptor.
040: *
041: * @author <a href="ate@douma.nu">Ate Douma </a>
042: *
043: * @version $Id: TestPortletDescriptorSecurityRoles.java,v 1.4 2004/05/27
044: * 19:57:24 weaver Exp $
045: */
046: public class TestPortletDescriptorSecurityRoles extends
047: AbstractRequestContextTestCase {
048:
049: /**
050: * Start the tests.
051: *
052: * @param args
053: * the arguments. Not used
054: */
055: public static void main(String args[]) {
056: TestRunner
057: .main(new String[] { TestPortletDescriptorSecurityRoles.class
058: .getName() });
059: }
060:
061: /**
062: * Creates the test suite.
063: *
064: * @return a test suite (<code>TestSuite</code>) that includes all
065: * methods starting with "test"
066: */
067: public static Test suite() {
068: // All methods starting with "test" will be executed in the test suite.
069: return new TestSuite(TestPortletDescriptorSecurityRoles.class);
070: }
071:
072: public void setUp() throws Exception {
073: super .setUp();
074:
075: }
076:
077: public void testSecurityRoles() throws Exception {
078: System.out.println("Testing securityRoles");
079: File warFile = new File("./test/testdata/deploy/webapp");
080: PortletApplicationWar paWar = new PortletApplicationWar(
081: new DirectoryHelper(warFile), "unit-test", "/");
082:
083: MutablePortletApplication app = paWar.createPortletApp();
084: assertNotNull("App is null", app);
085:
086: MutableWebApplication webApp = paWar.createWebApp();
087: assertNotNull("WebApp is null", webApp);
088:
089: app.setWebApplicationDefinition(webApp);
090:
091: PortletDefinition portlet = app
092: .getPortletDefinitionByName("TestPortlet");
093: assertNotNull("TestPortlet is null", portlet);
094: checkWebSecurityRoles(webApp);
095: checkPortletSecurityRoleRefs(portlet);
096: boolean validateFailed = false;
097: try {
098: paWar.validate();
099: } catch (PortletApplicationException e) {
100: validateFailed = true;
101: }
102: assertTrue("Invalid PortletDescriptor validation result",
103: validateFailed);
104: SecurityRoleImpl role = new SecurityRoleImpl();
105: role.setRoleName("users.manager");
106: webApp.addSecurityRole(role);
107: try {
108: paWar.validate();
109: validateFailed = false;
110: } catch (PortletApplicationException e) {
111: }
112: assertEquals("Invalid PortletDescriptor validation result",
113: false, validateFailed);
114:
115: // persist the app
116: try {
117:
118: portletRegistry.registerPortletApplication(app);
119:
120: } catch (Exception e) {
121: String msg = "Unable to register portlet application, "
122: + app.getName()
123: + ", through the portlet registry: " + e.toString();
124:
125: throw new Exception(msg, e);
126: }
127: // clear cache
128:
129: // read back in
130: app = portletRegistry.getPortletApplication("unit-test");
131: validateFailed = true;
132: try {
133: paWar.validate();
134: validateFailed = false;
135: } catch (PortletApplicationException e) {
136: }
137: assertEquals(
138: "Invalid loaded PortletDescriptor validation result",
139: false, validateFailed);
140:
141: // remove the app
142: try {
143:
144: portletRegistry.removeApplication(app);
145:
146: } catch (Exception e) {
147: String msg = "Unable to remove portlet application, "
148: + app.getName()
149: + ", through the portlet portletRegistry: "
150: + e.toString();
151: throw new Exception(msg, e);
152: }
153:
154: }
155:
156: private void checkWebSecurityRoles(MutableWebApplication webApp) {
157: SecurityRoleSet roles = webApp.getSecurityRoles();
158: assertEquals(
159: "Invalid number of security role definitions found", 1,
160: roles.size());
161: SecurityRole role = roles.get("users.admin");
162: assertNotNull("Role users.admin undefined", role);
163: }
164:
165: private void checkPortletSecurityRoleRefs(PortletDefinition portlet) {
166: SecurityRoleRefSet roleRefs = portlet
167: .getInitSecurityRoleRefSet();
168: assertEquals(
169: "Invalid number of security role references found", 2,
170: roleRefs.size());
171: SecurityRoleRef roleRef = roleRefs.get("admin");
172: assertNotNull("Security Role Ref admin undefined", roleRef);
173: assertEquals("security Role link expected", "users.admin",
174: roleRef.getRoleLink());
175: roleRef = roleRefs.get("users.manager");
176: assertNotNull("Security Role Ref users.manager undefined",
177: roleRef);
178: assertNull(
179: "Undefined security Role link for users.managers expected",
180: roleRef.getRoleLink());
181: }
182: }
|