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.Velocity;
33  import org.apache.velocity.test.misc.TestLogChute;
34  
35  /**
36   * Load templates from the Classpath.
37   *
38   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
39   * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
40   * @version $Id: MultiLoaderTestCase.java 704299 2008-10-14 03:13:16Z nbubna $
41   */
42  public class MultiLoaderTestCase extends BaseTestCase
43  {
44       /**
45       * VTL file extension.
46       */
47      private static final String TMPL_FILE_EXT = "vm";
48  
49      /**
50       * Comparison file extension.
51       */
52      private static final String CMP_FILE_EXT = "cmp";
53  
54      /**
55       * Comparison file extension.
56       */
57      private static final String RESULT_FILE_EXT = "res";
58  
59      /**
60       * Results relative to the build directory.
61       */
62      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/multiloader";
63  
64      /**
65       * Path for templates. This property will override the
66       * value in the default velocity properties file.
67       */
68      private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/multiloader";
69  
70      /**
71       * Results relative to the build directory.
72       */
73      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/multiloader/compare";
74  
75      /**
76       * Default constructor.
77       */
78      public MultiLoaderTestCase(String name)
79      {
80          super(name);
81      }
82  
83      public void setUp()
84              throws Exception
85      {
86          assureResultsDirectoryExists(RESULTS_DIR);
87  
88          /*
89           * Set up the file loader.
90           */
91  
92          Velocity.setProperty(Velocity.RESOURCE_LOADER, "file");
93  
94          Velocity.setProperty(
95              Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
96  
97          Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath");
98  
99          Velocity.addProperty(Velocity.RESOURCE_LOADER, "jar");
100 
101         /*
102          *  Set up the classpath loader.
103          */
104 
105         Velocity.setProperty(
106             "classpath." + Velocity.RESOURCE_LOADER + ".class",
107                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
108 
109         Velocity.setProperty(
110             "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");
111 
112         Velocity.setProperty(
113             "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
114                 "2");
115 
116         /*
117          *  setup the Jar loader
118          */
119 
120         Velocity.setProperty(
121                              "jar." + Velocity.RESOURCE_LOADER + ".class",
122                              "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
123 
124         Velocity.setProperty( "jar." + Velocity.RESOURCE_LOADER + ".path",
125                               "jar:file:" + FILE_RESOURCE_LOADER_PATH + "/test2.jar" );
126 
127 
128         Velocity.setProperty(
129                 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
130 
131         Velocity.init();
132     }
133 
134     public static Test suite ()
135     {
136         return new TestSuite(MultiLoaderTestCase.class);
137     }
138 
139     /**
140      * Runs the test.
141      */
142     public void testMultiLoader ()
143             throws Exception
144     {
145         /*
146          *  lets ensure the results directory exists
147          */
148         assureResultsDirectoryExists(RESULTS_DIR);
149 
150         /*
151          * Template to find with the file loader.
152          */
153         Template template1 = Velocity.getTemplate(
154             getFileName(null, "path1", TMPL_FILE_EXT));
155 
156         /*
157          * Template to find with the classpath loader.
158          */
159         Template template2 = Velocity.getTemplate("template/test1." + TMPL_FILE_EXT);
160 
161         /*
162          * Template to find with the jar loader
163          */
164         Template template3 = Velocity.getTemplate("template/test2." + TMPL_FILE_EXT);
165 
166         /*
167          * and the results files
168          */
169 
170         FileOutputStream fos1 =
171             new FileOutputStream (
172                 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
173 
174         FileOutputStream fos2 =
175             new FileOutputStream (
176                 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
177 
178         FileOutputStream fos3 =
179             new FileOutputStream (
180                 getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));
181 
182         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
183         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
184         Writer writer3 = new BufferedWriter(new OutputStreamWriter(fos3));
185 
186         /*
187          *  put the Vector into the context, and merge both
188          */
189 
190         VelocityContext context = new VelocityContext();
191 
192         template1.merge(context, writer1);
193         writer1.flush();
194         writer1.close();
195 
196         template2.merge(context, writer2);
197         writer2.flush();
198         writer2.close();
199 
200         template3.merge(context, writer3);
201         writer3.flush();
202         writer3.close();
203 
204         if (!isMatch(RESULTS_DIR,COMPARE_DIR,"path1",RESULT_FILE_EXT,CMP_FILE_EXT))
205         {
206             fail("Output incorrect for FileResourceLoader test.");
207         }
208 
209         if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT) )
210         {
211             fail("Output incorrect for ClasspathResourceLoader test.");
212         }
213 
214         if( !isMatch(RESULTS_DIR,COMPARE_DIR,"test3",RESULT_FILE_EXT,CMP_FILE_EXT))
215         {
216             fail("Output incorrect for JarResourceLoader test.");
217         }
218     }
219 }