View Javadoc

1   /*
2    * $Id: TaglibTestBase.java 54929 2004-10-16 16:38:42Z germuska $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.struts.Globals;
23  import org.apache.struts.config.ForwardConfig;
24  import org.apache.struts.config.ModuleConfig;
25  import org.apache.struts.config.impl.ModuleConfigImpl;
26  import org.apache.struts.mock.MockHttpServletRequest;
27  import org.apache.struts.mock.MockHttpServletResponse;
28  import org.apache.struts.mock.MockHttpSession;
29  import org.apache.struts.mock.MockPageContext;
30  import org.apache.struts.mock.MockServletConfig;
31  import org.apache.struts.mock.MockServletContext;
32  import org.apache.struts.util.MessageResources;
33  import org.apache.struts.util.MessageResourcesFactory;
34  import org.apache.struts.util.PropertyMessageResources;
35  
36  public class TagTestBase extends TestCase {
37      protected TagUtils tagutils = TagUtils.getInstance();
38      protected MockServletConfig servletConfig;
39      protected MockServletContext servletContext;
40      protected MockHttpServletRequest request;
41      protected MockPageContext pageContext;
42      protected ModuleConfig moduleConfig;
43      protected ModuleConfig moduleConfig2;
44      protected ModuleConfig moduleConfig3;
45  
46      public TagTestBase() {
47          super();
48      }
49  
50      public TagTestBase(String theName) {
51          super(theName);
52      }
53  
54      /***
55       * Helper method that creates/configures a basic configuration of Mock
56       * Objects.
57       *
58       *
59       * PageContext ServletConfig ServletContext HttpServletRequest HttpSession
60       * HttpServletResponse
61       *
62       * "/myapp", "/foo", null, null,
63       */
64      public void setUp() {
65          // -- default Module
66          this.moduleConfig = new ModuleConfigImpl("");
67          this.moduleConfig.addForwardConfig(new ForwardConfig("foo", "/bar.jsp",
68                  false));
69          this.moduleConfig.addForwardConfig(new ForwardConfig("relative1",
70                  "relative.jsp", false));
71          this.moduleConfig.addForwardConfig(new ForwardConfig("relative2",
72                  "relative.jsp", false));
73          this.moduleConfig.addForwardConfig(new ForwardConfig("external",
74                  "http://struts.apache.org/", false));
75  
76          // -- module "/2"
77          this.moduleConfig2 = new ModuleConfigImpl("/2");
78          this.moduleConfig2.addForwardConfig(new ForwardConfig("foo",
79                  "/baz.jsp", false));
80          this.moduleConfig2.addForwardConfig(new ForwardConfig("relative1",
81                  "relative.jsp", false));
82          this.moduleConfig2.addForwardConfig(new ForwardConfig("relative2",
83                  "relative.jsp", false));
84          this.moduleConfig2.addForwardConfig(new ForwardConfig("external",
85                  "http://struts.apache.org/", false));
86  
87          // -- module "/3"
88          this.moduleConfig3 = new ModuleConfigImpl("/3");
89  
90          // -- configure the ServletContext
91          this.servletContext = new MockServletContext();
92          this.servletContext.setAttribute(Globals.MODULE_KEY, moduleConfig);
93          this.servletContext.setAttribute(Globals.MODULE_KEY + "/2",
94              moduleConfig2);
95          this.servletContext.setAttribute(Globals.MODULE_KEY + "/3",
96              moduleConfig3);
97  
98          // -- configure the ServletConfig
99          this.servletConfig = new MockServletConfig();
100         this.servletConfig.setServletContext(servletContext);
101 
102         // -- configure the request
103         this.request = new MockHttpServletRequest(new MockHttpSession());
104 
105         pageContext =
106             new MockPageContext(servletConfig, request,
107                 new MockHttpServletResponse());
108     }
109 
110     public void tearDown() {
111         this.moduleConfig = null;
112         this.moduleConfig2 = null;
113         this.moduleConfig3 = null;
114         this.pageContext = null;
115         this.request = null;
116     }
117 
118     protected void putBundleInScope(int scope, boolean returnNull) {
119         MessageResourcesFactory factory =
120             MessageResourcesFactory.createFactory();
121         MessageResources messageResources =
122             new PropertyMessageResources(factory,
123                 "org.apache.struts.taglib.sample");
124 
125         messageResources.setReturnNull(returnNull);
126         pageContext.setAttribute(Globals.MESSAGES_KEY, messageResources, scope);
127     }
128 }