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