1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.tags;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.fail;
23  
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.jsp.JspException;
29  import javax.servlet.jsp.tagext.SimpleTagSupport;
30  
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  public class ParamTagTest extends SimpleTagTestCase {
35  
36  	private ParamTag tag;
37  
38  	@Before
39  	public void setup() throws Exception {
40  		tag = new ParamTag();
41  		setupSimpleTag(tag);
42  		setupErrors(context);
43  	}
44  
45  	@Test
46  	public void doTag1() throws JspException, IOException {
47  		final MockParentTag parent = new MockParentTag();
48  		tag.setParent(parent);
49  		tag.setName("paramname");
50  		tag.setValue("paramvalue");
51  		tag.doTag();
52  		final Map<String, String> parameters = parent.getParameters();
53  		assertEquals(1, parameters.size());
54  		final String value = parameters.get("paramname");
55  		assertNotNull(value);
56  		assertEquals("paramvalue", value);
57  	}
58  
59  	@Test
60  	public void doTag2() throws JspException, IOException {
61  		final MockParentTag parent = new MockParentTag();
62  		tag.setParent(parent);
63  		tag.setName("paramname");
64  		MockJspFragment body = new MockJspFragment();
65  		body.setBody("bodyvalue");
66  		tag.setJspBody(body);
67  		tag.doTag();
68  		final Map<String, String> parameters = parent.getParameters();
69  		assertEquals(1, parameters.size());
70  		final String value = parameters.get("paramname");
71  		assertNotNull(value);
72  		assertEquals("bodyvalue", value);
73  	}
74  
75  	@Test
76  	public void doTagHasIllegalParent() throws JspException, IOException {
77  		final InputTag parent = new InputTag();
78  		assertFalse(parent instanceof ParamParent);
79  		tag.setParent(parent);
80  		tag.setName("paramname");
81  		tag.setValue("paramvalue");
82  		try {
83  			tag.doTag();
84  			fail();
85  		} catch (final JspException e) {
86  			// ok
87  			e.printStackTrace();
88  		}
89  	}
90  
91  	@Test
92  	public void doTagHasNoParent() throws JspException, IOException {
93  		tag.setName("paramname");
94  		tag.setValue("paramvalue");
95  		try {
96  			tag.doTag();
97  			fail();
98  		} catch (final JspException e) {
99  			// ok
100 			e.printStackTrace();
101 		}
102 	}
103 
104 	private static class MockParentTag extends SimpleTagSupport implements ParamParent {
105 
106 		private final Map<String, String> parameters = new HashMap<String, String>();
107 
108 		public void addParameter(final String name, final String value) {
109 			parameters.put(name, value);
110 		}
111 
112 		public Map<String, String> getParameters() {
113 			return parameters;
114 		}
115 
116 	}
117 
118 }