KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > TestPageTag


1 /*
2  * $Id: TestPageTag.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.bean;
19
20 import javax.servlet.jsp.PageContext JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.cactus.WebResponse;
26 import org.apache.struts.taglib.TaglibTestBase;
27
28 /**
29  * Suite of unit tests for the
30  * <code>org.apache.struts.taglib.bean.PageTag</code> class.
31  *
32  */

33 public class TestPageTag extends TaglibTestBase {
34
35     protected final static String JavaDoc PAGETAG_KEY = "PAGETAG_KEY";
36     protected final static String JavaDoc PAGETAG_VAL = "PAGETAG_VAL";
37
38     /**
39      * Defines the testcase name for JUnit.
40      *
41      * @param theName the testcase's name.
42      */

43     public TestPageTag(String JavaDoc theName) {
44         super(theName);
45     }
46
47     /**
48      * Start the tests.
49      *
50      * @param theArgs the arguments. Not used
51      */

52     public static void main(String JavaDoc[] theArgs) {
53         junit.awtui.TestRunner.main(new String JavaDoc[] {TestPageTag.class.getName()});
54     }
55
56     /**
57      * @return a test suite (<code>TestSuite</code>) that includes all methods
58      * starting with "test"
59      */

60     public static Test suite() {
61         // All methods starting with "test" will be executed in the test suite.
62
return new TestSuite(TestPageTag.class);
63     }
64
65     private void runMyTest(String JavaDoc whichTest) throws Exception JavaDoc {
66         request.setAttribute("runTest", whichTest);
67         pageContext.forward("/test/org/apache/struts/taglib/bean/TestPageTag.jsp");
68     }
69
70         private void formatAndTest(String JavaDoc compare, String JavaDoc output) {
71                 //fix for introduced carriage return / line feeds
72
output = replace(output,"\r","");
73                 output = replace(output,"\n","");
74                 output = output.trim();
75                 //System.out.println("Testing [" + compare + "] == [" + output + "]");
76
assertEquals(compare, output);
77         }
78
79     public void testPageTagApplication() throws Exception JavaDoc {
80         pageContext.setAttribute(PAGETAG_KEY, PAGETAG_VAL, PageContext.APPLICATION_SCOPE);
81         runMyTest("testPageTagApplication");
82         }
83         public void endPageTagApplication(WebResponse response){
84                 formatAndTest(PAGETAG_VAL, response.getText());
85         }
86
87     public void testPageTagSession() throws Exception JavaDoc {
88         pageContext.setAttribute(PAGETAG_KEY, PAGETAG_VAL, PageContext.SESSION_SCOPE);
89         runMyTest("testPageTagSession");
90         }
91         public void endPageTagSession(WebResponse response){
92                 formatAndTest(PAGETAG_VAL, response.getText());
93         }
94
95     public void testPageTagRequest() throws Exception JavaDoc {
96         pageContext.setAttribute(PAGETAG_KEY, PAGETAG_VAL, PageContext.REQUEST_SCOPE);
97         runMyTest("testPageTagRequest");
98         }
99         public void endPageTagRequest(WebResponse response){
100                 formatAndTest(PAGETAG_VAL, response.getText());
101         }
102
103     public void testPageTagResponse() throws Exception JavaDoc {
104         pageContext.setAttribute(PAGETAG_KEY, PAGETAG_VAL, PageContext.APPLICATION_SCOPE);
105         runMyTest("testPageTagResponse");
106         }
107         public void endPageTagResponse(WebResponse response){
108                 formatAndTest(PAGETAG_VAL, response.getText());
109         }
110
111     public void testPageTagConfig() throws Exception JavaDoc {
112         config.getServletContext().setAttribute(PAGETAG_KEY, PAGETAG_VAL);
113         runMyTest("testPageTagConfig");
114         }
115         public void endPageTagConfig(WebResponse response){
116                 formatAndTest(PAGETAG_VAL, response.getText());
117         }
118
119
120 }
121
Popular Tags