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  
18  package org.apache.commons.configuration2.io;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertNull;
25  import static org.junit.jupiter.api.Assertions.assertSame;
26  import static org.junit.jupiter.api.Assertions.assertThrows;
27  import static org.junit.jupiter.api.Assertions.assertTrue;
28  import static org.mockito.Mockito.mock;
29  import static org.mockito.Mockito.verify;
30  import static org.mockito.Mockito.verifyNoMoreInteractions;
31  import static org.mockito.Mockito.when;
32  
33  import org.apache.commons.configuration2.AbstractConfiguration;
34  import org.apache.commons.configuration2.BaseConfiguration;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.impl.Log4JLogger;
37  import org.apache.commons.logging.impl.NoOpLog;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Test class for {@code ConfigurationLogger}.
42   */
43  public class TestConfigurationLogger {
44  
45      /** Constant for a message to be logged. */
46      private static final String MSG = "Interesting log output";
47  
48      /**
49       * Tests the logger set per default.
50       */
51      @Test
52      void testAbstractConfigurationDefaultLogger() {
53          final AbstractConfiguration config = new BaseConfiguration();
54          assertInstanceOf(NoOpLog.class, config.getLogger().getLog());
55      }
56  
57      /**
58       * Tests whether the logger can be set.
59       */
60      @Test
61      void testAbstractConfigurationSetLogger() {
62          final ConfigurationLogger logger = new ConfigurationLogger(getClass());
63          final AbstractConfiguration config = new BaseConfiguration();
64  
65          config.setLogger(logger);
66          assertSame(logger, config.getLogger());
67      }
68  
69      /**
70       * Tests that the logger can be disabled by setting it to null.
71       */
72      @Test
73      void testAbstractConfigurationSetLoggerNull() {
74          final AbstractConfiguration config = new BaseConfiguration();
75          config.setLogger(new ConfigurationLogger(getClass()));
76  
77          config.setLogger(null);
78          assertInstanceOf(NoOpLog.class, config.getLogger().getLog());
79      }
80  
81      /**
82       * Tests whether debug logging is possible.
83       */
84      @Test
85      void testDebug() {
86          final Log log = mock(Log.class);
87          final ConfigurationLogger logger = new ConfigurationLogger(log);
88  
89          logger.debug(MSG);
90  
91          verify(log).debug(MSG);
92          verifyNoMoreInteractions(log);
93      }
94  
95      /**
96       * Tests whether a dummy logger can be created.
97       */
98      @Test
99      void testDummyLogger() {
100         final ConfigurationLogger logger = ConfigurationLogger.newDummyLogger();
101 
102         assertInstanceOf(NoOpLog.class, logger.getLog());
103     }
104 
105     /**
106      * Tests whether error logging is possible.
107      */
108     @Test
109     void testError() {
110         final Log log = mock(Log.class);
111         final ConfigurationLogger logger = new ConfigurationLogger(log);
112 
113         logger.error(MSG);
114 
115         verify(log).error(MSG);
116         verifyNoMoreInteractions(log);
117     }
118 
119     /**
120      * Tests whether an exception can be logged on error level.
121      */
122     @Test
123     void testErrorWithException() {
124         final Log log = mock(Log.class);
125         final Throwable ex = new Exception("Test exception");
126         final ConfigurationLogger logger = new ConfigurationLogger(log);
127 
128         logger.error(MSG, ex);
129 
130         verify(log).error(MSG, ex);
131         verifyNoMoreInteractions(log);
132     }
133 
134     /**
135      * Tests whether info logging is possible.
136      */
137     @Test
138     void testInfo() {
139         final Log log = mock(Log.class);
140         final ConfigurationLogger logger = new ConfigurationLogger(log);
141 
142         logger.info(MSG);
143 
144         verify(log).info(MSG);
145         verifyNoMoreInteractions(log);
146     }
147 
148     /**
149      * Tries to create an instance without passing a logger class.
150      */
151     @Test
152     void testInitNoLoggerClass() {
153         assertThrows(IllegalArgumentException.class, () -> new ConfigurationLogger((Class<?>) null));
154     }
155 
156     /**
157      * Tries to create an instance without passing in a logger name.
158      */
159     @Test
160     void testInitNoLoggerName() {
161         assertThrows(IllegalArgumentException.class, () -> new ConfigurationLogger((String) null));
162     }
163 
164     /**
165      * Tests whether a correct internal logger is created.
166      */
167     @Test
168     void testInitWithLoggerSpec() {
169         final ConfigurationLogger logger1 = new ConfigurationLogger(getClass().getName());
170         final ConfigurationLogger logger2 = new ConfigurationLogger(getClass());
171 
172         assertNotNull(logger1.getLog());
173         if (logger1.getLog() instanceof Log4JLogger) {
174             assertEquals(logger1.getLog(), logger2.getLog());
175         }
176         // else: TODO assert what for the Slf4j adapter?
177     }
178 
179     /**
180      * Tests whether the debug status can be queried.
181      */
182     @Test
183     void testIsDebugEnabled() {
184         final Log log = mock(Log.class);
185 
186         when(log.isDebugEnabled()).thenReturn(Boolean.TRUE);
187 
188         final ConfigurationLogger logger = new ConfigurationLogger(log);
189 
190         assertTrue(logger.isDebugEnabled());
191 
192         verify(log).isDebugEnabled();
193         verifyNoMoreInteractions(log);
194     }
195 
196     /**
197      * Tests whether the info status can be queried.
198      */
199     @Test
200     void testIsInfoEnabled() {
201         final Log log = mock(Log.class);
202 
203         when(log.isInfoEnabled()).thenReturn(Boolean.FALSE);
204 
205         final ConfigurationLogger logger = new ConfigurationLogger(log);
206 
207         assertFalse(logger.isInfoEnabled());
208 
209         verify(log).isInfoEnabled();
210         verifyNoMoreInteractions(log);
211     }
212 
213     /**
214      * Tests that a derived class can be created for a logger.
215      */
216     @Test
217     void testSubClass() {
218         final StringBuilder buf = new StringBuilder();
219         final ConfigurationLogger logger = new ConfigurationLogger() {
220             @Override
221             public void info(final String msg) {
222                 buf.append(msg);
223             }
224         };
225 
226         assertNull(logger.getLog());
227         logger.info(MSG);
228         assertEquals(MSG, buf.toString());
229     }
230 
231     /**
232      * Tests whether warn logging is possible.
233      */
234     @Test
235     void testWarn() {
236         final Log log = mock(Log.class);
237         final ConfigurationLogger logger = new ConfigurationLogger(log);
238 
239         logger.warn(MSG);
240 
241         verify(log).warn(MSG);
242         verifyNoMoreInteractions(log);
243     }
244 
245     /**
246      * Tests whether an exception can be logged on warn level.
247      */
248     @Test
249     void testWarnWithException() {
250         final Log log = mock(Log.class);
251         final Throwable ex = new Exception("Test exception");
252         final ConfigurationLogger logger = new ConfigurationLogger(log);
253 
254         logger.warn(MSG, ex);
255 
256         verify(log).warn(MSG, ex);
257         verifyNoMoreInteractions(log);
258     }
259 }