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         } else {
175             // TODO assert what for the Slf4j adapter?
176         }
177     }
178 
179     /**
180      * Tests whether the debug status can be queried.
181      */
182     @Test
183     public 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     public 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     public 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     public 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     public 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 }