KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > ParamTagUnnamedTest


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import javax.servlet.jsp.JspException JavaDoc;
8 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
9 import javax.servlet.jsp.tagext.Tag JavaDoc;
10 import java.io.StringWriter JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14
15
16 /**
17  * ParamTagTest
18  *
19  * @author Jason Carreira
20  * Date: Mar 15, 2004 9:48:06 PM
21  */

22 public class ParamTagUnnamedTest extends AbstractTagTest {
23     //~ Static fields/initializers /////////////////////////////////////////////
24

25     private static final String JavaDoc NAME_EXP = "'name'";
26     private static final String JavaDoc NAME_VAL = "name";
27     private static final String JavaDoc VAL_EXP = "'value'";
28     private static final String JavaDoc VAL_VAL = "value";
29
30     //~ Instance fields ////////////////////////////////////////////////////////
31

32     private ParamTag tag;
33     private UnnamedParametricBodyTag parentTag;
34
35     //~ Methods ////////////////////////////////////////////////////////////////
36

37     public void testParamSetWithBodyContent() throws JspException JavaDoc {
38         StringWriter JavaDoc writer = new StringWriter JavaDoc();
39         WebWorkMockBodyContent webWorkMockBodyContent = new WebWorkMockBodyContent(new WebWorkMockJspWriter(writer));
40         webWorkMockBodyContent.setString(VAL_VAL);
41         tag.setBodyContent(webWorkMockBodyContent);
42         assertEquals(BodyTag.EVAL_BODY_BUFFERED, tag.doStartTag());
43         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
44
45         List JavaDoc params = parentTag.getValuesList();
46         assertEquals(1, params.size());
47         assertEquals(VAL_VAL, params.get(0));
48     }
49
50     public void testParamSetWithValueAttribute() throws JspException JavaDoc {
51         tag.setValue(VAL_EXP);
52         assertEquals(BodyTag.EVAL_BODY_BUFFERED, tag.doStartTag());
53         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
54
55         List JavaDoc params = parentTag.getValuesList();
56         assertEquals(1, params.size());
57         assertEquals(VAL_VAL, params.get(0));
58     }
59
60     public void testParamSetWithValueAttributeWhenBothValueAttrAndBodyAreSet() throws JspException JavaDoc {
61         StringWriter JavaDoc writer = new StringWriter JavaDoc();
62         tag.setValue(VAL_EXP);
63
64         WebWorkMockBodyContent webWorkMockBodyContent = new WebWorkMockBodyContent(new WebWorkMockJspWriter(writer));
65         webWorkMockBodyContent.setString("Will not be used");
66         tag.setBodyContent(webWorkMockBodyContent);
67         assertEquals(BodyTag.EVAL_BODY_BUFFERED, tag.doStartTag());
68         assertEquals(Tag.EVAL_PAGE, tag.doEndTag());
69
70         List JavaDoc params = parentTag.getValuesList();
71         assertEquals(1, params.size());
72         assertEquals(VAL_VAL, params.get(0));
73     }
74
75     protected void setUp() throws Exception JavaDoc {
76         super.setUp();
77         tag = new ParamTag();
78         tag.setPageContext(pageContext);
79         parentTag = new UnnamedParametricBodyTag();
80         tag.setParent(parentTag);
81     }
82
83     //~ Inner Classes //////////////////////////////////////////////////////////
84

85     private class UnnamedParametricBodyTag extends WebWorkBodyTagSupport implements ParamTag.UnnamedParametric {
86         private List JavaDoc values;
87
88         public Map JavaDoc getParameters() {
89             return null;
90         }
91
92         public List JavaDoc getValuesList() {
93             return values;
94         }
95
96         public void addParameter(String JavaDoc aName, Object JavaDoc aValue) {
97             addParameter(aValue);
98         }
99
100         public void addParameter(Object JavaDoc aValue) {
101             if (aValue == null) {
102                 return;
103             }
104
105             if (values == null) {
106                 values = new ArrayList JavaDoc();
107             }
108
109             values.add(aValue);
110         }
111     }
112 }
113
Popular Tags