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