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.test.BaseTestCase;
27  import org.apache.velocity.test.misc.TestLogChute;
28  
29  /**
30   * Test Case for <a href="https://issues.apache.org/jira/browse/VELOCITY-537">Velocity Issue 537</a>.
31   */
32  public class Velocity537TestCase extends BaseTestCase
33  {
34      /**
35       * Comparison file extension.
36       */
37      private static final String CMP_FILE_EXT    = "cmp";
38  
39      /**
40       * Comparison file extension.
41       */
42      private static final String RESULT_FILE_EXT = "res";
43  
44      /**
45       * Results relative to the build directory.
46       */
47      private static final String RESULTS_DIR     = TEST_RESULT_DIR + "/issues/velocity-537";
48  
49      /**
50       * Template Directory
51       */
52      private static final String TEMPLATE_DIR    = TEST_COMPARE_DIR + "/issues/velocity-537/templates";
53  
54      /**
55       * Results relative to the build directory.
56       */
57      private static final String COMPARE_DIR     = TEST_COMPARE_DIR + "/issues/velocity-537/compare";
58  
59      public Velocity537TestCase(final String name) throws Exception
60      {
61          super(name);
62      }
63  
64      public static Test suite()
65      {
66          return new TestSuite(Velocity537TestCase.class);
67      }
68  
69      private VelocityEngine velocityEngine;
70      public void setUp() throws Exception
71      {
72  
73          assureResultsDirectoryExists(RESULTS_DIR);
74  
75          velocityEngine = new VelocityEngine();
76          velocityEngine.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH, TEMPLATE_DIR);
77  
78          velocityEngine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
79  
80          velocityEngine.init();
81      }
82  
83      public void testVelocity537() throws Exception
84      {
85          executeTest("velocity537.vm");
86      }
87  
88      public void testVelocity537Again() throws Exception
89      {
90          executeTest("velocity537b.vm");
91      }
92  
93      protected Template executeTest(final String templateName) throws Exception
94      {
95          Template template = velocityEngine.getTemplate(templateName);
96  
97          FileOutputStream fos = new FileOutputStream(getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
98  
99          Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
100 
101         VelocityContext context = new VelocityContext();
102 
103         template.merge(context, writer);
104         writer.flush();
105         writer.close();
106 
107         if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName, RESULT_FILE_EXT, CMP_FILE_EXT))
108         {
109             // just to be useful, output the output in the fail message
110             StringWriter out = new StringWriter();
111             template.merge(context, out);
112 
113             String compare = getFileContents(COMPARE_DIR, templateName, CMP_FILE_EXT);
114 
115             fail("Output incorrect for Template: " + templateName + ": \""+out+"\""+
116                  "; it did not match: \""+compare+"\"");
117         }
118 
119         return template;
120     }
121 }