1   /*
2    * Copyright 2004-2008 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 java.util.Arrays;
19  import java.util.List;
20  
21  import javax.servlet.jsp.PageContext;
22  import javax.servlet.jsp.tagext.JspTag;
23  
24  import org.jdom.Element;
25  import org.seasar.cubby.CubbyConstants;
26  
27  public class LinkTagTest extends AbstractStandardTagTestCase {
28  
29  	private LinkTag tag;
30  
31  	@Override
32  	protected void setUp() throws Exception {
33  		include(getClass().getName().replace('.', '/') + ".dicon");
34  		super.setUp();
35  		tag = new LinkTag();
36  		setupBodyTag(tag);
37  		setupErrors(context);
38  		context.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "/brabra",
39  				PageContext.REQUEST_SCOPE);
40  	}
41  
42  	public void testDoTag() throws Exception {
43  		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
44  		tag.setActionMethod("foo");
45  		doLifecycle(tag);
46  
47  		System.out.println(context.getResult());
48  
49  		assertEquals("URL出力", "/brabra/mockFormTagTest/foo", context
50  				.getResult());
51  	}
52  
53  	public void testDoTagWithParam() throws Exception {
54  		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
55  		tag.setActionMethod("bar");
56  		doLifecycle(tag, new ChildrenFactory() {
57  			public List<JspTag> create() {
58  				ParamTag paramTag1 = new ParamTag();
59  				paramTag1.setParent(tag);
60  				paramTag1.setName("id");
61  				paramTag1.setValue("123");
62  				ParamTag paramTag2 = new ParamTag();
63  				paramTag2.setParent(tag);
64  				paramTag2.setName("token");
65  				paramTag2.setValue("abc");
66  				return Arrays.asList(new JspTag[] { paramTag1, paramTag2 });
67  			}
68  		});
69  
70  		System.out.println(context.getResult());
71  
72  		assertEquals("パラメータ付きでURL出力",
73  				"/brabra/mockFormTagTest/bar/123?token=abc", context
74  						.getResult());
75  	}
76  
77  	public void testDoTagOutputTag() throws Exception {
78  		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
79  		tag.setActionMethod("foo");
80  		tag.setTag("a");
81  		tag.setAttr("href");
82  		jspBody.setBody("body");
83  		doLifecycle(tag);
84  
85  		System.out.println(context.getResult());
86  
87  		Element element = getResultAsElementFromContext();
88  		String message = "タグ出力";
89  		assertEquals(message, "a", element.getName());
90  		assertEquals(message, 1, element.getAttributes().size());
91  		assertEquals(message, "/brabra/mockFormTagTest/foo", element
92  				.getAttributeValue("href"));
93  		assertEquals(message, 0, element.getChildren().size());
94  		assertEquals(message, "body", element.getValue());
95  	}
96  
97  	public void testDoTagOutputTagWithParam() throws Exception {
98  		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
99  		tag.setActionMethod("bar");
100 		tag.setTag("img");
101 		tag.setAttr("src");
102 		doLifecycle(tag, new ChildrenFactory() {
103 			public List<JspTag> create() {
104 				ParamTag paramTag1 = new ParamTag();
105 				paramTag1.setParent(tag);
106 				paramTag1.setName("id");
107 				paramTag1.setValue("123");
108 				ParamTag paramTag2 = new ParamTag();
109 				paramTag2.setParent(tag);
110 				paramTag2.setName("token");
111 				paramTag2.setValue("abc");
112 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2 });
113 			}
114 		});
115 
116 		System.out.println(context.getResult());
117 
118 		Element element = getResultAsElementFromContext();
119 		String message = "パラメータ付きでタグ出力";
120 		assertEquals(message, "img", element.getName());
121 		assertEquals(message, 1, element.getAttributes().size());
122 		assertEquals(message, "/brabra/mockFormTagTest/bar/123?token=abc",
123 				element.getAttributeValue("src"));
124 		assertEquals(message, 0, element.getChildren().size());
125 	}
126 
127 }