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  
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import javax.servlet.jsp.PageContext;
24  import javax.servlet.jsp.tagext.JspTag;
25  
26  import org.jdom.Element;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.seasar.cubby.CubbyConstants;
30  import org.seasar.cubby.action.RequestMethod;
31  import org.seasar.cubby.mock.MockContainerProvider;
32  import org.seasar.cubby.mock.MockPathResolverProvider;
33  import org.seasar.cubby.plugin.PluginRegistry;
34  import org.seasar.cubby.plugins.BinderPlugin;
35  import org.seasar.cubby.routing.PathResolver;
36  import org.seasar.cubby.routing.impl.PathResolverImpl;
37  import org.seasar.cubby.routing.impl.PathTemplateParserImpl;
38  import org.seasar.cubby.spi.ContainerProvider;
39  import org.seasar.cubby.spi.PathResolverProvider;
40  import org.seasar.cubby.spi.container.Container;
41  import org.seasar.cubby.spi.container.LookupException;
42  
43  public class LinkTagTest extends AbstractStandardTagTestCase {
44  
45  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
46  
47  	private LinkTag tag;
48  
49  	@Before
50  	public void setup() throws Exception {
51  		tag = new LinkTag();
52  		setupBodyTag(tag);
53  		setupErrors(context);
54  		context.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "/brabra",
55  				PageContext.REQUEST_SCOPE);
56  	}
57  
58  	@Before
59  	public void setupContainer() {
60  		final BinderPlugin binderPlugin = new BinderPlugin();
61  		binderPlugin.bind(ContainerProvider.class).toInstance(
62  				new MockContainerProvider(new Container() {
63  
64  					public <T> T lookup(Class<T> type) {
65  						throw new LookupException();
66  					}
67  				}));
68  		final PathResolver pathResolver = new PathResolverImpl(
69  				new PathTemplateParserImpl());
70  		pathResolver.add("/mockFormTagTest/foo", MockFormTagTestAction.class,
71  				"foo", RequestMethod.GET, null, 0);
72  		pathResolver.add("/mockFormTagTest/bar/{id}",
73  				MockFormTagTestAction.class, "bar", RequestMethod.GET, null, 0);
74  		binderPlugin.bind(PathResolverProvider.class).toInstance(
75  				new MockPathResolverProvider(pathResolver));
76  		pluginRegistry.register(binderPlugin);
77  	}
78  
79  	@Test
80  	public void doTag() throws Exception {
81  		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
82  		tag.setActionMethod("foo");
83  		doLifecycle(tag);
84  
85  		System.out.println(context.getResult());
86  
87  		assertEquals("URL出力", "/brabra/mockFormTagTest/foo", context
88  				.getResult());
89  	}
90  
91  	@Test
92  	public void doTagWithParam() throws Exception {
93  		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
94  		tag.setActionMethod("bar");
95  		doLifecycle(tag, new ChildrenFactory() {
96  			public List<JspTag> create() {
97  				ParamTag paramTag1 = new ParamTag();
98  				paramTag1.setParent(tag);
99  				paramTag1.setName("id");
100 				paramTag1.setValue("123");
101 				ParamTag paramTag2 = new ParamTag();
102 				paramTag2.setParent(tag);
103 				paramTag2.setName("token");
104 				paramTag2.setValue("abc");
105 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2 });
106 			}
107 		});
108 
109 		System.out.println(context.getResult());
110 
111 		assertEquals("パラメータ付きでURL出力",
112 				"/brabra/mockFormTagTest/bar/123?token=abc", context
113 						.getResult());
114 	}
115 
116 	@Test
117 	public void doTagOutputTag() throws Exception {
118 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
119 		tag.setActionMethod("foo");
120 		tag.setTag("a");
121 		tag.setAttr("href");
122 		jspBody.setBody("body");
123 		doLifecycle(tag);
124 
125 		System.out.println(context.getResult());
126 
127 		Element element = getResultAsElementFromContext();
128 		String message = "タグ出力";
129 		assertEquals(message, "a", element.getName());
130 		assertEquals(message, 1, element.getAttributes().size());
131 		assertEquals(message, "/brabra/mockFormTagTest/foo", element
132 				.getAttributeValue("href"));
133 		assertEquals(message, 0, element.getChildren().size());
134 		assertEquals(message, "body", element.getValue());
135 	}
136 
137 	@Test
138 	public void doTagOutputTagWithParam() throws Exception {
139 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
140 		tag.setActionMethod("bar");
141 		tag.setTag("img");
142 		tag.setAttr("src");
143 		doLifecycle(tag, new ChildrenFactory() {
144 			public List<JspTag> create() {
145 				ParamTag paramTag1 = new ParamTag();
146 				paramTag1.setParent(tag);
147 				paramTag1.setName("id");
148 				paramTag1.setValue("123");
149 				ParamTag paramTag2 = new ParamTag();
150 				paramTag2.setParent(tag);
151 				paramTag2.setName("token");
152 				paramTag2.setValue("abc");
153 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2 });
154 			}
155 		});
156 
157 		System.out.println(context.getResult());
158 
159 		Element element = getResultAsElementFromContext();
160 		String message = "パラメータ付きでタグ出力";
161 		assertEquals(message, "img", element.getName());
162 		assertEquals(message, 1, element.getAttributes().size());
163 		assertEquals(message, "/brabra/mockFormTagTest/bar/123?token=abc",
164 				element.getAttributeValue("src"));
165 		assertEquals(message, 0, element.getChildren().size());
166 	}
167 
168 	public void testDoTagWithProtocol() throws Exception {
169 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
170 		tag.setActionMethod("foo");
171 		tag.setProtocol("https");
172 		doLifecycle(tag);
173 
174 		System.out.println(context.getResult());
175 
176 		assertEquals("URL出力", "https://localhost/brabra/mockFormTagTest/foo",
177 				context.getResult());
178 	}
179 
180 	public void testDoTagWithPort() throws Exception {
181 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
182 		tag.setActionMethod("foo");
183 		tag.setPort(8080);
184 		doLifecycle(tag);
185 
186 		System.out.println(context.getResult());
187 
188 		assertEquals("URL出力",
189 				"http://localhost:8080/brabra/mockFormTagTest/foo", context
190 						.getResult());
191 	}
192 
193 	public void testDoTagWithProtocolAndPort() throws Exception {
194 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
195 		tag.setActionMethod("foo");
196 		tag.setProtocol("https");
197 		tag.setPort(8080);
198 		doLifecycle(tag);
199 
200 		System.out.println(context.getResult());
201 
202 		assertEquals("URL出力",
203 				"https://localhost:8080/brabra/mockFormTagTest/foo", context
204 						.getResult());
205 	}
206 }