1 5 package com.opensymphony.webwork.views.jsp; 6 7 import com.opensymphony.webwork.TestAction; 8 import com.opensymphony.webwork.ServletActionContext; 9 import com.opensymphony.webwork.views.jsp.AbstractTagTest; 10 import com.opensymphony.webwork.views.jsp.TextTag; 11 import com.opensymphony.webwork.views.jsp.ui.TestAction1; 12 import com.opensymphony.webwork.views.jsp.ui.WebWorkBodyContent; 13 import com.opensymphony.xwork.Action; 14 import com.opensymphony.xwork.ActionContext; 15 import com.opensymphony.xwork.util.OgnlValueStack; 16 17 import javax.servlet.jsp.JspException ; 18 import javax.servlet.jsp.tagext.Tag ; 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.List ; 23 import java.util.Locale ; 24 25 26 31 public class TextTagTest extends AbstractTagTest { 32 34 private String fooValue = "com.opensymphony.webwork.views.jsp.TextTagTest.fooValue"; 35 private TextTag tag; 36 37 39 public Action getAction() { 40 TestAction action = new TestAction(); 41 action.setFoo(fooValue); 42 43 return action; 44 } 45 46 public void testExpressionsEvaluated() throws Exception { 47 String key = "expressionKey"; 48 String value = "Foo is " + fooValue; 49 tag.setName(key); 50 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 51 assertEquals(value, writer.toString()); 52 } 53 54 public void testMessageFormatWorks() throws Exception { 55 String key = "messageFormatKey"; 56 String pattern = "Params are {0} {1} {2}"; 57 Object param1 = new Integer (12); 58 Object param2 = new Date (); 59 Object param3 = "StringVal"; 60 List params = new ArrayList (); 61 params.add(param1); 62 params.add(param2); 63 params.add(param3); 64 65 String expected = MessageFormat.format(pattern, params.toArray()); 66 tag.setName(key); 67 tag.addParameter(param1); 68 tag.addParameter(param2); 69 tag.addParameter(param3); 70 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 71 assertEquals(expected, writer.toString()); 72 } 73 74 public void testSimpleKeyValueWorks() throws JspException { 75 String key = "simpleKey"; 76 String value = "Simple Message"; 77 tag.setName(key); 78 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 79 assertEquals(value, writer.toString()); 80 } 81 82 public void testTextTagUsesValueStackInRequestNotActionContext() throws JspException { 83 String key = "simpleKey"; 84 String value1 = "Simple Message"; 85 String value2="This is TestBean1"; 86 tag.setName(key); 87 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 88 assertEquals(value1, writer.toString()); 89 final StringBuffer buffer = writer.getBuffer(); 90 buffer.delete(0,buffer.length()); 91 OgnlValueStack newStack = new OgnlValueStack(); 92 newStack.getContext().put(ActionContext.LOCALE,Locale.US); 93 newStack.push(new TestAction1()); 94 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, newStack); 95 assertNotSame(ActionContext.getContext().getValueStack().peek(),newStack.peek()); 96 97 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 98 assertEquals(value2, writer.toString()); 99 } 100 101 public void testTextTagUsesLocaleFromValueStack() throws JspException { 102 stack.pop(); 103 stack.push(new TestAction1()); 104 ActionContext.getContext().setLocale(Locale.US); 105 String key = "simpleKey"; 106 String value_en="This is TestBean1"; 107 tag.setName(key); 108 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 109 assertEquals(value_en, writer.toString()); 110 final StringBuffer buffer = writer.getBuffer(); 111 buffer.delete(0,buffer.length()); 112 String value_de="This is TestBean1 in German"; 113 OgnlValueStack newStack = new OgnlValueStack(stack); 114 newStack.getContext().put(ActionContext.LOCALE,Locale.GERMANY); 115 assertNotSame(newStack.getContext().get(ActionContext.LOCALE),ActionContext.getContext().getLocale()); 116 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, newStack); 117 assertEquals(ActionContext.getContext().getValueStack().peek(),newStack.peek()); 118 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 119 assertEquals(value_de, writer.toString()); 120 } 121 122 public void testWithNoMessageAndBodyIsNotEmptyBodyIsReturned() throws Exception { 123 final String key = "key.does.not.exist"; 124 final String bodyText = "body text"; 125 tag.setName(key); 126 127 WebWorkBodyContent bodyContent = new WebWorkBodyContent(null); 128 bodyContent.print(bodyText); 129 tag.setBodyContent(bodyContent); 130 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 131 assertEquals(bodyText, writer.toString()); 132 } 133 134 public void testWithNoMessageAndNoDefaultKeyReturned() throws JspException { 135 final String key = "key.does.not.exist"; 136 tag.setName("'" + key + "'"); 137 assertEquals(Tag.EVAL_PAGE, tag.doEndTag()); 138 assertEquals(key, writer.toString()); 139 } 140 141 146 protected void setUp() throws Exception { 147 super.setUp(); 148 tag = new TextTag(); 149 tag.setPageContext(pageContext); 150 ActionContext.setContext(new ActionContext(stack.getContext())); 151 } 152 153 protected void tearDown() throws Exception { 154 OgnlValueStack valueStack = new OgnlValueStack(); 155 ActionContext.setContext(new ActionContext(valueStack.getContext())); 156 } 157 } 158 | Popular Tags |