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