KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.File JavaDoc;
24
25 import java.util.Properties JavaDoc;
26
27 import org.apache.velocity.Template;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.VelocityContext;
30 import org.apache.velocity.test.provider.TestProvider;
31 import org.apache.velocity.util.StringUtils;
32 import org.apache.velocity.runtime.VelocimacroFactory;
33
34 import junit.framework.TestCase;
35
36 /**
37  * Load templates from the Classpath.
38  *
39  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
40  * @author <a HREF="mailto:daveb@miceda-data.com">Dave Bryson</a>
41  * @version $Id: MultiLoaderTestCase.java,v 1.4.8.1 2004/03/03 23:23:04 geirm Exp $
42  */

43 public class MultiLoaderTestCase extends BaseTestCase
44 {
45      /**
46      * VTL file extension.
47      */

48     private static final String JavaDoc TMPL_FILE_EXT = "vm";
49
50     /**
51      * Comparison file extension.
52      */

53     private static final String JavaDoc CMP_FILE_EXT = "cmp";
54
55     /**
56      * Comparison file extension.
57      */

58     private static final String JavaDoc RESULT_FILE_EXT = "res";
59
60     /**
61      * Results relative to the build directory.
62      */

63     private static final String JavaDoc RESULTS_DIR = "../test/multiloader/results";
64
65     /**
66      * Path for templates. This property will override the
67      * value in the default velocity properties file.
68      */

69     private final static String JavaDoc FILE_RESOURCE_LOADER_PATH = "../test/multiloader";
70
71     /**
72      * Results relative to the build directory.
73      */

74     private static final String JavaDoc COMPARE_DIR = "../test/multiloader/compare";
75
76     /**
77      * Default constructor.
78      */

79     public MultiLoaderTestCase()
80     {
81         super("MultiLoaderTestCase");
82
83         try
84         {
85             assureResultsDirectoryExists(RESULTS_DIR);
86             
87             /*
88              * Set up the file loader.
89              */

90             
91             Velocity.setProperty(Velocity.RESOURCE_LOADER, "file");
92             
93             Velocity.setProperty(
94                 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
95             
96             Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath");
97
98             Velocity.addProperty(Velocity.RESOURCE_LOADER, "jar");
99
100             /*
101              * Set up the classpath loader.
102              */

103
104             Velocity.setProperty(
105                 "classpath." + Velocity.RESOURCE_LOADER + ".class",
106                     "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
107
108             Velocity.setProperty(
109                 "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");
110
111             Velocity.setProperty(
112                 "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
113                     "2");
114
115             /*
116              * setup the Jar loader
117              */

118
119             Velocity.setProperty(
120                                  "jar." + Velocity.RESOURCE_LOADER + ".class",
121                                  "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
122
123             Velocity.setProperty( "jar." + Velocity.RESOURCE_LOADER + ".path",
124                                   "jar:file:" + FILE_RESOURCE_LOADER_PATH + "/test2.jar" );
125
126             Velocity.init();
127         }
128         catch (Exception JavaDoc e)
129         {
130             System.err.println("Cannot setup MultiLoaderTestCase!");
131             e.printStackTrace();
132             System.exit(1);
133         }
134     }
135
136     public static junit.framework.Test suite ()
137     {
138         return new MultiLoaderTestCase();
139     }
140
141     /**
142      * Runs the test.
143      */

144     public void runTest ()
145     {
146         try
147         {
148             /*
149              * lets ensure the results directory exists
150              */

151             assureResultsDirectoryExists(RESULTS_DIR);
152
153             /*
154              * Template to find with the file loader.
155              */

156             Template template1 = Velocity.getTemplate(
157                 getFileName(null, "path1", TMPL_FILE_EXT));
158             
159             /*
160              * Template to find with the classpath loader.
161              */

162             Template template2 = Velocity.getTemplate(
163                 getFileName(null, "template/test1", TMPL_FILE_EXT));
164            
165             /*
166              * Template to find with the jar loader
167              */

168             Template template3 = Velocity.getTemplate(
169                getFileName(null, "template/test2", TMPL_FILE_EXT));
170
171             /*
172              * and the results files
173              */

174
175             FileOutputStream JavaDoc fos1 =
176                 new FileOutputStream JavaDoc (
177                     getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
178
179             FileOutputStream JavaDoc fos2 =
180                 new FileOutputStream JavaDoc (
181                     getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
182
183             FileOutputStream JavaDoc fos3 =
184                 new FileOutputStream JavaDoc (
185                     getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));
186
187             Writer JavaDoc writer1 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos1));
188             Writer JavaDoc writer2 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos2));
189             Writer JavaDoc writer3 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos3));
190             
191             /*
192              * put the Vector into the context, and merge both
193              */

194
195             VelocityContext context = new VelocityContext();
196
197             template1.merge(context, writer1);
198             writer1.flush();
199             writer1.close();
200             
201             template2.merge(context, writer2);
202             writer2.flush();
203             writer2.close();
204
205             template3.merge(context, writer3);
206             writer3.flush();
207             writer3.close();
208
209             if (!isMatch(RESULTS_DIR,COMPARE_DIR,"path1",RESULT_FILE_EXT,CMP_FILE_EXT))
210             {
211                 fail("Output incorrect for FileResourceLoader test.");
212             }
213  
214             if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT) )
215             {
216                 fail("Output incorrect for ClasspathResourceLoader test.");
217             }
218             
219             if( !isMatch(RESULTS_DIR,COMPARE_DIR,"test3",RESULT_FILE_EXT,CMP_FILE_EXT))
220             {
221                 fail("Output incorrect for JarResourceLoader test.");
222             }
223         }
224         catch (Exception JavaDoc e)
225         {
226             fail(e.getMessage());
227         }
228     }
229 }
230
Popular Tags