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.TestCase; 24 import junit.framework.TestSuite; 25 import org.apache.commons.io.FileUtils; 26 import org.apache.commons.lang3.StringUtils; 27 import org.apache.velocity.app.VelocityEngine; 28 import org.apache.velocity.test.misc.TestLogger; 29 import org.apache.velocity.util.DeprecationAwareExtProperties; 30 31 import java.io.File; 32 import java.lang.reflect.Field; 33 import java.util.HashSet; 34 import java.util.List; 35 import java.util.Set; 36 import java.util.regex.Matcher; 37 import java.util.regex.Pattern; 38 39 /** 40 * Tests if we can hand Velocity an arbitrary class for logging. 41 * 42 * @author <a href="mailto:[email protected]">Geir Magnusson Jr.</a> 43 * @version $Id$ 44 */ 45 public class OldPropertiesTestCase extends TestCase implements TemplateTestBase 46 { 47 private VelocityEngine ve = null; 48 private TestLogger logger = null; 49 50 /** 51 * Default constructor. 52 */ OldPropertiesTestCase(String name)53 public OldPropertiesTestCase(String name) 54 { 55 super(name); 56 } 57 58 @Override setUp()59 public void setUp() 60 throws Exception 61 { 62 } 63 suite()64 public static Test suite () 65 { 66 return new TestSuite(OldPropertiesTestCase.class); 67 } 68 69 static Pattern propPattern = Pattern.compile("^([a-z._]+)\\s*=\\s*[^#]+.*$", Pattern.CASE_INSENSITIVE); 70 static Pattern warnPattern = Pattern.compile("^\\s*\\[warn\\]\\s*configuration key '([a-z._]+)' has been deprecated in favor of '([a-z._]+)'$", Pattern.CASE_INSENSITIVE); 71 72 static class Translator extends DeprecationAwareExtProperties 73 { 74 @Override translateKey(String oldName)75 public String translateKey(String oldName) { return super.translateKey(oldName); } 76 } 77 78 /** 79 * Check old properties setting and retrieval 80 */ testOldProperties()81 public void testOldProperties() 82 throws Exception 83 { 84 String oldProperties = TEST_COMPARE_DIR + "/oldproperties/velocity.properties"; 85 ve = new VelocityEngine(); 86 logger = new TestLogger(false, true); 87 logger.setEnabledLevel(TestLogger.LOG_LEVEL_WARN); 88 89 // put our test logger where it belongs for this test 90 Field loggerField = DeprecationAwareExtProperties.class.getDeclaredField("logger"); 91 loggerField.setAccessible(true); 92 loggerField.set(null, logger); 93 94 logger.on(); 95 ve.setProperties(oldProperties); 96 logger.off(); 97 98 Translator translator = new Translator(); 99 100 // check getting old/new values 101 List<String> oldPropSettings = FileUtils.readLines(new File(oldProperties)); 102 Set<String> oldKeys = new HashSet<>(); 103 for (String oldProp : oldPropSettings) 104 { 105 Matcher matcher = propPattern.matcher(oldProp); 106 if (matcher.matches()) 107 { 108 String propName = matcher.group(1); 109 String translated = translator.translateKey(propName); 110 if (!translated.equals(propName)) 111 { 112 Object oldKeyValue = ve.getProperty(propName); 113 Object newKeyValue = ve.getProperty(translated); 114 assertEquals(oldKeyValue, newKeyValue); 115 oldKeys.add(propName); 116 } 117 } 118 } 119 120 // check warnings in the logs 121 String log = logger.getLog(); 122 String logLines[] = log.split("\\r?\\n"); 123 for (String logLine : logLines) 124 { 125 Matcher matcher = warnPattern.matcher(logLine); 126 if (matcher.matches() && matcher.groupCount() == 2) 127 { 128 String oldName = matcher.group(1); 129 assertTrue(oldKeys.remove(oldName)); 130 } 131 } 132 if (oldKeys.size() > 0) 133 { 134 fail("No warning detected for the following properties: " + StringUtils.join(oldKeys, ", ")); 135 } 136 } 137 138 /** 139 * Check default properties 140 */ testNewProperties()141 public void testNewProperties() 142 throws Exception 143 { 144 ve = new VelocityEngine(); 145 logger = new TestLogger(false, true); 146 logger.setEnabledLevel(TestLogger.LOG_LEVEL_WARN); 147 148 // put our test logger where it belongs for this test 149 Field loggerField = DeprecationAwareExtProperties.class.getDeclaredField("logger"); 150 loggerField.setAccessible(true); 151 loggerField.set(null, logger); 152 153 logger.on(); 154 ve.init(); 155 logger.off(); 156 157 // check warnings in the logs 158 String log = logger.getLog(); 159 String logLines[] = log.split("\\r?\\n"); 160 for (String logLine : logLines) 161 { 162 Matcher matcher = warnPattern.matcher(logLine); 163 if (matcher.matches() && matcher.groupCount() == 2) 164 { 165 fail("Default properties contain deprecated property '" + matcher.group(1) + "', deprecated in favor of '" + matcher.group(2) + "'"); 166 } 167 } 168 169 } 170 } 171