KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > AccessControlTest


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

17
18 /* $Id: AccessControlTest.java 42625 2004-03-04 15:45:03Z egli $ */
19
20 package org.apache.lenya.ac.impl;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.avalon.framework.component.ComponentSelector;
25 import org.apache.lenya.ac.AccessControlException;
26 import org.apache.lenya.ac.AccessControllerResolver;
27 import org.apache.lenya.ac.AccreditableManager;
28 import org.apache.lenya.ac.Identity;
29 import org.apache.lenya.ac.User;
30 import org.apache.lenya.ac.file.FileAccreditableManager;
31 import org.apache.lenya.ac.file.FilePolicyManager;
32 import org.apache.lenya.cms.ExcaliburTest;
33 import org.apache.lenya.cms.PublicationHelper;
34 import org.apache.lenya.cms.ac.PublicationAccessControllerResolver;
35
36 /**
37  * To change the template for this generated type comment go to
38  * Window>Preferences>Java>Code Generation>Code and Comments
39  */

40 public class AccessControlTest extends ExcaliburTest {
41
42     private ComponentSelector accessControllerResolverSelector;
43     private AccessControllerResolver accessControllerResolver;
44     private DefaultAccessController accessController;
45     
46     private File JavaDoc accreditablesDirectory;
47
48     /**
49      * @param test The test.
50      */

51     public AccessControlTest(String JavaDoc test) {
52         super(test);
53     }
54
55     /**
56      * Returns the access controller.
57      * @return An access controller.
58      */

59     public DefaultAccessController getAccessController() {
60         return accessController;
61     }
62
63     protected static final String JavaDoc URL = "/test/authoring/index.html";
64
65     /** @see junit.framework.TestCase#setUp() */
66     protected void setUp() throws Exception JavaDoc {
67
68         if (PublicationHelper.getPublication() == null) {
69             String JavaDoc[] args = { "D:\\Development\\build\\tomcat-4.1.24\\webapps\\lenya", "test" };
70             PublicationHelper.extractPublicationArguments(args);
71         }
72
73         super.setUp();
74
75         accessControllerResolverSelector =
76             (ComponentSelector) manager.lookup(AccessControllerResolver.ROLE + "Selector");
77         assertNotNull(accessControllerResolverSelector);
78
79         accessControllerResolver =
80             (AccessControllerResolver) accessControllerResolverSelector.select(
81                 AccessControllerResolver.DEFAULT_RESOLVER);
82
83         assertNotNull(accessControllerResolver);
84         getLogger().info(
85             "Using access controller resolver: [" + accessControllerResolver.getClass() + "]");
86
87         accessController =
88             (DefaultAccessController)
89                 (
90                     (
91                         PublicationAccessControllerResolver) accessControllerResolver)
92                             .resolveAccessController(
93                 URL);
94
95         assertNotNull(accessController);
96         getLogger().info("Resolved access controller: [" + accessController.getClass() + "]");
97
98         File JavaDoc servletContext = PublicationHelper.getPublication().getServletContext();
99         ((FilePolicyManager) accessController.getPolicyManager()).setPoliciesDirectory(
100             servletContext);
101
102         accreditablesDirectory =
103             new File JavaDoc(
104                 PublicationHelper.getPublication().getDirectory(),
105                 "config/ac/passwd".replace('/', File.separatorChar));
106         (
107             (FileAccreditableManager) accessController
108                 .getAccreditableManager())
109                 .setConfigurationDirectory(
110             accreditablesDirectory);
111
112     }
113
114     /**
115      * The teardown method for JUnit
116      *
117      * @exception Exception Description of Exception
118      * @since
119      */

120     public void tearDown() throws Exception JavaDoc {
121         super.tearDown();
122
123         if (accessControllerResolverSelector != null) {
124             if (accessControllerResolver != null) {
125                 if (accessController != null) {
126                     accessControllerResolver.release(accessController);
127                 }
128                 accessControllerResolverSelector.release(accessControllerResolver);
129             }
130             manager.release(accessControllerResolver);
131         }
132     }
133
134     protected static final String JavaDoc USERNAME = "lenya";
135
136     /**
137      * Returns the identity.
138      * @return The identity.
139      * @throws AccessControlException when something went wrong.
140      */

141     protected Identity getIdentity() throws AccessControlException {
142         DefaultAccessController controller = getAccessController();
143         User user = controller.getAccreditableManager().getUserManager().getUser(USERNAME);
144         assertNotNull(user);
145
146         Identity identity = new Identity();
147         identity.addIdentifiable(user);
148
149         return identity;
150     }
151
152     /**
153      * Returns the policy manager.
154      * @return A policy manager.
155      */

156     protected FilePolicyManager getPolicyManager() {
157         return (FilePolicyManager) getAccessController().getPolicyManager();
158     }
159
160     /**
161      * Returns the accreditable manager.
162      * @return An accreditable manager.
163      */

164     protected AccreditableManager getAccreditableManager() {
165         return getAccessController().getAccreditableManager();
166     }
167
168     /**
169      * Returns the directories where accreditables are stored.
170      * @return A file.
171      */

172     protected File JavaDoc getAccreditablesDirectory() {
173         return accreditablesDirectory;
174     }
175
176 }
177
Popular Tags