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 junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.velocity.app.Velocity;
26  import org.apache.velocity.runtime.RuntimeConstants;
27  import org.apache.velocity.test.misc.TestLogChute;
28  import org.apache.velocity.Template;
29  import org.apache.velocity.VelocityContext;
30  
31  import java.io.*;
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 697214 2008-09-19 19:55:59Z nbubna $
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      /**
66       * Default constructor.
67       */
68      public MacroForwardDefineTestCase(String name)
69      {
70          super(name);
71      }
72  
73      public void setUp()
74          throws Exception
75      {
76          assureResultsDirectoryExists(RESULTS_DIR);
77  
78          // use Velocity.setProperty (instead of properties file) so that we can use actual instance of log
79          Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER,"file");
80          Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH );
81          Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID,"true");
82  
83          // actual instance of logger
84          logger = new TestLogChute();
85          logger.off();
86          Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,logger);
87          Velocity.setProperty(TestLogChute.TEST_LOGGER_LEVEL, "debug");
88          Velocity.init();
89      }
90  
91      public static Test suite()
92      {
93         return new TestSuite(MacroForwardDefineTestCase.class);
94      }
95  
96      public void testLogResult()
97          throws Exception
98      {
99          VelocityContext context = new VelocityContext();
100         Template template = Velocity.getTemplate("macros.vm");
101 
102         // try to get only messages during merge
103         logger.on();
104         template.merge(context, new StringWriter());
105         logger.off();
106 
107         String resultLog = logger.getLog();
108         if ( !isMatch(resultLog, COMPARE_DIR, "velocity.log", "cmp"))
109         {
110             String compare = getFileContents(COMPARE_DIR, "velocity.log", CMP_FILE_EXT);
111 
112             String msg = "Log output was incorrect\n"+
113                 "-----Result-----\n"+ resultLog +
114                 "----Expected----\n"+ compare +
115                 "----------------";
116 
117             fail(msg);
118         }
119     }
120 }