1 package org.seasar.cubby.tags;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.servlet.jsp.JspException;
8 import javax.servlet.jsp.tagext.SimpleTagSupport;
9
10 public class ParamTagTest extends SimpleTagTestCase {
11
12 private ParamTag tag;
13
14 @Override
15 protected void setUp() throws Exception {
16 super.setUp();
17 tag = new ParamTag();
18 setupSimpleTag(tag);
19 setupErrors(context);
20 }
21
22 public void testDoTag1() throws JspException, IOException {
23 final MockParentTag parent = new MockParentTag();
24 tag.setParent(parent);
25 tag.setName("paramname");
26 tag.setValue("paramvalue");
27 tag.doTag();
28 final Map<String, String> parameters = parent.getParameters();
29 assertEquals(1, parameters.size());
30 final String value = parameters.get("paramname");
31 assertNotNull(value);
32 assertEquals("paramvalue", value);
33 }
34
35 public void testDoTag2() throws JspException, IOException {
36 final MockParentTag parent = new MockParentTag();
37 tag.setParent(parent);
38 tag.setName("paramname");
39 MockJspFragment body = new MockJspFragment();
40 body.setBody("bodyvalue");
41 tag.setJspBody(body);
42 tag.doTag();
43 final Map<String, String> parameters = parent.getParameters();
44 assertEquals(1, parameters.size());
45 final String value = parameters.get("paramname");
46 assertNotNull(value);
47 assertEquals("bodyvalue", value);
48 }
49
50 public void testDoTagHasIllegalParent() throws JspException, IOException {
51 final InputTag parent = new InputTag();
52 assertFalse(parent instanceof ParamParent);
53 tag.setParent(parent);
54 tag.setName("paramname");
55 tag.setValue("paramvalue");
56 try {
57 tag.doTag();
58 fail();
59 } catch (final JspException e) {
60
61 e.printStackTrace();
62 }
63 }
64
65 public void testDoTagHasNoParent() throws JspException, IOException {
66 tag.setName("paramname");
67 tag.setValue("paramvalue");
68 try {
69 tag.doTag();
70 fail();
71 } catch (final JspException e) {
72
73 e.printStackTrace();
74 }
75 }
76
77 private class MockParentTag extends SimpleTagSupport implements
78 ParamParent {
79
80 private final Map<String, String> parameters = new HashMap<String, String>();
81
82 public void addParameter(final String name, final String value) {
83 parameters.put(name, value);
84 }
85
86 public Map<String, String> getParameters() {
87 return parameters;
88 }
89
90 }
91
92 }