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.File;
24  import java.io.FileOutputStream;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import junit.framework.TestSuite;
31  
32  import org.apache.velocity.Template;
33  import org.apache.velocity.VelocityContext;
34  import org.apache.velocity.app.Velocity;
35  import org.apache.velocity.app.VelocityEngine;
36  import org.apache.velocity.runtime.RuntimeConstants;
37  import org.apache.velocity.test.misc.TestLogChute;
38  
39  /**
40   * Macro library inclution via the Template.merge method is tested using this
41   * class.
42   */
43  
44  public class VMLibraryTestCase extends BaseTestCase
45  {
46      /**
47       * This engine is used with local namespaces
48       */
49      private VelocityEngine ve1 = new VelocityEngine();
50  
51      /**
52       * This engine is used with global namespaces
53       */
54      private VelocityEngine ve2 = new VelocityEngine();
55  
56      private static final String RESULT_DIR = TEST_RESULT_DIR + "/macrolibs";
57  
58      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/macrolibs/compare";
59  
60      public VMLibraryTestCase(String name)
61      {
62          super(name);
63      }
64  
65      public void setUp()
66              throws Exception
67      {
68          /*
69           *  setup local scope for templates
70           */
71          ve1.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
72          ve1.setProperty("velocimacro.permissions.allow.inline.to.replace.global",
73                  Boolean.FALSE);
74          /**
75           * Turn on the cache
76           */
77          ve1.setProperty("file.resource.loader.cache", Boolean.TRUE);
78  
79          ve1.setProperty(
80                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
81  
82          ve1.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
83          ve1.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
84                  TEST_COMPARE_DIR + "/macrolibs");
85          ve1.init();
86  
87          /**
88           * Set to global namespaces
89           */
90          ve2.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.FALSE);
91          ve2.setProperty("velocimacro.permissions.allow.inline.to.replace.global",
92                  Boolean.TRUE);
93          /**
94           * Turn on the cache
95           */
96          ve2.setProperty("file.resource.loader.cache", Boolean.FALSE);
97  
98          ve2.setProperty(
99                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
100 
101         ve2.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
102         ve2.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
103                 TEST_COMPARE_DIR + "/macrolibs");
104         ve2.init();
105     }
106 
107     public static junit.framework.Test suite()
108     {
109         return new TestSuite(VMLibraryTestCase.class);
110     }
111 
112     /**
113      * Runs the tests with local namespace.
114      */
115     public void testVelociMacroLibWithLocalNamespace()
116             throws Exception
117     {
118         assureResultsDirectoryExists(RESULT_DIR);
119         /**
120          * Clear the file before proceeding
121          */
122         File file = new File(getFileName(
123                 RESULT_DIR, "vm_library_local", RESULT_FILE_EXT));
124         if (file.exists())
125         {
126             file.delete();
127         }
128 
129         /**
130          * Create a file output stream for appending
131          */
132         FileOutputStream fos = new FileOutputStream (getFileName(
133                 RESULT_DIR, "vm_library_local", RESULT_FILE_EXT), true);
134 
135         List templateList = new ArrayList();
136         VelocityContext context = new VelocityContext();
137         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
138 
139         templateList.add("vm_library1.vm");
140 
141         Template template = ve1.getTemplate("vm_library_local.vm");
142         template.merge(context, writer, templateList);
143 
144         /**
145          * remove the first template library and includes a new library
146          * with a new definition for macros
147          */
148         templateList.remove(0);
149         templateList.add("vm_library2.vm");
150         template = ve1.getTemplate("vm_library_local.vm");
151         template.merge(context, writer, templateList);
152 
153         /*
154          *Show that caching is working
155          */
156         Template t1 = ve1.getTemplate("vm_library_local.vm");
157         Template t2 = ve1.getTemplate("vm_library_local.vm");
158 
159         assertEquals("Both templates refer to the same object", t1, t2);
160 
161         /**
162          * Remove the libraries
163          */
164         template = ve1.getTemplate("vm_library_local.vm");
165         template.merge(context, writer);
166 
167         /**
168          * Write to the file
169          */
170         writer.flush();
171         writer.close();
172 
173         if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_local",
174                 RESULT_FILE_EXT,CMP_FILE_EXT))
175         {
176             fail("Processed template did not match expected output");
177         }
178     }
179 
180     /**
181      * Runs the tests with global namespace.
182      */
183     public void testVelociMacroLibWithGlobalNamespace()
184             throws Exception
185     {
186         assureResultsDirectoryExists(RESULT_DIR);
187         /**
188          * Clear the file before proceeding
189          */
190         File file = new File(getFileName(
191                 RESULT_DIR, "vm_library_global", RESULT_FILE_EXT));
192         if (file.exists())
193         {
194             file.delete();
195         }
196 
197         /**
198          * Create a file output stream for appending
199          */
200         FileOutputStream fos = new FileOutputStream (getFileName(
201                 RESULT_DIR, "vm_library_global", RESULT_FILE_EXT), true);
202 
203         List templateList = new ArrayList();
204         VelocityContext context = new VelocityContext();
205         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
206 
207         templateList.add("vm_library1.vm");
208 
209         Template template = ve1.getTemplate("vm_library_global.vm");
210         template.merge(context, writer, templateList);
211 
212         /**
213          * remove the first template library and includes a new library
214          * with a new definition for macros
215          */
216         templateList.remove(0);
217         templateList.add("vm_library2.vm");
218         template = ve1.getTemplate("vm_library_global.vm");
219         template.merge(context, writer, templateList);
220 
221         /*
222          *Show that caching is not working (We have turned off cache)
223          */
224         Template t1 = ve2.getTemplate("vm_library_global.vm");
225         Template t2 = ve2.getTemplate("vm_library_global.vm");
226 
227         assertNotSame("Defferent objects", t1, t2);
228 
229         /**
230          * Write to the file
231          */
232         writer.flush();
233         writer.close();
234 
235         if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_global",
236                 RESULT_FILE_EXT,CMP_FILE_EXT))
237         {
238             fail("Processed template did not match expected output");
239         }
240     }
241 
242     /**
243      * Runs the tests with global namespace.
244      */
245     public void testVelociMacroLibWithDuplicateDefinitions()
246             throws Exception
247     {
248         assureResultsDirectoryExists(RESULT_DIR);
249         /**
250          * Clear the file before proceeding
251          */
252         File file = new File(getFileName(
253                 RESULT_DIR, "vm_library_duplicate", RESULT_FILE_EXT));
254         if (file.exists())
255         {
256             file.delete();
257         }
258 
259         /**
260          * Create a file output stream for appending
261          */
262         FileOutputStream fos = new FileOutputStream (getFileName(
263                 RESULT_DIR, "vm_library_duplicate", RESULT_FILE_EXT), true);
264 
265         List templateList = new ArrayList();
266         VelocityContext context = new VelocityContext();
267         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
268 
269         templateList.add("vm_library1.vm");
270         templateList.add("vm_library2.vm");
271 
272         Template template = ve1.getTemplate("vm_library.vm");
273         template.merge(context, writer, templateList);
274 
275         /**
276          * Write to the file
277          */
278         writer.flush();
279         writer.close();
280 
281         if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_duplicate",
282                 RESULT_FILE_EXT,CMP_FILE_EXT))
283         {
284             fail("Processed template did not match expected output");
285         }
286     }
287 
288     /**
289      * Test whether the literal text is given if a definition cannot be
290      * found for a macro.
291      *
292      * @throws Exception
293      */
294     public void testMacrosWithNoDefinition()
295             throws Exception
296     {
297         assureResultsDirectoryExists(RESULT_DIR);
298 
299         FileOutputStream fos = new FileOutputStream (getFileName(
300                 RESULT_DIR, "vm_library", RESULT_FILE_EXT));
301 
302         VelocityContext context = new VelocityContext();
303         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
304 
305         Template template = ve1.getTemplate("vm_library.vm");
306         template.merge(context, writer, null);
307 
308         /**
309          * Write to the file
310          */
311         writer.flush();
312         writer.close();
313 
314         /**
315          * outputs the macro calls
316          */
317         if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library",
318                 RESULT_FILE_EXT,CMP_FILE_EXT))
319         {
320             fail("Processed template did not match expected output");
321         }
322     }
323 
324 
325 }