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