KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > testcase > servlets > TestJspTaglibs


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.testcase.servlets;
54
55 import java.io.BufferedReader JavaDoc;
56 import java.io.File JavaDoc;
57 import java.io.FileReader JavaDoc;
58 import java.io.InputStream JavaDoc;
59 import java.io.PrintWriter JavaDoc;
60 import java.io.Reader JavaDoc;
61 import java.io.StringReader JavaDoc;
62 import java.io.StringWriter JavaDoc;
63 import java.net.URL JavaDoc;
64 import java.security.Principal JavaDoc;
65 import java.util.Collections JavaDoc;
66 import java.util.Enumeration JavaDoc;
67 import java.util.Hashtable JavaDoc;
68 import java.util.Locale JavaDoc;
69 import java.util.Map JavaDoc;
70 import java.util.Properties JavaDoc;
71 import java.util.Set JavaDoc;
72 import java.net.URL JavaDoc;
73
74 import javax.servlet.RequestDispatcher JavaDoc;
75 import javax.servlet.Servlet JavaDoc;
76 import javax.servlet.ServletConfig JavaDoc;
77 import javax.servlet.ServletContext JavaDoc;
78 import javax.servlet.ServletInputStream JavaDoc;
79 import javax.servlet.ServletOutputStream JavaDoc;
80 import javax.servlet.http.Cookie JavaDoc;
81 import javax.servlet.http.HttpServletRequest JavaDoc;
82 import javax.servlet.http.HttpServletResponse JavaDoc;
83 import javax.servlet.http.HttpSession JavaDoc;
84 import javax.servlet.http.HttpSessionContext JavaDoc;
85
86 import freemarker.ext.servlet.FreemarkerServlet;
87 import freemarker.template.TemplateException;
88 import freemarker.testcase.TemplateTestCase;
89 import junit.framework.TestCase;
90
91 /**
92  * @version $Id: TestJspTaglibs.java,v 1.13 2005/06/12 00:44:39 revusky Exp $
93  * @author Attila Szegedi
94  */

95 public class TestJspTaglibs extends TestCase {
96     
97     File JavaDoc refFile;
98     
99     public TestJspTaglibs(String JavaDoc name) {
100         super(name);
101     }
102     
103     public TestJspTaglibs(String JavaDoc name, String JavaDoc filename) {
104         super(name);
105     }
106
107     public void setUp() throws Exception JavaDoc {
108         URL JavaDoc url = TestJspTaglibs.class.getResource("TestJspTaglibs.class");
109         File JavaDoc thisDir = new File JavaDoc(url.getFile()).getParentFile();
110         refFile = new File JavaDoc(thisDir, "reference/test-jsptaglibs.txt");
111     }
112
113     public void runTest() throws TemplateException {
114         try {
115             ServletConfig JavaDoc cfg = new MockServletConfig();
116             FreemarkerServlet servlet = new FreemarkerServlet();
117             servlet.init(cfg);
118             MockRequest req = new MockRequest("test-jsptaglibs.txt");
119             MockResponse resp = new MockResponse();
120             servlet.doGet(req, resp);
121             StringReader JavaDoc output = new StringReader JavaDoc(resp.toString());
122             Reader JavaDoc reference = new FileReader JavaDoc(refFile);
123             TemplateTestCase.compare(reference,output);
124 // showTestResults( referenceText, resp.toString() );
125
}
126         catch(Exception JavaDoc e) {
127             e.printStackTrace();
128             throw new TemplateException(e, null);
129         }
130     }
131
132     private static class MockServletConfig
133         implements ServletConfig JavaDoc, ServletContext JavaDoc {
134         private final Properties JavaDoc initParams = new Properties JavaDoc();
135         private final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
136
137         MockServletConfig() {
138             initParams.setProperty("TemplatePath", "/template/");
139             initParams.setProperty("NoCache", "true");
140             initParams.setProperty("TemplateUpdateInterval", "0");
141             initParams.setProperty("DefaultEncoding", "UTF-8");
142             initParams.setProperty("ObjectWrapper", "beans");
143         }
144
145         public String JavaDoc getInitParameter(String JavaDoc name) {
146             return initParams.getProperty(name);
147         }
148
149         public Enumeration JavaDoc getInitParameterNames() {
150             return initParams.keys();
151         }
152
153         public ServletContext JavaDoc getServletContext() {
154             return this;
155         }
156
157         public String JavaDoc getServletName() {
158             return "freemarker";
159         }
160
161         public Object JavaDoc getAttribute(String JavaDoc arg0) {
162             return attributes.get(arg0);
163         }
164
165         public Enumeration JavaDoc getAttributeNames() {
166             return attributes.keys();
167         }
168
169         public ServletContext JavaDoc getContext(String JavaDoc arg0) {
170             throw new UnsupportedOperationException JavaDoc();
171         }
172
173         public int getMajorVersion() {
174             return 0;
175         }
176
177         public String JavaDoc getMimeType(String JavaDoc arg0) {
178             throw new UnsupportedOperationException JavaDoc();
179         }
180
181         public int getMinorVersion() {
182             return 0;
183         }
184
185         public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc arg0) {
186             throw new UnsupportedOperationException JavaDoc();
187         }
188
189         public String JavaDoc getRealPath(String JavaDoc arg0) {
190             return null;
191         }
192
193         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc arg0) {
194             throw new UnsupportedOperationException JavaDoc();
195         }
196
197         public URL JavaDoc getResource(String JavaDoc url) {
198             if (url.startsWith("/")) {
199                 url = url.substring(1);
200             }
201             return getClass().getResource(url);
202         }
203
204         public InputStream JavaDoc getResourceAsStream(String JavaDoc url) {
205             if (url.startsWith("/")) {
206                 url = url.substring(1);
207             }
208             return getClass().getResourceAsStream(url);
209         }
210
211         public Set JavaDoc getResourcePaths(String JavaDoc path) {
212             if(path.equals("/WEB-INF/lib")) {
213                 return Collections.singleton("/WEB-INF/lib/taglib-foo.jar");
214             }
215             else {
216                 return Collections.EMPTY_SET;
217             }
218         }
219
220         public String JavaDoc getServerInfo() {
221             return "FreeMarker/JUnit";
222         }
223
224         /**
225          * @deprecated
226          */

