1   package org.apache.velocity.test.issues;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
5    * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
7    * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
8    * applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
9    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  
13  import java.io.BufferedWriter;
14  import java.io.FileOutputStream;
15  import java.io.OutputStreamWriter;
16  import java.io.StringWriter;
17  import java.io.Writer;
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  import org.apache.velocity.Template;
23  import org.apache.velocity.VelocityContext;
24  import org.apache.velocity.app.Velocity;
25  import org.apache.velocity.app.VelocityEngine;
26  import org.apache.velocity.runtime.RuntimeSingleton;
27  import org.apache.velocity.test.misc.TestLogChute;
28  import org.apache.velocity.test.BaseTestCase;
29  
30  /**
31   * Test Case for <a href="https://issues.apache.org/jira/browse/VELOCITY-537">Velocity Issue 537</a>.
32   */
33  public class Velocity537TestCase extends BaseTestCase
34  {
35      /**
36       * Comparison file extension.
37       */
38      private static final String CMP_FILE_EXT    = "cmp";
39  
40      /**
41       * Comparison file extension.
42       */
43      private static final String RESULT_FILE_EXT = "res";
44  
45      /**
46       * Results relative to the build directory.
47       */
48      private static final String RESULTS_DIR     = TEST_RESULT_DIR + "/issues/velocity-537";
49  
50      /**
51       * Template Directory
52       */
53      private static final String TEMPLATE_DIR    = TEST_COMPARE_DIR + "/issues/velocity-537/templates";
54  
55      /**
56       * Results relative to the build directory.
57       */
58      private static final String COMPARE_DIR     = TEST_COMPARE_DIR + "/issues/velocity-537/compare";
59  
60      public Velocity537TestCase(final String name) throws Exception
61      {
62          super(name);
63      }
64  
65      public static Test suite()
66      {
67          return new TestSuite(Velocity537TestCase.class);
68      }
69  
70      private VelocityEngine velocityEngine;
71      public void setUp() throws Exception
72      {
73  
74          assureResultsDirectoryExists(RESULTS_DIR);
75  
76          velocityEngine = new VelocityEngine();
77          velocityEngine.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH, TEMPLATE_DIR);
78  
79          velocityEngine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
80  
81          velocityEngine.init();
82      }
83  
84      public void testVelocity537() throws Exception
85      {
86          executeTest("velocity537.vm");
87      }
88  
89      public void testVelocity537Again() throws Exception
90      {
91          executeTest("velocity537b.vm");
92      }
93  
94      protected Template executeTest(final String templateName) throws Exception
95      {
96          Template template = velocityEngine.getTemplate(templateName);
97  
98          FileOutputStream fos = new FileOutputStream(getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
99  
100         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
101 
102         VelocityContext context = new VelocityContext();
103 
104         template.merge(context, writer);
105         writer.flush();
106         writer.close();
107 
108         if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName, RESULT_FILE_EXT, CMP_FILE_EXT))
109         {
110             // just to be useful, output the output in the fail message
111             StringWriter out = new StringWriter();
112             template.merge(context, out);
113 
114             String compare = getFileContents(COMPARE_DIR, templateName, CMP_FILE_EXT);
115 
116             fail("Output incorrect for Template: " + templateName + ": \""+out+"\""+
117                  "; it did not match: \""+compare+"\"");
118         }
119 
120         return template;
121     }
122 }