1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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.jetspeed.tools.pamanager;
18  
19  import java.io.File;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  
25  import org.apache.jetspeed.AbstractRequestContextTestCase;
26  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
27  import org.apache.jetspeed.om.common.servlet.MutableWebApplication;
28  import org.apache.jetspeed.om.servlet.impl.SecurityRoleImpl;
29  import org.apache.jetspeed.util.DirectoryHelper;
30  import org.apache.jetspeed.util.descriptor.PortletApplicationWar;
31  import org.apache.pluto.om.common.SecurityRole;
32  import org.apache.pluto.om.common.SecurityRoleRef;
33  import org.apache.pluto.om.common.SecurityRoleRefSet;
34  import org.apache.pluto.om.common.SecurityRoleSet;
35  import org.apache.pluto.om.portlet.PortletDefinition;
36  
37  /***
38   * TestPortletDescriptorSecurityRoles - test and validate security roles and
39   * security role references from portlet.xml and web.xml deployment descriptor.
40   *
41   * @author <a href="ate@douma.nu">Ate Douma </a>
42   *
43   * @version $Id: TestPortletDescriptorSecurityRoles.java,v 1.4 2004/05/27
44   *                19:57:24 weaver Exp $
45   */
46  public class TestPortletDescriptorSecurityRoles extends AbstractRequestContextTestCase
47  {
48  
49      /***
50       * Start the tests.
51       *
52       * @param args
53       *                  the arguments. Not used
54       */
55      public static void main( String args[] )
56      {
57          TestRunner.main(new String[]{TestPortletDescriptorSecurityRoles.class.getName()});
58      }
59  
60      /***
61       * Creates the test suite.
62       *
63       * @return a test suite (<code>TestSuite</code>) that includes all
64       *              methods starting with "test"
65       */
66      public static Test suite()
67      {
68          // All methods starting with "test" will be executed in the test suite.
69          return new TestSuite(TestPortletDescriptorSecurityRoles.class);
70      }
71  
72      public void setUp() throws Exception
73      {
74          super.setUp();
75          
76      }
77      
78      public void testSecurityRoles() throws Exception
79      {
80          System.out.println("Testing securityRoles");
81          File warFile = new File("./test/testdata/deploy/webapp");
82          PortletApplicationWar paWar = new PortletApplicationWar(new DirectoryHelper(warFile), "unit-test", "/" );
83  
84          MutablePortletApplication app = paWar.createPortletApp();
85          assertNotNull("App is null", app);
86  
87          MutableWebApplication webApp = paWar.createWebApp();
88          assertNotNull("WebApp is null", webApp);
89  
90          app.setWebApplicationDefinition(webApp);
91  
92          PortletDefinition portlet = app.getPortletDefinitionByName("TestPortlet");
93          assertNotNull("TestPortlet is null", portlet);
94          checkWebSecurityRoles(webApp);
95          checkPortletSecurityRoleRefs(portlet);
96          boolean validateFailed = false;
97          try
98          {
99              paWar.validate();
100         }
101         catch (PortletApplicationException e)
102         {
103             validateFailed = true;
104         }
105         assertTrue("Invalid PortletDescriptor validation result", validateFailed);
106         SecurityRoleImpl role = new SecurityRoleImpl();
107         role.setRoleName("users.manager");
108         webApp.addSecurityRole(role);
109         try
110         {
111             paWar.validate();
112             validateFailed = false;
113         }
114         catch (PortletApplicationException e)
115         {
116         }
117         assertEquals("Invalid PortletDescriptor validation result", false, validateFailed);
118 
119         // persist the app
120         try
121         {
122             
123             portletRegistry.registerPortletApplication(app);
124             
125         }
126         catch (Exception e)
127         {
128             String msg = "Unable to register portlet application, " + app.getName()
129                     + ", through the portlet registry: " + e.toString();
130             
131             throw new Exception(msg, e);
132         }
133         // clear cache
134         
135 
136         // read back in
137         app = portletRegistry.getPortletApplication("unit-test");
138         validateFailed = true;
139         try
140         {
141             paWar.validate();
142             validateFailed = false;
143         }
144         catch (PortletApplicationException e)
145         {
146         }
147         assertEquals("Invalid loaded PortletDescriptor validation result", false, validateFailed);
148 
149         // remove the app
150         try
151         {
152             
153             portletRegistry.removeApplication(app);
154             
155         }
156         catch (Exception e)
157         {
158             String msg = "Unable to remove portlet application, " + app.getName()
159                     + ", through the portlet portletRegistry: " + e.toString();
160             throw new Exception(msg, e);
161         }
162 
163     }
164 
165     private void checkWebSecurityRoles( MutableWebApplication webApp )
166     {
167         SecurityRoleSet roles = webApp.getSecurityRoles();
168         assertEquals("Invalid number of security role definitions found", 1, roles.size());
169         SecurityRole role = roles.get("users.admin");
170         assertNotNull("Role users.admin undefined", role);
171     }
172 
173     private void checkPortletSecurityRoleRefs( PortletDefinition portlet )
174     {
175         SecurityRoleRefSet roleRefs = portlet.getInitSecurityRoleRefSet();
176         assertEquals("Invalid number of security role references found", 2, roleRefs.size());
177         SecurityRoleRef roleRef = roleRefs.get("admin");
178         assertNotNull("Security Role Ref admin undefined", roleRef);
179         assertEquals("security Role link expected", "users.admin", roleRef.getRoleLink());
180         roleRef = roleRefs.get("users.manager");
181         assertNotNull("Security Role Ref users.manager undefined", roleRef);
182         assertNull("Undefined security Role link for users.managers expected", roleRef.getRoleLink());
183     }
184 }