1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
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 }