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.StringWriter;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.velocity.Template;
28  import org.apache.velocity.VelocityContext;
29  import org.apache.velocity.app.VelocityEngine;
30  import org.apache.velocity.runtime.RuntimeConstants;
31  import org.apache.velocity.test.misc.TestLogChute;
32  
33  /**
34   * Make sure that a forward referenced macro inside another macro definition does
35   * not report an error in the log.
36   * (VELOCITY-71).
37   * 
38   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
39   * @version $Id: MacroForwardDefineTestCase.java 832247 2009-11-03 01:29:30Z wglass $
40   */
41  public class MacroForwardDefineTestCase 
42          extends BaseTestCase
43  {
44     /**
45      * Path for templates. This property will override the
46      * value in the default velocity properties file.
47      */
48     private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/macroforwarddefine";
49  
50      /**
51       * Results relative to the build directory.
52       */
53      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/macroforwarddefine";
54  
55      /**
56       * Results relative to the build directory.
57       */
58      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/macroforwarddefine/compare";
59  
60      /**
61       * Collects the log messages.
62       */
63      private TestLogChute logger = new TestLogChute();
64  
65      VelocityEngine engine;
66      
67      /**
68       * Default constructor.
69       */
70      public MacroForwardDefineTestCase(String name)
71      {
72          super(name);
73      }
74  
75      public void setUp()
76          throws Exception
77      {
78          assureResultsDirectoryExists(RESULTS_DIR);
79  
80          engine = new VelocityEngine();
81          
82          // use Velocity.setProperty (instead of properties file) so that we can use actual instance of log
83          engine.setProperty(RuntimeConstants.RESOURCE_LOADER,"file");
84          engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH );
85          engine.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID,"true");
86  
87          // actual instance of logger
88          logger = new TestLogChute(true, false);
89          engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,logger);
90          engine.setProperty(TestLogChute.TEST_LOGGER_LEVEL, "debug");
91          engine.init();
92      }
93  
94      public static Test suite()
95      {
96         return new TestSuite(MacroForwardDefineTestCase.class);
97      }
98  
99      public void testLogResult()
100         throws Exception
101     {
102         VelocityContext context = new VelocityContext();
103         Template template = engine.getTemplate("macros.vm");
104 
105         // try to get only messages during merge
106         logger.startCapture();
107         template.merge(context, new StringWriter());
108         logger.stopCapture();
109 
110         String resultLog = logger.getLog();
111         if ( !isMatch(resultLog, COMPARE_DIR, "velocity.log", "cmp"))
112         {
113             String compare = getFileContents(COMPARE_DIR, "velocity.log", CMP_FILE_EXT);
114 
115             String msg = "Log output was incorrect\n"+
116                 "-----Result-----\n"+ resultLog +
117                 "----Expected----\n"+ compare +
118                 "----------------";
119 
120             fail(msg);
121         }
122     }
123 }