KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TestWriteTag.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 java.util.Locale JavaDoc;
21
22 import javax.servlet.jsp.PageContext JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.cactus.WebResponse;
28 import org.apache.struts.Globals;
29 import org.apache.struts.taglib.SimpleBeanForTesting;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.taglib.TaglibTestBase;
32
33 /**
34  * Suite of unit tests for the
35  * <code>org.apache.struts.taglib.bean.WriteTag</code> class.
36  *
37  */

38 public class TestWriteTag extends TaglibTestBase {
39         protected final static String JavaDoc TEST_STRING_VAL = "Test Value";
40         protected final static Integer JavaDoc TEST_INTEGER_VAL = new Integer JavaDoc("1234");
41         protected final static Double JavaDoc TEST_DOUBLE_VAL = new Double JavaDoc("1234.5961");
42         protected final static String JavaDoc REQUEST_KEY = "REQUEST_KEY";
43
44     /**
45      * Defines the testcase name for JUnit.
46      *
47      * @param theName the testcase's name.
48      */

49     public TestWriteTag(String JavaDoc theName) {
50         super(theName);
51     }
52
53     /**
54      * Start the tests.
55      *
56      * @param theArgs the arguments. Not used
57      */

58     public static void main(String JavaDoc[] theArgs) {
59         junit.awtui.TestRunner.main(new String JavaDoc[] {TestWriteTag.class.getName()});
60     }
61
62     /**
63      * @return a test suite (<code>TestSuite</code>) that includes all methods
64      * starting with "test"
65      */

66     public static Test suite() {
67         // All methods starting with "test" will be executed in the test suite.
68
return new TestSuite(TestWriteTag.class);
69     }
70
71         private void formatAndTest(String JavaDoc compare, String JavaDoc output) {
72                 //fix for introduced carriage return / line feeds
73
output = replace(output,"\r","");
74                 output = replace(output,"\n","");
75                 output = output.trim();
76                 //System.out.println("Testing [" + compare + "] == [" + output + "]");
77
assertEquals(compare, output);
78         }
79
80     private void runMyTest(String JavaDoc whichTest, String JavaDoc locale) throws Exception JavaDoc {
81         pageContext.setAttribute(Globals.LOCALE_KEY, new Locale JavaDoc(locale, locale), PageContext.SESSION_SCOPE);
82                 request.setAttribute("runTest", whichTest);
83                 pageContext.forward("/test/org/apache/struts/taglib/bean/TestWriteTag.jsp");
84     }
85
86         // Name
87
public void testWriteTagName() throws Exception JavaDoc {
88                 pageContext.setAttribute(REQUEST_KEY,TEST_STRING_VAL,PageContext.REQUEST_SCOPE);
89                 runMyTest("testWriteTagName", "");
90         }
91         public void endWriteTagName(WebResponse response){
92                 formatAndTest(TEST_STRING_VAL, response.getText());
93         }
94
95         // Property
96
public void testWriteTagNameProperty() throws Exception JavaDoc {
97         SimpleBeanForTesting sbft = new SimpleBeanForTesting(TEST_STRING_VAL);
98                 pageContext.setAttribute(REQUEST_KEY, sbft,PageContext.REQUEST_SCOPE);
99                 runMyTest("testWriteTagNameProperty", "");
100         }
101         public void endWriteTagNameProperty(WebResponse response){
102                 formatAndTest(TEST_STRING_VAL, response.getText());
103         }
104
105         // Name and Format
106
public void testWriteTagNameFormat() throws Exception JavaDoc {
107                 pageContext.setAttribute(REQUEST_KEY,TEST_INTEGER_VAL,PageContext.REQUEST_SCOPE);
108                 runMyTest("testWriteTagNameFormat", "");
109         }
110         public void endWriteTagNameFormat(WebResponse response){
111                 formatAndTest("1,234" , response.getText());
112         }
113
114         // Name, Format, and FormatKey (default bundle)
115
public void testWriteTagNameFormatKeyDefaultBundle() throws Exception JavaDoc {
116                 pageContext.setAttribute(REQUEST_KEY,TEST_INTEGER_VAL,PageContext.REQUEST_SCOPE);
117                 runMyTest("testWriteTagNameFormatKeyDefaultBundle", "");
118         }
119         public void endWriteTagNameFormatKeyDefaultBundle(WebResponse response){
120                 formatAndTest("$1,234" , response.getText());
121         }
122
123         // Name, Format, and FormatKey (alternate bundle)
124
public void testWriteTagNameFormatKeyAlternateBundle() throws Exception JavaDoc {
125                 pageContext.setAttribute(REQUEST_KEY,TEST_INTEGER_VAL,PageContext.REQUEST_SCOPE);
126                 runMyTest("testWriteTagNameFormatKeyAlternateBundle", "");
127         }
128         public void endWriteTagNameFormatKeyAlternateBundle(WebResponse response){
129                 formatAndTest("$1,234" , response.getText());
130         }
131
132         // Name, Format, and FormatKey (default bundle) (Double)
133
public void testWriteTagNameFormatKeyDefaultBundleDouble() throws Exception JavaDoc {
134                 pageContext.setAttribute(REQUEST_KEY, TEST_DOUBLE_VAL,PageContext.REQUEST_SCOPE);
135                 runMyTest("testWriteTagNameFormatKeyDefaultBundleDouble", "");
136         }
137         public void endWriteTagNameFormatKeyDefaultBundleDouble(WebResponse response){
138                 formatAndTest("$1,235" , response.getText());
139         }
140
141         // Name, Format, and FormatKey (alternate bundle) (Double)
142
public void testWriteTagNameFormatKeyAlternateBundleDouble() throws Exception JavaDoc {
143                 pageContext.setAttribute(REQUEST_KEY,TEST_DOUBLE_VAL,PageContext.REQUEST_SCOPE);
144                 runMyTest("testWriteTagNameFormatKeyAlternateBundleDouble", "");
145         }
146         public void endWriteTagNameFormatKeyAlternateBundleDouble(WebResponse response){
147                 formatAndTest("$1,234.6" , response.getText());
148         }
149
150         // Name, Format, and FormatKey (default bundle)
151
public void testWriteTagNameFormatKeyDefaultBundle_fr() throws Exception JavaDoc {
152                 pageContext.setAttribute(REQUEST_KEY, TEST_DOUBLE_VAL,PageContext.REQUEST_SCOPE);
153                 runMyTest("testWriteTagNameFormatKeyDefaultBundle", "fr");
154         }
155         public void endWriteTagNameFormatKeyDefaultBundle_fr(WebResponse response){
156                 formatAndTest("$1234,5961.", response.getText());
157         }
158
159         // Name, Format, and FormatKey (alternate bundle)
160
public void testWriteTagNameFormatKeyAlternateBundle_fr() throws Exception JavaDoc {
161                 pageContext.setAttribute(REQUEST_KEY,TEST_DOUBLE_VAL,PageContext.REQUEST_SCOPE);
162                 runMyTest("testWriteTagNameFormatKeyAlternateBundle", "fr");
163         }
164         public void endWriteTagNameFormatKeyAlternateBundle_fr(WebResponse response){
165                 formatAndTest("$1234,5961." , response.getText());
166         }
167
168         // Name, Property, and Format
169
public void testWriteTagNamePropertyFormat() throws Exception JavaDoc {
170         SimpleBeanForTesting sbft = new SimpleBeanForTesting(TEST_INTEGER_VAL);
171                 pageContext.setAttribute(REQUEST_KEY, sbft,PageContext.REQUEST_SCOPE);
172                 runMyTest("testWriteTagNamePropertyFormat", "");
173         }
174         public void endWriteTagNamePropertyFormat(WebResponse response){
175                 formatAndTest("1,234", response.getText());
176         }
177
178         // Name and ignore
179
public void testWriteTagNameIgnore() throws Exception JavaDoc {
180                 runMyTest("testWriteTagNameIgnore", "");
181         }
182         public void endWriteTagNameIgnore(WebResponse response){
183                 formatAndTest("", response.getText());
184         }
185
186         // Name and filter
187
public void testWriteTagNameFilter() throws Exception JavaDoc {
188                 pageContext.setAttribute(REQUEST_KEY,"<testing&'\">",PageContext.REQUEST_SCOPE);
189                 runMyTest("testWriteTagNameFilter", "");
190         }
191         public void endWriteTagNameFilter(WebResponse response){
192                 formatAndTest(TagUtils.getInstance().filter("<testing&'\">"), response.getText());
193         }
194
195
196 }
197
Popular Tags