KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > ClasspathResourceTest


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23
24 import org.apache.velocity.Template;
25 import org.apache.velocity.app.Velocity;
26 import org.apache.velocity.VelocityContext;
27 import org.apache.velocity.runtime.RuntimeSingleton;
28
29 /**
30  * Load templates from the Classpath.
31  *
32  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
33  * @author <a HREF="mailto:daveb@miceda-data.com">Dave Bryson</a>
34  * @version $Id: ClasspathResourceTest.java,v 1.9.4.1 2004/03/03 23:23:04 geirm Exp $
35  */

36 public class ClasspathResourceTest extends BaseTestCase
37 {
38      /**
39      * VTL file extension.
40      */

41     private static final String JavaDoc TMPL_FILE_EXT = "vm";
42
43     /**
44      * Comparison file extension.
45      */

46     private static final String JavaDoc CMP_FILE_EXT = "cmp";
47
48     /**
49      * Comparison file extension.
50      */

51     private static final String JavaDoc RESULT_FILE_EXT = "res";
52
53     /**
54      * Results relative to the build directory.
55      */

56     private static final String JavaDoc RESULTS_DIR = "../test/cpload/results";
57
58     /**
59      * Results relative to the build directory.
60      */

61     private static final String JavaDoc COMPARE_DIR = "../test/cpload/compare";
62
63     /**
64      * Default constructor.
65      */

66     public ClasspathResourceTest()
67     {
68         super("ClasspathResourceTest");
69
70         try
71         {
72             assureResultsDirectoryExists(RESULTS_DIR);
73             
74             Velocity.setProperty(Velocity.RESOURCE_LOADER, "classpath");
75
76             /*
77              * I don't think I should have to do this, these should
78              * be in the default config file.
79              */

80
81             Velocity.addProperty(
82                 "classpath." + Velocity.RESOURCE_LOADER + ".class",
83                     "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
84
85             Velocity.setProperty(
86                 "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");
87
88             Velocity.setProperty(
89                 "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
90                     "2");
91
92             Velocity.init();
93         }
94         catch (Exception JavaDoc e)
95         {
96             System.err.println("Cannot setup ClasspathResourceTest!");
97             e.printStackTrace();
98             System.exit(1);
99         }
100     }
101
102     public static junit.framework.Test suite ()
103     {
104         return new ClasspathResourceTest();
105     }
106
107     /**
108      * Runs the test.
109      */

110     public void runTest ()
111     {
112         try
113         {
114             /*
115              * lets ensure the results directory exists
116              */

117             assureResultsDirectoryExists(RESULTS_DIR);
118
119             Template template1 = RuntimeSingleton.getTemplate(
120                 getFileName(null, "template/test1", TMPL_FILE_EXT));
121             
122             Template template2 = RuntimeSingleton.getTemplate(
123                 getFileName(null, "template/test2", TMPL_FILE_EXT));
124            
125             FileOutputStream JavaDoc fos1 =
126                 new FileOutputStream JavaDoc (
127                     getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT));
128
129             FileOutputStream JavaDoc fos2 =
130                 new FileOutputStream JavaDoc (
131                     getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
132
133             Writer JavaDoc writer1 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos1));
134             Writer JavaDoc writer2 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos2));
135             
136             /*
137              * put the Vector into the context, and merge both
138              */

139
140             VelocityContext context = new VelocityContext();
141
142             template1.merge(context, writer1);
143             writer1.flush();
144             writer1.close();
145             
146             template2.merge(context, writer2);
147             writer2.flush();
148             writer2.close();
149
150             if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test1",RESULT_FILE_EXT,CMP_FILE_EXT) ||
151                 !isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT))
152             {
153                 fail("Output is incorrect!");
154             }
155         }
156         catch (Exception JavaDoc e)
157         {
158             fail(e.getMessage());
159         }
160     }
161 }
162
Popular Tags