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 org.apache.velocity.Template; 23 import org.apache.velocity.VelocityContext; 24 import org.apache.velocity.app.VelocityEngine; 25 import org.apache.velocity.runtime.RuntimeConstants; 26 27 import java.io.BufferedWriter; 28 import java.io.FileOutputStream; 29 import java.io.OutputStreamWriter; 30 import java.io.Writer; 31 32 /** 33 * This class tests the mode where velocimacros do preserve arguments literals 34 */ 35 36 public class VelocimacroBCModeTestCase extends BaseTestCase 37 { 38 private static final String BASE_DIR = TEST_COMPARE_DIR + "/bc_mode"; 39 private static final String CMP_DIR = BASE_DIR + "/compare"; 40 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/bc_mode"; 41 VelocimacroBCModeTestCase(final String name)42 public VelocimacroBCModeTestCase(final String name) 43 { 44 super(name); 45 } 46 47 @Override setUpEngine(VelocityEngine engine)48 protected void setUpEngine(VelocityEngine engine) 49 { 50 boolean bcMode = !getName().contains("NoPreserve"); 51 engine.setProperty(RuntimeConstants.VM_ENABLE_BC_MODE, bcMode); 52 engine.setProperty("file.resource.loader.path", TEST_COMPARE_DIR + "/bc_mode"); 53 } 54 testPreserveLiterals()55 public void testPreserveLiterals() 56 { 57 assertEvalEquals("$bar","#macro(m $foo)$foo#end#m($bar)"); 58 } 59 testGlobalDefaults()60 public void testGlobalDefaults() 61 { 62 assertEvalEquals("foo","#macro(m $foo)$foo#end#set($foo='foo')#m()"); 63 } 64 testVariousCasesPreserve()65 public void testVariousCasesPreserve() throws Exception 66 { 67 doTestVariousCases("bc_mode_enabled"); 68 } 69 testVariousCasesNoPreserve()70 public void testVariousCasesNoPreserve() throws Exception 71 { 72 doTestVariousCases("bc_mode_disabled"); 73 } 74 doTestVariousCases(String compare_ext)75 private void doTestVariousCases(String compare_ext) throws Exception 76 { 77 assureResultsDirectoryExists(RESULTS_DIR); 78 String basefilename = "test_bc_mode"; 79 Template template = engine.getTemplate( getFileName(null, basefilename, "vtl") ); 80 context = new VelocityContext(); 81 FileOutputStream fos; 82 Writer fwriter; 83 84 fos = new FileOutputStream (getFileName(RESULTS_DIR, basefilename, RESULT_FILE_EXT)); 85 86 fwriter = new BufferedWriter( new OutputStreamWriter(fos) ); 87 88 template.merge(context, fwriter); 89 fwriter.flush(); 90 fwriter.close(); 91 92 if (!isMatch(RESULTS_DIR, CMP_DIR, basefilename, RESULT_FILE_EXT, compare_ext)) 93 { 94 String result = getFileContents(RESULTS_DIR, basefilename, RESULT_FILE_EXT); 95 String compare = getFileContents(CMP_DIR, basefilename, compare_ext); 96 97 assertEquals(compare, result); 98 } 99 } 100 } 101