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