KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TestHeaderTag.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 junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.cactus.WebRequest;
24 import org.apache.cactus.WebResponse;
25 import org.apache.struts.taglib.TaglibTestBase;
26
27 /**
28  * Suite of unit tests for the
29  * <code>org.apache.struts.taglib.bean.HeaderTag</code> class.
30  *
31  */

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

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

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

59     public static Test suite() {
60         // All methods starting with "test" will be executed in the test suite.
61
return new TestSuite(TestHeaderTag.class);
62     }
63
64     private void runMyTest(String JavaDoc whichTest) throws Exception JavaDoc {
65                 request.setAttribute("runTest", whichTest);
66                 pageContext.forward("/test/org/apache/struts/taglib/bean/TestHeaderTag.jsp");
67     }
68
69         private void formatAndTest(String JavaDoc compare, String JavaDoc output) {
70                 //fix for introduced carriage return / line feeds
71
output = replace(output,"\r","");
72                 output = replace(output,"\n","");
73                 output = output.trim();
74             assertEquals(compare, output);
75         }
76
77
78     /*
79      * Testing HeaderTag, with Name specified in tags.
80      */

81     public void beginHeaderTagName(WebRequest webRequest) {
82                 webRequest.addHeader(HEADER_KEY, HEADER_VAL);
83         webRequest.addParameter("cacheId", "1");
84     }
85     public void testHeaderTagName() throws Exception JavaDoc {
86         runMyTest("testHeaderTagName");
87         }
88         public void endHeaderTagName(WebResponse response){
89                 formatAndTest(HEADER_VAL, response.getText());
90         }
91
92
93     /*
94      * Testing HeaderTag, with Name and Multiple specified in tags.
95      */

96     public void beginHeaderTagNameMultiple(WebRequest webRequest) {
97         for (int i = 0; i < 10; i++) {
98                webRequest.addHeader(HEADER_KEY, HEADER_VAL + i);
99                 }
100         webRequest.addParameter("cacheId", "1");
101     }
102     public void testHeaderTagNameMultiple() throws Exception JavaDoc {
103         runMyTest("testHeaderTagNameMultiple");
104         }
105         public void endHeaderTagNameMultiple(WebResponse response){
106             String JavaDoc compareTo = "";
107             //Multiple Headers are comma delimited
108
for (int i = 0; i < 10; i++) {
109                         compareTo += HEADER_VAL + i + ",";
110                 }
111                 //remove the trailing comma
112
compareTo = compareTo.substring(0, compareTo.length() - 1);
113                 formatAndTest(compareTo, response.getText());
114         }
115
116     /*
117      * Testing HeaderTag, with Name and Value specified in tags.
118      */

119     public void beginHeaderTagNameValue(WebRequest webRequest) {
120         webRequest.addParameter("cacheId", "1");
121     }
122     public void testHeaderTagNameValue() throws Exception JavaDoc {
123         runMyTest("testHeaderTagNameValue");
124         }
125         public void endHeaderTagNameValue(WebResponse response){
126                 formatAndTest(HEADER_VAL, response.getText());
127         }
128
129
130
131
132
133 }
134
Popular Tags