1 18 package org.apache.struts.taglib.bean; 19 20 import java.util.Locale ; 21 22 import javax.servlet.jsp.PageContext ; 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 38 public class TestWriteTag extends TaglibTestBase { 39 protected final static String TEST_STRING_VAL = "Test Value"; 40 protected final static Integer TEST_INTEGER_VAL = new Integer ("1234"); 41 protected final static Double TEST_DOUBLE_VAL = new Double ("1234.5961"); 42 protected final static String REQUEST_KEY = "REQUEST_KEY"; 43 44 49 public TestWriteTag(String theName) { 50 super(theName); 51 } 52 53 58 public static void main(String [] theArgs) { 59 junit.awtui.TestRunner.main(new String [] {TestWriteTag.class.getName()}); 60 } 61 62 66 public static Test suite() { 67 return new TestSuite(TestWriteTag.class); 69 } 70 71 private void formatAndTest(String compare, String output) { 72 output = replace(output,"\r",""); 74 output = replace(output,"\n",""); 75 output = output.trim(); 76 assertEquals(compare, output); 78 } 79 80 private void runMyTest(String whichTest, String locale) throws Exception { 81 pageContext.setAttribute(Globals.LOCALE_KEY, new Locale (locale, locale), PageContext.SESSION_SCOPE); 82 request.setAttribute("runTest", whichTest); 83 pageContext.forward("/test/org/apache/struts/taglib/bean/TestWriteTag.jsp"); 84 } 85 86 public void testWriteTagName() throws Exception { 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 public void testWriteTagNameProperty() throws Exception { 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 public void testWriteTagNameFormat() throws Exception { 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 public void testWriteTagNameFormatKeyDefaultBundle() throws Exception { 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 public void testWriteTagNameFormatKeyAlternateBundle() throws Exception { 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 public void testWriteTagNameFormatKeyDefaultBundleDouble() throws Exception { 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 public void testWriteTagNameFormatKeyAlternateBundleDouble() throws Exception { 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 public void testWriteTagNameFormatKeyDefaultBundle_fr() throws Exception { 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 public void testWriteTagNameFormatKeyAlternateBundle_fr() throws Exception { 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 public void testWriteTagNamePropertyFormat() throws Exception { 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 public void testWriteTagNameIgnore() throws Exception { 180 runMyTest("testWriteTagNameIgnore", ""); 181 } 182 public void endWriteTagNameIgnore(WebResponse response){ 183 formatAndTest("", response.getText()); 184 } 185 186 public void testWriteTagNameFilter() throws Exception { 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 |