KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > portlet > test > SecurityMappingTest


1 /*
2  * Copyright 2003,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 package org.apache.pluto.portalImpl.portlet.test;
17
18 import java.util.Map JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import javax.portlet.ActionRequest;
22 import javax.portlet.ActionResponse;
23 import javax.portlet.PortletContext;
24 import javax.portlet.PortletRequest;
25
26 import org.apache.pluto.testsuite.ExpectedResults;
27
28 /**
29  * @author <a HREF="ddewolf@apache.org">David H. DeWolf</a>
30  */

31 public class SecurityMappingTest extends AbstractReflectivePortletTest {
32
33     public String JavaDoc getTestSuiteName() {
34         return "Security Mapping Tests";
35     }
36
37     public Map JavaDoc doPrerequisiteAction(PortletContext context, ActionRequest req,
38                                     ActionResponse res) {
39         return new java.util.HashMap JavaDoc();
40     }
41
42     protected TestResult checkIsUserInMappedRole(PortletRequest req) {
43         TestResult res = isUserLoggedIn(req);
44         res.setName("User In Mapped Role Test");
45         res.setDesc("Test if user is in mapped role");
46         if(TestResult.WARNING.equals(res.getReturnCode())) {
47             return res;
48         }
49
50         Properties JavaDoc props = ExpectedResults.getExpectedProperties();
51         String JavaDoc role = props.getProperty("expected.security.role.mapped");
52
53         if(req.isUserInRole(role)) {
54             res.setReturnCode(TestResult.PASSED);
55         }
56         else {
57             res.setReturnCode(TestResult.WARNING);
58             res.setReturnCode("User is not in the expected '"+role+"' role. This may be due to misconfiuration.");
59         }
60
61         return res;
62     }
63
64     protected TestResult checkIsUserInUnmappedRole(PortletRequest req) {
65         TestResult res = isUserLoggedIn(req);
66         res.setName("User In Unmapped Role Test");
67         res.setDesc("Test if user is in unmapped role");
68         if(TestResult.WARNING.equals(res.getReturnCode())) {
69             return res;
70         }
71
72         Properties JavaDoc props = ExpectedResults.getExpectedProperties();
73         String JavaDoc role = props.getProperty("expected.security.role");
74
75         if(req.isUserInRole(role)) {
76             res.setReturnCode(TestResult.PASSED);
77         }
78         else {
79             res.setReturnCode(TestResult.WARNING);
80             res.setReturnCode("User is not in the expected '"+role+"' role. This may be due to misconfiuration.");
81         }
82
83         return res;
84     }
85
86     protected TestResult checkIsUserIndUndeclaredRole(PortletRequest req) {
87         TestResult res = isUserLoggedIn(req);
88         res.setName("User In Undeclared Role Test");
89         res.setDesc("Test if user is in undeclared role");
90         if(TestResult.WARNING.equals(res.getReturnCode())) {
91             return res;
92         }
93
94         if(!req.isUserInRole("fakeTestRoleFooBar")) {
95             res.setReturnCode(TestResult.PASSED);
96         }
97         else {
98             res.setReturnCode(TestResult.FAILED);
99             res.setReturnCode("User is in the fake role named 'fakeTestRoleFooBar'");
100         }
101
102         return res;
103     }
104
105
106     private TestResult isUserLoggedIn(PortletRequest req) {
107         if (req.getRemoteUser()==null) {
108             TestResult res = new TestResult();
109             res.setReturnCode(TestResult.WARNING);
110             res.setResults("User is not logged in.");
111             return res;
112         }
113         return new TestResult();
114     }
115 }
116
Popular Tags