1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.interpol;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotEquals;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.File;
25
26 import org.apache.commons.configuration2.ConfigurationAssert;
27 import org.apache.commons.configuration2.XMLConfiguration;
28 import org.apache.commons.configuration2.ex.ConfigurationException;
29 import org.apache.commons.configuration2.io.ConfigurationLogger;
30 import org.apache.commons.configuration2.io.FileHandler;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.commons.logging.impl.Log4JLogger;
34 import org.apache.log4j.ConsoleAppender;
35 import org.apache.log4j.Level;
36 import org.apache.log4j.Logger;
37 import org.apache.log4j.SimpleLayout;
38 import org.junit.jupiter.api.Test;
39
40
41
42
43 public class TestExprLookup {
44 public static class Utility {
45 String message;
46
47 public Utility(final String msg) {
48 this.message = msg;
49 }
50
51 public String getMessage() {
52 return message;
53 }
54
55 public String str(final String str) {
56 return str;
57 }
58 }
59
60 private static final String PATTERN1 = "String.replace(Util.message, 'Hello', 'Goodbye') + System.getProperty('user.name')";
61 private static final String PATTERN2 = "'$[element] ' + String.trimToEmpty('$[space.description]')";
62
63 private static final File TEST_FILE = ConfigurationAssert.getTestFile("test.xml");
64
65
66
67
68
69
70
71 private static XMLConfiguration loadConfig() throws ConfigurationException {
72 final XMLConfiguration config = new XMLConfiguration();
73 final FileHandler handler = new FileHandler(config);
74 handler.load(TEST_FILE);
75 return config;
76 }
77
78
79
80
81 @Test
82 public void testGetVariables() {
83 final ExprLookup.Variables vars = new ExprLookup.Variables();
84 vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
85 final ExprLookup lookup = new ExprLookup(vars);
86 assertEquals(vars, lookup.getVariables());
87 }
88
89
90
91
92 @Test
93 public void testGetVariablesDefensiveCopy() {
94 final ExprLookup.Variables vars = new ExprLookup.Variables();
95 vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
96 final ExprLookup lookup = new ExprLookup(vars);
97 final ExprLookup.Variables vars2 = lookup.getVariables();
98 vars2.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
99 assertEquals(vars, lookup.getVariables());
100 }
101
102 @Test
103 public void testLookup() throws Exception {
104 final ConsoleAppender app = new ConsoleAppender(new SimpleLayout());
105 final Log log = LogFactory.getLog("TestLogger");
106
107
108
109
110 final ExprLookup.Variables vars = new ExprLookup.Variables();
111 vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
112 vars.add(new ExprLookup.Variable("Util", new Utility("Hello")));
113 vars.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
114 final XMLConfiguration config = loadConfig();
115 final ConfigurationLogger testLogger = new ConfigurationLogger("TestLogger");
116 config.setLogger(testLogger);
117 final ExprLookup lookup = new ExprLookup(vars);
118 lookup.setInterpolator(config.getInterpolator());
119 lookup.setLogger(testLogger);
120 String str = lookup.lookup(PATTERN1);
121 assertTrue(str.startsWith("Goodbye"));
122 str = lookup.lookup(PATTERN2);
123 assertEquals("value Some text", str);
124
125 }
126
127 @Test
128 public void testLookupLog4j1() throws Exception {
129 final ConsoleAppender app = new ConsoleAppender(new SimpleLayout());
130 final Log log = LogFactory.getLog("TestLogger");
131 if (log instanceof Log4JLogger) {
132 final Logger logger = ((Log4JLogger) log).getLogger();
133 logger.addAppender(app);
134 logger.setLevel(Level.DEBUG);
135 logger.setAdditivity(false);
136 final ExprLookup.Variables vars = new ExprLookup.Variables();
137 vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
138 vars.add(new ExprLookup.Variable("Util", new Utility("Hello")));
139 vars.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
140 final XMLConfiguration config = loadConfig();
141 final ConfigurationLogger testLogger = new ConfigurationLogger("TestLogger");
142 config.setLogger(testLogger);
143 final ExprLookup lookup = new ExprLookup(vars);
144 lookup.setInterpolator(config.getInterpolator());
145 lookup.setLogger(testLogger);
146 String str = lookup.lookup(PATTERN1);
147 assertTrue(str.startsWith("Goodbye"));
148 str = lookup.lookup(PATTERN2);
149 assertEquals("value Some text", str);
150 logger.removeAppender(app);
151 }
152 }
153
154
155
156
157 @Test
158 public void testLookupNoConfigurationInterpolator() {
159 final ExprLookup.Variables vars = new ExprLookup.Variables();
160 vars.add(new ExprLookup.Variable("String", org.apache.commons.lang3.StringUtils.class));
161 final ExprLookup lookup = new ExprLookup(vars);
162 final String value = "test";
163 assertEquals(value, lookup.lookup(value));
164 }
165
166
167
168
169 @Test
170 public void testLookupNonStringExpression() throws ConfigurationException {
171 final ExprLookup.Variables vars = new ExprLookup.Variables();
172 vars.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
173 final ExprLookup lookup = new ExprLookup(vars);
174 final XMLConfiguration config = loadConfig();
175 lookup.setInterpolator(config.getInterpolator());
176 final String pattern = "System.currentTimeMillis()";
177 final String result = lookup.lookup(pattern);
178 assertNotEquals(pattern, result);
179 }
180
181
182
183
184 @Test
185 public void testLookupNullExpression() throws ConfigurationException {
186 final ExprLookup.Variables vars = new ExprLookup.Variables();
187 vars.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
188 final ExprLookup lookup = new ExprLookup(vars);
189 final XMLConfiguration config = loadConfig();
190 lookup.setInterpolator(config.getInterpolator());
191 assertNull(lookup.lookup("System.getProperty('undefined.property')"));
192 }
193 }