001: /*
002: * $Id: TestAuthorizeAction.java 481833 2006-12-03 17:32:52Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.chain.commands.servlet;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.commons.chain.web.servlet.ServletWebContext;
026: import org.apache.struts.chain.commands.UnauthorizedActionException;
027: import org.apache.struts.chain.contexts.ServletActionContext;
028: import org.apache.struts.config.ActionConfig;
029: import org.apache.struts.mock.MockActionServlet;
030: import org.apache.struts.mock.MockHttpServletRequest;
031: import org.apache.struts.mock.MockHttpServletResponse;
032: import org.apache.struts.mock.MockPrincipal;
033: import org.apache.struts.mock.MockServletConfig;
034: import org.apache.struts.mock.MockServletContext;
035:
036: /* JUnitTest case for class: org.apache.struts.chain.commands.servlet.AuthorizeAction */
037: public class TestAuthorizeAction extends TestCase {
038: MockHttpServletRequest request = null;
039: MockPrincipal principal = null;
040: ServletWebContext swContext = null;
041: ServletActionContext saContext = null;
042: AuthorizeAction command = null;
043:
044: public TestAuthorizeAction(String _name) {
045: super (_name);
046: }
047:
048: /* setUp method for test case */
049: protected void setUp() throws Exception {
050: this .request = new MockHttpServletRequest();
051: this .principal = new MockPrincipal("Mr. Macri",
052: new String[] { "administrator" });
053: this .request.setUserPrincipal(principal);
054:
055: MockServletConfig servletConfig = new MockServletConfig();
056: MockServletContext servletContext = new MockServletContext();
057: MockActionServlet servlet = new MockActionServlet(
058: servletContext, servletConfig);
059:
060: servlet.initInternal();
061:
062: this .saContext = new ServletActionContext(servletContext,
063: request, new MockHttpServletResponse());
064:
065: this .saContext.setActionServlet(servlet);
066: this .command = new AuthorizeAction();
067: }
068:
069: /* tearDown method for test case */
070: protected void tearDown() {
071: }
072:
073: public void testAuthorizeOneRole() throws Exception {
074: ActionConfig config = new ActionConfig();
075:
076: config.setPath("/testAuthorizeOneRole");
077: config.setRoles("administrator");
078: this .saContext.setActionConfig(config);
079:
080: boolean result = command.execute(saContext);
081:
082: assertFalse(result);
083: }
084:
085: public void testAuthorizeOneOfManyRoles() throws Exception {
086: ActionConfig config = new ActionConfig();
087:
088: config.setPath("/testAuthorizeOneOfManyRoles");
089: config.setRoles("administrator,roustabout,memory");
090: this .saContext.setActionConfig(config);
091:
092: boolean result = command.execute(saContext);
093:
094: assertFalse(result);
095: }
096:
097: public void testAuthorizeNoRoles() throws Exception {
098: ActionConfig config = new ActionConfig();
099:
100: config.setPath("/testAuthorizeNoRoles");
101: this .saContext.setActionConfig(config);
102:
103: boolean result = command.execute(saContext);
104:
105: assertFalse(result);
106: }
107:
108: public void testNotAuthorizedOneRole() throws Exception {
109: ActionConfig config = new ActionConfig();
110:
111: config.setPath("/testNotAuthorizedOneRole");
112: config.setRoles("roustabout");
113: this .saContext.setActionConfig(config);
114:
115: try {
116: boolean result = command.execute(saContext);
117: } catch (UnauthorizedActionException ex) {
118: }
119: }
120:
121: public void testNotAuthorizedOneOfManyRoles() throws Exception {
122: ActionConfig config = new ActionConfig();
123:
124: config.setPath("/testNotAuthorizedOneOfManyRoles");
125: config.setRoles("roustabout,memory");
126: this .saContext.setActionConfig(config);
127:
128: try {
129: boolean result = command.execute(saContext);
130: } catch (UnauthorizedActionException ex) {
131: }
132: }
133:
134: /* Executes the test case */
135: public static void main(String[] argv) {
136: String[] testCaseList = { TestAuthorizeAction.class.getName() };
137:
138: junit.textui.TestRunner.main(testCaseList);
139: }
140: }
|