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.contexts.ServletActionContext;
7   import org.apache.struts.config.ForwardConfig;
8   import org.apache.struts.mock.MockActionServlet;
9   import org.apache.struts.mock.MockHttpServletRequest;
10  import org.apache.struts.mock.MockHttpServletResponse;
11  import org.apache.struts.mock.MockPrincipal;
12  import org.apache.struts.mock.MockServletConfig;
13  import org.apache.struts.mock.MockServletContext;
14  
15  /* JUnitTest case for class: org.apache.struts.chain.commands.servlet.PerformForward */
16  public class TestPerformForward extends TestCase {
17      MockHttpServletRequest request = null;
18      MockPrincipal principal = null;
19      ServletWebContext swContext = null;
20      ServletActionContext saContext = null;
21      PerformForward command = null;
22  
23      public TestPerformForward(String _name) {
24          super(_name);
25      }
26  
27      /* setUp method for test case */
28      protected void setUp() throws Exception {
29          this.request = new MockHttpServletRequest();
30          this.principal =
31              new MockPrincipal("Mr. Macri", new String[] { "administrator" });
32          this.request.setUserPrincipal(principal);
33  
34          MockServletConfig servletConfig = new MockServletConfig();
35          MockServletContext servletContext = new MockServletContext();
36          MockActionServlet servlet =
37              new MockActionServlet(servletContext, servletConfig);
38  
39          servlet.initInternal();
40  
41          this.saContext =
42              new ServletActionContext(servletContext, request,
43                  new MockHttpServletResponse());
44  
45          this.saContext.setActionServlet(servlet);
46          this.command = new PerformForward();
47      }
48  
49      /* tearDown method for test case */
50      protected void tearDown() {
51      }
52  
53      public void testNullForwardPath()
54          throws Exception {
55          ForwardConfig config = new ForwardConfig();
56  
57          config.setPath(null);
58  
59          try {
60              command.perform(saContext, config);
61              fail(
62                  "Didn't throw an illegal argument exception on null forward path");
63          } catch (IllegalArgumentException ex) {
64              System.out.println("exception: " + ex.getMessage());
65  
66              // Do nothing, the test passed
67          }
68      }
69  
70      /* Executes the test case */
71      public static void main(String[] argv) {
72          String[] testCaseList = { TestPerformForward.class.getName() };
73  
74          junit.textui.TestRunner.main(testCaseList);
75      }
76  }