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  
29  /**
30   * Make sure that a forward referenced macro inside another macro definition does
31   * not report an error in the log.
32   * (VELOCITY-71).
33   * 
34   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
35   * @version $Id: MacroForwardDefineTestCase.java 491369 2006-12-31 02:52:05Z wglass $
36   */
37  public class MacroForwardDefineTestCase 
38          extends BaseTestCase
39  {
40     /**
41      * Path for templates. This property will override the
42      * value in the default velocity properties file.
43      */
44     private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/macroforwarddefine";
45  
46      /**
47       * Results relative to the build directory.
48       */
49      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/macroforwarddefine";
50  
51      /**
52       * Results relative to the build directory.
53       */
54      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/macroforwarddefine/compare";
55  
56      /**
57       * Collects the log messages.
58       */
59      private TestLogChute logger = new TestLogChute();
60      
61      /**
62       * Default constructor.
63       */
64      public MacroForwardDefineTestCase(String name)
65      {
66          super(name);
67      }
68  
69      public void setUp()
70          throws Exception
71      {
72          assureResultsDirectoryExists(RESULTS_DIR);
73                  
74          // use Velocity.setProperty (instead of properties file) so that we can use actual instance of log
75          Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER,"file");
76          Velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH );
77          Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID,"true");
78          Velocity.setProperty(RuntimeConstants.VM_LIBRARY, "macros.vm");
79  
80          // actual instance of logger
81          logger = new TestLogChute();
82          Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM,logger);
83          Velocity.setProperty("runtime.log.logsystem.test.level", "error");
84  
85          Velocity.init();
86      }
87  
88      public static Test suite()
89      {
90         return new TestSuite(MacroForwardDefineTestCase.class);
91      }
92  
93      public void testLogResult()
94          throws Exception
95      {
96          if ( !isMatch(logger.getLog(), COMPARE_DIR, "velocity.log", "cmp"))
97          {
98              fail("Output incorrect.");
99          }
100     }
101 }