View Javadoc

1   package org.apache.struts.chain.commands.servlet;
2   
3   import junit.framework.TestCase;
4   
5   import org.apache.commons.chain.web.servlet.ServletWebContext;
6   import org.apache.struts.chain.commands.UnauthorizedActionException;
7   import org.apache.struts.chain.contexts.ServletActionContext;
8   import org.apache.struts.config.ActionConfig;
9   import org.apache.struts.mock.MockActionServlet;
10  import org.apache.struts.mock.MockHttpServletRequest;
11  import org.apache.struts.mock.MockHttpServletResponse;
12  import org.apache.struts.mock.MockPrincipal;
13  import org.apache.struts.mock.MockServletConfig;
14  import org.apache.struts.mock.MockServletContext;
15  
16  /* JUnitTest case for class: org.apache.struts.chain.commands.servlet.AuthorizeAction */
17  public class TestAuthorizeAction extends TestCase {
18      MockHttpServletRequest request = null;
19      MockPrincipal principal = null;
20      ServletWebContext swContext = null;
21      ServletActionContext saContext = null;
22      AuthorizeAction command = null;
23  
24      public TestAuthorizeAction(String _name) {
25          super(_name);
26      }
27  
28      /* setUp method for test case */
29      protected void setUp() throws Exception {
30          this.request = new MockHttpServletRequest();
31          this.principal =
32              new MockPrincipal("Mr. Macri", new String[] { "administrator" });
33          this.request.setUserPrincipal(principal);
34  
35          MockServletConfig servletConfig = new MockServletConfig();
36          MockServletContext servletContext = new MockServletContext();
37          MockActionServlet servlet =
38              new MockActionServlet(servletContext, servletConfig);
39  
40          servlet.initInternal();
41  
42          this.saContext =
43              new ServletActionContext(servletContext, request,
44                  new MockHttpServletResponse());
45  
46          this.saContext.setActionServlet(servlet);
47          this.command = new AuthorizeAction();
48      }
49  
50      /* tearDown method for test case */
51      protected void tearDown() {
52      }
53  
54      public void testAuthorizeOneRole()
55          throws Exception {
56          ActionConfig config = new ActionConfig();
57  
58          config.setPath("/testAuthorizeOneRole");
59          config.setRoles("administrator");
60          this.saContext.setActionConfig(config);
61  
62          boolean result = command.execute(saContext);
63  
64          assertFalse(result);
65      }
66  
67      public void testAuthorizeOneOfManyRoles()
68          throws Exception {
69          ActionConfig config = new ActionConfig();
70  
71          config.setPath("/testAuthorizeOneOfManyRoles");
72          config.setRoles("administrator,roustabout,memory");
73          this.saContext.setActionConfig(config);
74  
75          boolean result = command.execute(saContext);
76  
77          assertFalse(result);
78      }
79  
80      public void testAuthorizeNoRoles()
81          throws Exception {
82          ActionConfig config = new ActionConfig();
83  
84          config.setPath("/testAuthorizeNoRoles");
85          this.saContext.setActionConfig(config);
86  
87          boolean result = command.execute(saContext);
88  
89          assertFalse(result);
90      }
91  
92      public void testNotAuthorizedOneRole()
93          throws Exception {
94          ActionConfig config = new ActionConfig();
95  
96          config.setPath("/testNotAuthorizedOneRole");
97          config.setRoles("roustabout");
98          this.saContext.setActionConfig(config);
99  
100         try {
101             boolean result = command.execute(saContext);
102         } catch (UnauthorizedActionException ex) {
103         }
104     }
105 
106     public void testNotAuthorizedOneOfManyRoles()
107         throws Exception {
108         ActionConfig config = new ActionConfig();
109 
110         config.setPath("/testNotAuthorizedOneOfManyRoles");
111         config.setRoles("roustabout,memory");
112         this.saContext.setActionConfig(config);
113 
114         try {
115             boolean result = command.execute(saContext);
116         } catch (UnauthorizedActionException ex) {
117         }
118     }
119 
120     /* Executes the test case */
121     public static void main(String[] argv) {
122         String[] testCaseList = { TestAuthorizeAction.class.getName() };
123 
124         junit.textui.TestRunner.main(testCaseList);
125     }
126 }