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    *     https://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.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   * Test class for ExprLookup.
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       * Loads the test configuration.
69       *
70       * @return the test configuration
71       * @throws ConfigurationException if an error occurs
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       * Tests whether variables can be queried.
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       * Tests that getVariables() returns a copy of the original variables.
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         //final Logger logger = ((Log4JLogger) log).getLogger();
109         //logger.addAppender(app);
110         //logger.setLevel(Level.DEBUG);
111         //logger.setAdditivity(false);
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         //logger.removeAppender(app);
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      * Tests a lookup() operation if no ConfigurationInterpolator object has been set.
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      * Tests an expression that does not yield a string.
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      * Tests an expression that yields a null value.
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 }