227         public Servlet JavaDoc getServlet(String JavaDoc arg0) {
228             throw new UnsupportedOperationException JavaDoc();
229         }
230
231         public String JavaDoc getServletContextName() {
232             return "freemarker";
233         }
234
235         /**
236          * @deprecated
237          */

238         public Enumeration JavaDoc getServletNames() {
239             throw new UnsupportedOperationException JavaDoc();
240         }
241
242         /**
243          * @deprecated
244          */

245         public Enumeration JavaDoc getServlets() {
246             throw new UnsupportedOperationException JavaDoc();
247         }
248
249         /**
250          * @deprecated
251          */

252         public void log(Exception JavaDoc arg0, String JavaDoc arg1) {
253         }
254
255         public void log(String JavaDoc arg0, Throwable JavaDoc arg1) {
256         }
257
258         public void log(String JavaDoc arg0) {
259         }
260
261         public void removeAttribute(String JavaDoc arg0) {
262             attributes.remove(arg0);
263         }
264
265         public void setAttribute(String JavaDoc arg0, Object JavaDoc arg1) {
266             attributes.put(arg0, arg1);
267         }
268     }
269
270     private static final class MockRequest
271     implements
272         HttpServletRequest JavaDoc
273     {
274         private final String JavaDoc pathInfo;
275         private HttpSession JavaDoc session;
276                    
277         MockRequest(String JavaDoc pathInfo) {
278             this.pathInfo = pathInfo;
279         }
280
281         public String JavaDoc getAuthType() {
282             return null;
283         }
284
285         public String JavaDoc getContextPath() {
286             return null;
287         }
288
289         public Cookie JavaDoc[] getCookies() {
290             return null;
291         }
292
293         public long getDateHeader(String JavaDoc arg0) {
294             return 0;
295         }
296
297         public String JavaDoc getHeader(String JavaDoc arg0) {
298             return null;
299         }
300
301         public Enumeration JavaDoc getHeaderNames() {
302             return null;
303         }
304
305         public Enumeration JavaDoc getHeaders(String JavaDoc arg0) {
306             return null;
307         }
308
309         public int getIntHeader(String JavaDoc arg0) {
310             return 0;
311         }
312
313         public String JavaDoc getMethod() {
314             return null;
315         }
316
317         public String JavaDoc getPathInfo() {
318             return pathInfo;
319         }
320
321         public String JavaDoc getPathTranslated() {
322             return null;
323         }
324
325         public String JavaDoc getQueryString() {
326             return null;
327         }
328
329         public String JavaDoc getRemoteUser() {
330             return null;
331         }
332
333         public String JavaDoc getRequestedSessionId() {
334             return null;
335         }
336
337         public String JavaDoc getRequestURI() {
338             return null;
339         }
340
341         public StringBuffer JavaDoc getRequestURL() {
342             return null;
343         }
344
345         public String JavaDoc getServletPath() {
346             return null;
347         }
348
349         public HttpSession JavaDoc getSession() {
350             return getSession(true);
351         }
352
353         public HttpSession JavaDoc getSession(boolean arg0) {
354             if(session == null && arg0) session = new MockSession();
355             return session;
356         }
357
358         public Principal JavaDoc getUserPrincipal() {
359             return null;
360         }
361
362         public boolean isRequestedSessionIdFromCookie() {
363             return false;
364         }
365
366         /**
367          * @deprecated
368          */

369         public boolean isRequestedSessionIdFromUrl() {
370             return false;
371         }
372
373         public boolean isRequestedSessionIdFromURL() {
374             return false;
375         }
376
377         public boolean isRequestedSessionIdValid() {
378             return false;
379         }
380
381         public boolean isUserInRole(String JavaDoc arg0) {
382             return false;
383         }
384
385         public Object JavaDoc getAttribute(String JavaDoc arg0) {
386             return null;
387         }
388
389         public Enumeration JavaDoc getAttributeNames() {
390             return null;
391         }
392
393         public String JavaDoc getCharacterEncoding() {
394             return null;
395         }
396
397         public int getContentLength() {
398             return 0;
399         }
400
401         public String JavaDoc getContentType() {
402             return null;
403         }
404
405         public ServletInputStream JavaDoc getInputStream() {
406             return null;
407         }
408
409         public Locale JavaDoc getLocale() {
410             return Locale.getDefault();
411         }
412
413         public Enumeration JavaDoc getLocales() {
414             return null;
415         }
416
417         public String JavaDoc getParameter(String JavaDoc arg0) {
418             return null;
419         }
420
421         public Map JavaDoc getParameterMap() {
422             return null;
423         }
424
425         public Enumeration JavaDoc getParameterNames() {
426             return null;
427         }
428
429         public String JavaDoc[] getParameterValues(String JavaDoc arg0) {
430             return null;
431         }
432
433         public String JavaDoc getProtocol() {
434             return null;
435         }
436
437         public BufferedReader JavaDoc getReader() {
438             return null;
439         }
440
441         /**
442          * @deprecated
443          */

444         public String JavaDoc getRealPath(String JavaDoc arg0) {
445             return null;
446         }
447
448         public String JavaDoc getRemoteAddr() {
449             return null;
450         }
451
452         public String JavaDoc getRemoteHost() {
453             return null;
454         }
455
456         public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc arg0) {
457             return null;
458         }
459
460         public String JavaDoc getScheme() {
461             return null;
462         }
463
464         public String JavaDoc getServerName() {
465             return null;
466         }
467
468         public int getServerPort() {
469             return 0;
470         }
471
472         public boolean isSecure() {
473             return false;
474         }
475
476         public void removeAttribute(String JavaDoc arg0) {
477         }
478
479         public void setAttribute(String JavaDoc arg0, Object JavaDoc arg1) {
480         }
481
482         public void setCharacterEncoding(String JavaDoc arg0) {
483         }
484     }
485
486     private static final class MockResponse
487     implements
488         HttpServletResponse JavaDoc
489     {
490         private final StringWriter JavaDoc writer = new StringWriter JavaDoc();
491         private final PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(writer);
492         
493         public void addCookie(Cookie JavaDoc arg0) {
494         }
495
496         public void addDateHeader(String JavaDoc arg0, long arg1) {
497         }
498
499         public void addHeader(String JavaDoc arg0, String JavaDoc arg1) {
500         }
501
502         public void addIntHeader(String JavaDoc arg0, int arg1) {
503         }
504
505         public boolean containsHeader(String JavaDoc arg0) {
506             return false;
507         }
508
509         /**
510          * @deprecated
511          */

512         public String JavaDoc encodeRedirectUrl(String JavaDoc arg0) {
513             return null;
514         }
515
516         public String JavaDoc encodeRedirectURL(String JavaDoc arg0) {
517             return null;
518         }
519
520         /**
521          * @deprecated
522          */

523         public String JavaDoc encodeUrl(String JavaDoc arg0) {
524             return null;
525         }
526
527         public String JavaDoc encodeURL(String JavaDoc arg0) {
528             return null;
529         }
530
531         public void sendError(int arg0, String JavaDoc arg1) {
532         }
533
534         public void sendError(int arg0) {
535         }
536
537         public void sendRedirect(String JavaDoc arg0) {
538         }
539
540         public void setDateHeader(String JavaDoc arg0, long arg1) {
541         }
542
543         public void setHeader(String JavaDoc arg0, String JavaDoc arg1) {
544         }
545
546         public void setIntHeader(String JavaDoc arg0, int arg1) {
547         }
548
549         /**
550          * @deprecated
551          */

552         public void setStatus(int arg0, String JavaDoc arg1) {
553         }
554
555         public void setStatus(int arg0) {
556         }
557
558         public void flushBuffer() {
559         }
560
561         public int getBufferSize() {
562             return 0;
563         }
564
565         public String JavaDoc getCharacterEncoding() {
566             return null;
567         }
568
569         public Locale JavaDoc getLocale() {
570             return null;
571         }
572
573         public ServletOutputStream JavaDoc getOutputStream() {
574             return null;
575         }
576
577         public PrintWriter JavaDoc getWriter() {
578             return pwriter;
579         }
580
581         public boolean isCommitted() {
582             return false;
583         }
584
585         public void reset() {
586         }
587
588         public void resetBuffer() {
589         }
590
591         public void setBufferSize(int arg0) {
592         }
593
594         public void setContentLength(int arg0) {
595         }
596
597         public void setContentType(String JavaDoc arg0) {
598         }
599
600         public void setLocale(Locale JavaDoc arg0) {
601         }
602         
603         public String JavaDoc toString() {
604             pwriter.flush();
605             return writer.toString();
606         }
607     }
608
609     private static final class MockSession implements HttpSession JavaDoc
610     {
611         public Object JavaDoc getAttribute(String JavaDoc arg0) {
612             return null;
613         }
614
615         public Enumeration JavaDoc getAttributeNames() {
616             return null;
617         }
618
619         public long getCreationTime() {
620             return 0;
621         }
622
623         public String JavaDoc getId() {
624             return null;
625         }
626
627         public long getLastAccessedTime() {
628             return 0;
629         }
630
631         public int getMaxInactiveInterval() {
632             return 0;
633         }
634
635         public ServletContext JavaDoc getServletContext() {
636             return null;
637         }
638
639         /**
640          * @deprecated
641          */

642         public HttpSessionContext JavaDoc getSessionContext() {
643             return null;
644         }
645
646         /**
647          * @deprecated
648          */

649         public Object JavaDoc getValue(String JavaDoc arg0) {
650             return null;
651         }
652
653         /**
654          * @deprecated
655          */

656         public String JavaDoc[] getValueNames() {
657             return null;
658         }
659
660         public void invalidate() {
661         }
662
663         public boolean isNew() {
664             return false;
665         }
666
667         /**
668          * @deprecated
669          */

670         public void putValue(String JavaDoc arg0, Object JavaDoc arg1) {
671         }
672
673         public void removeAttribute(String JavaDoc arg0) {
674         }
675
676         /**
677          * @deprecated
678          */

679         public void removeValue(String JavaDoc arg0) {
680         }
681
682         public void setAttribute(String JavaDoc arg0, Object JavaDoc arg1) {
683         }
684
685         public void setMaxInactiveInterval(int arg0) {
686         }
687 }
688     
689     /** Bootstrap for the self-test code.
690      */

691     public static void main( String JavaDoc[] argc ) throws Exception JavaDoc {
692         TestCase test = new TestJspTaglibs( "test-jsptaglibs.txt" );
693         test.run();
694     }
695 }
696
Popular Tags