1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.BufferedWriter;
23  import java.io.FileOutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.velocity.Template;
31  import org.apache.velocity.VelocityContext;
32  import org.apache.velocity.app.VelocityEngine;
33  import org.apache.velocity.runtime.RuntimeConstants;
34  import org.apache.velocity.test.misc.TestLogChute;
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 832247 2009-11-03 01:29:30Z wglass $
42   */
43  public class MultiLoaderTestCase extends BaseTestCase
44  {
45       /**
46       * VTL file extension.
47       */
48      private static final String TMPL_FILE_EXT = "vm";
49  
50      /**
51       * Comparison file extension.
52       */
53      private static final String CMP_FILE_EXT = "cmp";
54  
55      /**
56       * Comparison file extension.
57       */
58      private static final String RESULT_FILE_EXT = "res";
59  
60      /**
61       * Results relative to the build directory.
62       */
63      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/multiloader";
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 FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/multiloader";
70  
71      /**
72       * Results relative to the build directory.
73       */
74      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/multiloader/compare";
75  
76      VelocityEngine engine;
77      
78      /**
79       * Default constructor.
80       */
81      public MultiLoaderTestCase(String name)
82      {
83          super(name);
84      }
85  
86      public void setUp()
87              throws Exception
88      {
89          engine = new VelocityEngine();
90          
91          assureResultsDirectoryExists(RESULTS_DIR);
92  
93          /*
94           * Set up the file loader.
95           */
96  
97          engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
98  
99          engine.setProperty(
100                 RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
101 
102         engine.addProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
103 
104         engine.addProperty(RuntimeConstants.RESOURCE_LOADER, "jar");
105 
106         /*
107          *  Set up the classpath loader.
108          */
109 
110         engine.setProperty(
111             "classpath." + RuntimeConstants.RESOURCE_LOADER + ".class",
112                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
113 
114         engine.setProperty(
115             "classpath." + RuntimeConstants.RESOURCE_LOADER + ".cache", "false");
116 
117         engine.setProperty(
118             "classpath." + RuntimeConstants.RESOURCE_LOADER + ".modificationCheckInterval",
119                 "2");
120 
121         /*
122          *  setup the Jar loader
123          */
124 
125         engine.setProperty(
126                              "jar." + RuntimeConstants.RESOURCE_LOADER + ".class",
127                              "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
128 
129         engine.setProperty( "jar." + RuntimeConstants.RESOURCE_LOADER + ".path",
130                               "jar:file:" + FILE_RESOURCE_LOADER_PATH + "/test2.jar" );
131 
132 
133         engine.setProperty(
134                 RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
135 
136         engine.init();
137     }
138 
139     public static Test suite ()
140     {
141         return new TestSuite(MultiLoaderTestCase.class);
142     }
143 
144     /**
145      * Runs the test.
146      */
147     public void testMultiLoader ()
148             throws Exception
149     {
150         /*
151          *  lets ensure the results directory exists
152          */
153         assureResultsDirectoryExists(RESULTS_DIR);
154 
155         /*
156          * Template to find with the file loader.
157          */
158         Template template1 = engine.getTemplate(
159             getFileName(null, "path1", TMPL_FILE_EXT));
160 
161         /*
162          * Template to find with the classpath loader.
163          */
164         Template template2 = engine.getTemplate("template/test1." + TMPL_FILE_EXT);
165 
166         /*
167          * Template to find with the jar loader
168          */
169         Template template3 = engine.getTemplate("template/test2." + TMPL_FILE_EXT);
170 
171         /*
172          * and the results files
173          */
174 
175         FileOutputStream fos1 =
176             new FileOutputStream (
177                 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
178 
179         FileOutputStream fos2 =
180             new FileOutputStream (
181                 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
182 
183         FileOutputStream fos3 =
184             new FileOutputStream (
185                 getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));
186 
187         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
188         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
189         Writer writer3 = new BufferedWriter(new OutputStreamWriter(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 }