001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.configuration.interpol; 018 019 import static org.junit.Assert.assertTrue; 020 021 import java.io.File; 022 023 import org.apache.commons.configuration.ConfigurationAssert; 024 import org.apache.commons.configuration.XMLConfiguration; 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 import org.apache.commons.logging.impl.Log4JLogger; 028 import org.apache.log4j.ConsoleAppender; 029 import org.apache.log4j.Level; 030 import org.apache.log4j.Logger; 031 import org.apache.log4j.SimpleLayout; 032 import org.junit.Test; 033 034 /** 035 * Test class for ExprLookup. 036 * 037 * @version $Id: TestExprLookup.java 1225659 2011-12-29 21:12:54Z oheger $ 038 */ 039 public class TestExprLookup 040 { 041 private static File TEST_FILE = ConfigurationAssert.getTestFile("test.xml"); 042 043 private static String PATTERN1 = 044 "String.replace(Util.message, 'Hello', 'Goodbye') + System.getProperty('user.name')"; 045 private static String PATTERN2 = 046 "'$[element] ' + String.trimToEmpty('$[space.description]')"; 047 048 @Test 049 public void testLookup() throws Exception 050 { 051 ConsoleAppender app = new ConsoleAppender(new SimpleLayout()); 052 Log log = LogFactory.getLog("TestLogger"); 053 Logger logger = ((Log4JLogger)log).getLogger(); 054 logger.addAppender(app); 055 logger.setLevel(Level.DEBUG); 056 logger.setAdditivity(false); 057 ExprLookup.Variables vars = new ExprLookup.Variables(); 058 vars.add(new ExprLookup.Variable("String", org.apache.commons.lang.StringUtils.class)); 059 vars.add(new ExprLookup.Variable("Util", new Utility("Hello"))); 060 vars.add(new ExprLookup.Variable("System", "Class:java.lang.System")); 061 XMLConfiguration config = new XMLConfiguration(TEST_FILE); 062 config.setLogger(log); 063 ExprLookup lookup = new ExprLookup(vars); 064 lookup.setConfiguration(config); 065 String str = lookup.lookup(PATTERN1); 066 assertTrue(str.startsWith("Goodbye")); 067 str = lookup.lookup(PATTERN2); 068 assertTrue("Incorrect value: " + str, str.equals("value Some text")); 069 logger.removeAppender(app); 070 071 } 072 073 public static class Utility 074 { 075 String message; 076 077 public Utility(String msg) 078 { 079 this.message = msg; 080 } 081 082 public String getMessage() 083 { 084 return message; 085 } 086 087 public String str(String str) 088 { 089 return str; 090 } 091 } 092 }