1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.tags;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.SimpleTagSupport;
29  
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  public class ParamTagTest extends SimpleTagTestCase {
34  
35  	private ParamTag tag;
36  
37  	@Before
38  	public void setup() throws Exception {
39  		tag = new ParamTag();
40  		setupSimpleTag(tag);
41  		setupErrors(context);
42  	}
43  
44  	@Test
45  	public void doTag1() throws JspException, IOException {
46  		final MockParentTag parent = new MockParentTag();
47  		tag.setParent(parent);
48  		tag.setName("paramname");
49  		tag.setValue("paramvalue");
50  		tag.doTag();
51  		final Map<String, String> parameters = parent.getParameters();
52  		assertEquals(1, parameters.size());
53  		final String value = parameters.get("paramname");
54  		assertNotNull(value);
55  		assertEquals("paramvalue", value);
56  	}
57  
58  	@Test
59  	public void doTag2() throws JspException, IOException {
60  		final MockParentTag parent = new MockParentTag();
61  		tag.setParent(parent);
62  		tag.setName("paramname");
63  		MockJspFragment body = new MockJspFragment();
64  		body.setBody("bodyvalue");
65  		tag.setJspBody(body);
66  		tag.doTag();
67  		final Map<String, String> parameters = parent.getParameters();
68  		assertEquals(1, parameters.size());
69  		final String value = parameters.get("paramname");
70  		assertNotNull(value);
71  		assertEquals("bodyvalue", value);
72  	}
73  
74  	@Test
75  	public void doTagHasIllegalParent() throws JspException, IOException {
76  		final InputTag parent = new InputTag();
77  		assertFalse(parent instanceof ParamParent);
78  		tag.setParent(parent);
79  		tag.setName("paramname");
80  		tag.setValue("paramvalue");
81  		try {
82  			tag.doTag();
83  			fail();
84  		} catch (final JspException e) {
85  			// ok
86  			e.printStackTrace();
87  		}
88  	}
89  
90  	@Test
91  	public void doTagHasNoParent() throws JspException, IOException {
92  		tag.setName("paramname");
93  		tag.setValue("paramvalue");
94  		try {
95  			tag.doTag();
96  			fail();
97  		} catch (final JspException e) {
98  			// ok
99  			e.printStackTrace();
100 		}
101 	}
102 
103 	private static class MockParentTag extends SimpleTagSupport implements ParamParent {
104 
105 		private final Map<String, String> parameters = new HashMap<String, String>();
106 
107 		public void addParameter(final String name, final String value) {
108 			parameters.put(name, value);
109 		}
110 
111 		public Map<String, String> getParameters() {
112 			return parameters;
113 		}
114 
115 	}
116 
117 }