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.page;
18  
19  import java.security.Principal;
20  import java.util.HashSet;
21  import java.util.Set;
22  
23  import javax.security.auth.Subject;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  import org.apache.jetspeed.om.folder.Folder;
30  import org.apache.jetspeed.page.psml.CastorXmlPageManager;
31  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
32  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
33  
34  /***
35   * TestSecureCastorXmlPageManager
36   * 
37   * @author <a href="rwatler@apache.org">Randy Watler</a>
38   * @version $Id$
39   */
40  public class TestCreateUserHomePagesFromRoles extends TestCase implements PageManagerTestShared 
41  {
42      protected CastorXmlPageManager pageManager;
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see junit.framework.TestCase#setUp()
48       */
49      protected void setUp() throws Exception
50      {
51          super.setUp();
52          pageManager = Shared.makeCastorXMLPageManager("secure-pages", false, true);
53      }
54  
55      /***
56       * <p>
57       * tearDown
58       * </p>
59       * 
60       * @see junit.framework.TestCase#tearDown()
61       * @throws java.lang.Exception
62       */
63      protected void tearDown() throws Exception
64      {
65          super.tearDown();
66      }
67  
68      /***
69       * Defines the testcase name for JUnit.
70       * 
71       * @param name
72       *            the testcase's name.
73       */
74      public TestCreateUserHomePagesFromRoles( String name )
75      {
76          super(name);
77      }
78  
79      /***
80       * Start the tests.
81       * 
82       * @param args
83       *            the arguments. Not used
84       */
85      public static void main( String args[] )
86      {
87          junit.awtui.TestRunner.main(new String[]{TestCreateUserHomePagesFromRoles.class.getName()});
88      }
89  
90      /***
91       * Creates the test suite.
92       * 
93       * @return a test suite (<code>TestSuite</code>) that includes all
94       *         methods starting with "test"
95       */
96      public static Test suite()
97      {
98          // All methods starting with "test" will be executed in the test suite.
99          return new TestSuite(TestCreateUserHomePagesFromRoles.class);
100     }
101 
102     
103     static final String FOLDER1 = Folder.ROLE_FOLDER + "role1";
104     static final String FOLDER2 = Folder.ROLE_FOLDER + "role2";
105     static final String FOLDER3 = Folder.ROLE_FOLDER + "role3";
106         
107     static final String DEFAULT_PAGE = Folder.USER_FOLDER + "david" + Folder.PATH_SEPARATOR + "default-page.psml";
108     static final String ROLE_PAGE_1 = Folder.USER_FOLDER + "david" + Folder.PATH_SEPARATOR + "role1-default-page.psml";
109     static final String ROLE_PAGE_2 = Folder.USER_FOLDER + "david" + Folder.PATH_SEPARATOR + "role2-default-page.psml";
110     static final String ROLE_PAGE_3 = Folder.USER_FOLDER + "david" + Folder.PATH_SEPARATOR + "role3-default-page.psml";
111     static final String SUB_PAGE = Folder.USER_FOLDER + "david" + Folder.PATH_SEPARATOR + "sub1" + Folder.PATH_SEPARATOR + "default-page.psml";
112     static final String SUB_LINK = Folder.USER_FOLDER + "david" + Folder.PATH_SEPARATOR + "sub1" + Folder.PATH_SEPARATOR + "apache_portals.link";    
113     
114     public void testCreateUserHomePagesFromRoles() throws Exception
115     {
116         PageManager pageManager = Shared.makeCastorXMLPageManager("pages", false, false);         
117 
118         assertTrue("folder1 failed to create", pageManager.folderExists(FOLDER1));
119         assertTrue("folder2 failed to create", pageManager.folderExists(FOLDER2));
120         assertTrue("folder3 failed to create", pageManager.folderExists(FOLDER3));
121         
122         Set principals = new HashSet();
123         
124         // create the role principals
125         Principal rolePrincipal1 = new RolePrincipalImpl("role1");
126         Principal rolePrincipal2 = new RolePrincipalImpl("role2");
127         Principal rolePrincipal3 = new RolePrincipalImpl("role3");
128         principals.add(rolePrincipal1);
129         principals.add(rolePrincipal2);
130         principals.add(rolePrincipal3);
131         
132         // create the user principal
133         Principal userPrincipal = new UserPrincipalImpl("david");        
134         principals.add(userPrincipal);
135         
136         // create the subject
137         Subject subject = new Subject(true, principals, new HashSet(), new HashSet());
138 
139         pageManager.createUserHomePagesFromRoles(subject);
140         
141         assertTrue("failed to create default page", pageManager.pageExists(DEFAULT_PAGE));
142         assertTrue("failed to create sub page", pageManager.pageExists(SUB_PAGE));
143         assertTrue("failed to create sub link", pageManager.linkExists(SUB_LINK));
144     }
145     
146 }