KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > test > ATagTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.html.test;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12
13 import org.jdom.Document;
14 import org.jdom.Element;
15
16 import com.inversoft.config.ConfigurationException;
17 import com.inversoft.junit.JspTestCase;
18 import com.inversoft.junit.Request;
19 import com.inversoft.junit.URL;
20 import com.inversoft.junit.internal.http.MockHttpServletRequest;
21 import com.inversoft.verge.mvc.view.jsp.html.ATag;
22 import com.inversoft.verge.util.url.config.URLConfigBuilder;
23 import com.inversoft.verge.util.url.config.URLConfigRegistry;
24
25
26 /**
27  * <p>
28  * This class has the test cases for the a tag
29  * </p>
30  *
31  * @author Brian Pontarelli
32  * @since 2.0
33  * @version 2.0
34  */

35 public class ATagTest extends JspTestCase {
36
37     static {
38         URLConfigBuilder builder = new URLConfigBuilder();
39         Document doc = createGoodDocument();
40
41         try {
42             builder.build(doc,
43                 URLConfigRegistry.getInstance(new MockHttpServletRequest(null)));
44         } catch (ConfigurationException ce) {
45             fail(ce.toString());
46         }
47     }
48
49     /**
50      * Constructor for ATagTest.
51      * @param name
52      */

53     public ATagTest(String JavaDoc name) {
54         super(name);
55         setLocal(true);
56     }
57
58
59     /**
60      * Tests the id attribute
61      */

62     public void testId() {
63         ATag tag = new ATag();
64         tag.setPageContext(pageContext);
65         tag.setId("test");
66         tag.setHref("test");
67         tag.setBodyContent(createBodyContent("link"));
68
69         try {
70             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
71             String JavaDoc tagStr = getPageContext().getMockOut().getText();
72
73             System.out.println("A tag: " + tagStr);
74             assertEquals("Should have tag", tagStr,
75                 "<a id=\"test\" HREF=\"test\">link</a>");
76         } catch (JspException JavaDoc e) {
77             fail(e.toString());
78         }
79     }
80
81     /**
82      * Test that the anchor tag uses the name. These names are used for anchors
83      * inside a page as well as for javascript.
84      */

85     public void testWithoutContext() {
86         ATag tag = new ATag();
87         tag.setPageContext(pageContext);
88         tag.setName("testName");
89         tag.setHref("some.jsp");
90         tag.setBodyContent(createBodyContent());
91
92         try {
93             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
94             String JavaDoc tagStr = getPageContext().getMockOut().getText();
95             String JavaDoc expected = "<a name=\"testName\" HREF=\"some.jsp\"></a>";
96
97             System.out.println("Expecgted: " + expected);
98             System.out.println("a tag: " + tagStr);
99             assertEquals(expected, tagStr);
100         } catch (JspException JavaDoc e) {
101             fail(e.toString());
102         }
103     }
104
105     /**
106      * Sets up a context path
107      */

108     public void beginContext(Request request) {
109         URL url = new URL("context", null, "http", null, "test", "test");
110         request.setURL(url);
111     }
112
113     /**
114      * Test that the image works with the context path
115      */

116     public void testContext() {
117         ATag tag = new ATag();
118         tag.setPageContext(pageContext);
119         tag.setHref("/some.jsp");
120         tag.setBodyContent(createBodyContent("link"));
121
122         try {
123             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
124             String JavaDoc tagStr = getPageContext().getMockOut().getText();
125             String JavaDoc expected = "<a HREF=\"/context/some.jsp\">link</a>";
126
127             System.out.println("expected: " + tagStr);
128             System.out.println("a tag: " + tagStr);
129             assertEquals(expected, tagStr);
130         } catch (JspException JavaDoc e) {
131             fail(e.toString());
132         }
133     }
134
135     /**
136      * Sets up a context path
137      */

138     public void beginNoContext(Request request) {
139         URL url = new URL("context", null, "http", null, "test", "test");
140         request.setURL(url);
141     }
142
143     /**
144      * Test that the image works with the context path
145      */

146     public void testNoContext() {
147         ATag tag = new ATag();
148         tag.setPageContext(pageContext);
149         tag.setHref("/some.jsp");
150         tag.setContext(Boolean.FALSE);
151         tag.setBodyContent(createBodyContent());
152
153         try {
154             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
155             String JavaDoc tagStr = getPageContext().getMockOut().getText();
156
157             System.out.println("a tag: " + tagStr);
158             assertEquals("Should have tag", tagStr, "<a HREF=\"/some.jsp\"></a>");
159         } catch (JspException JavaDoc e) {
160             fail(e.toString());
161         }
162     }
163
164     /**
165      * Test that the URLGenerator system works with the a tag
166      */

167     public void testURLGenerator() {
168         ATag tag = new ATag();
169         tag.setPageContext(pageContext);
170         tag.setHref("/some.jsp");
171         tag.setCategory("cat1");
172         tag.setBodyContent(createBodyContent());
173
174         try {
175             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
176             String JavaDoc tagStr = getPageContext().getMockOut().getText();
177             String JavaDoc expected = "<a HREF=\"https://www.inversoft.com:8000/MyWebApp/some.jsp\"></a>";
178
179             System.out.println("a tag: " + tagStr);
180             System.out.println("expected: " + expected);
181             assertEquals("Should have tag", tagStr, expected);
182         } catch (JspException JavaDoc e) {
183             fail(e.toString());
184         }
185     }
186
187     /**
188      * Tests the parameters map
189      */

190     public void testParameters() {
191         ATag tag = new ATag();
192         tag.setPageContext(pageContext);
193         tag.setId("test");
194         tag.setHref("test");
195         tag.getParameterMap().put("param1", "value1");
196         tag.setBodyContent(createBodyContent());
197
198         try {
199             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
200
201             String JavaDoc tagStr = getPageContext().getMockOut().getText();
202             String JavaDoc expected = "<a id=\"test\" HREF=\"test?param1=value1\"></a>";
203
204             System.out.println("Expected: " + expected);
205             System.out.println("A tag: " + tagStr);
206             assertEquals(expected, tagStr);
207         } catch (JspException JavaDoc e) {
208             fail(e.toString());
209         }
210
211         getPageContext().clearMockOut();
212         tag.getParameterMap().put("param1", "value1");
213         tag.getParameterMap().put("param2", "value2");
214         try {
215             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
216
217             String JavaDoc tagStr = getPageContext().getMockOut().getText();
218             String JavaDoc expected = "<a id=\"test\" HREF=\"test?param1=value1&param2=value2\"></a>";
219
220             System.out.println("Expected: " + expected);
221             System.out.println("A tag: " + tagStr);
222             assertTrue(tagStr.startsWith("<a id=\"test\" HREF=\"test?"));
223             assertTrue(tagStr.indexOf("param1=value1") != -1);
224             assertTrue(tagStr.indexOf("param2=value2") != -1);
225             assertTrue(tagStr.indexOf("&") != -1);
226         } catch (JspException JavaDoc e) {
227             fail(e.toString());
228         }
229
230         getPageContext().clearMockOut();
231         tag.getParameterMap().clear();
232         tag.getParameterMap().put("param1", "value1");
233         tag.setHref("test;JSESSIONID=2?pre1=pre1");
234         try {
235             assertEquals("Should return EVAL_PAGE", runTag(tag), Tag.EVAL_PAGE);
236
237             String JavaDoc tagStr = getPageContext().getMockOut().getText();
238             String JavaDoc expected = "<a id=\"test\" HREF=\"test;JSESSIONID=2?pre1=pre1&param1=value1\"></a>";
239
240             System.out.println("Expected: " + expected);
241             System.out.println("A tag: " + tagStr);
242             assertEquals(expected, tagStr);
243         } catch (JspException JavaDoc e) {
244             fail(e.toString());
245         }
246     }
247
248     public static Document createGoodDocument() {
249         Element root = new Element("url");
250         Document doc = new Document(root);
251
252         Element cat = new Element("category");
253         cat.setAttribute("name", "cat1");
254         Element protocol = new Element("protocol");
255         protocol.addContent("https");
256         Element host = new Element("host");
257         host.addContent("www.inversoft.com");
258         Element port = new Element("port");
259         port.addContent("8000");
260         Element context = new Element("context");
261         context.addContent("MyWebApp");
262         cat.addContent(protocol);
263         cat.addContent(host);
264         cat.addContent(port);
265         cat.addContent(context);
266         root.addContent(cat);
267
268         /*
269         cat = new Element("category");
270         cat.setAttribute("name", "view");
271         protocol = new Element("protocol");
272         protocol.addContent("http");
273         host = new Element("host");
274         host.addContent("www.yahoo.com");
275         port = new Element("port");
276         port.addContent("8000");
277         context = new Element("context");
278         context.addContent("YahooContext");
279         cat.addContent(protocol);
280         cat.addContent(host);
281         cat.addContent(port);
282         cat.addContent(context);
283         root.addContent(cat);
284         */

285
286         return doc;
287     }
288 }
Popular Tags