View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.interpol;
18  
19  import static org.junit.Assert.assertTrue;
20  
21  import java.io.File;
22  
23  import org.apache.commons.configuration.ConfigurationAssert;
24  import org.apache.commons.configuration.XMLConfiguration;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.commons.logging.impl.Log4JLogger;
28  import org.apache.log4j.ConsoleAppender;
29  import org.apache.log4j.Level;
30  import org.apache.log4j.Logger;
31  import org.apache.log4j.SimpleLayout;
32  import org.junit.Test;
33  
34  /**
35   * Test class for ExprLookup.
36   *
37   * @version $Id: TestExprLookup.java 1225659 2011-12-29 21:12:54Z oheger $
38   */
39  public class TestExprLookup
40  {
41      private static File TEST_FILE = ConfigurationAssert.getTestFile("test.xml");
42  
43      private static String PATTERN1 =
44          "String.replace(Util.message, 'Hello', 'Goodbye') + System.getProperty('user.name')";
45      private static String PATTERN2 =
46          "'$[element] ' + String.trimToEmpty('$[space.description]')";
47  
48      @Test
49      public void testLookup() throws Exception
50      {
51          ConsoleAppender app = new ConsoleAppender(new SimpleLayout());
52          Log log = LogFactory.getLog("TestLogger");
53          Logger logger = ((Log4JLogger)log).getLogger();
54          logger.addAppender(app);
55          logger.setLevel(Level.DEBUG);
56          logger.setAdditivity(false);
57          ExprLookup.Variables vars = new ExprLookup.Variables();
58          vars.add(new ExprLookup.Variable("String", org.apache.commons.lang.StringUtils.class));
59          vars.add(new ExprLookup.Variable("Util", new Utility("Hello")));
60          vars.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
61          XMLConfiguration config = new XMLConfiguration(TEST_FILE);
62          config.setLogger(log);
63          ExprLookup lookup = new ExprLookup(vars);
64          lookup.setConfiguration(config);
65          String str = lookup.lookup(PATTERN1);
66          assertTrue(str.startsWith("Goodbye"));
67          str = lookup.lookup(PATTERN2);
68          assertTrue("Incorrect value: " + str, str.equals("value Some text"));
69          logger.removeAppender(app);
70  
71      }
72  
73      public static class Utility
74      {
75          String message;
76  
77          public Utility(String msg)
78          {
79              this.message = msg;
80          }
81  
82          public String getMessage()
83          {
84              return message;
85          }
86  
87          public String str(String str)
88          {
89              return str;
90          }
91      }
92  }