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