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.logging;
18  
19  import junit.framework.TestCase;
20  
21  /**
22   * Tests the basic logging operations to ensure that they all function
23   * without exception failure. In other words, that they do no fail by
24   * throwing exceptions.
25   * This is the minimum requirement for any well behaved logger
26   * and so this test should be run for each kind.
27   */
28  public class BasicOperationsTestCase extends TestCase {
29  
30      public void executeIsEnabledTest(final Log log) {
31          try {
32              log.isTraceEnabled();
33              log.isDebugEnabled();
34              log.isInfoEnabled();
35              log.isWarnEnabled();
36              log.isErrorEnabled();
37              log.isFatalEnabled();
38          } catch (final Throwable t) {
39              t.printStackTrace();
40              fail("Exception thrown: " + t);
41          }
42      }
43  
44      public void executeMessageWithExceptionTest(final Log log) {
45          try {
46              log.trace("Hello, Mum", new ArithmeticException());
47              log.debug("Hello, Mum", new ArithmeticException());
48              log.info("Hello, Mum", new ArithmeticException());
49              log.warn("Hello, Mum", new ArithmeticException());
50              log.error("Hello, Mum", new ArithmeticException());
51              log.fatal("Hello, Mum", new ArithmeticException());
52          } catch (final Throwable t) {
53              t.printStackTrace();
54              fail("Exception thrown: " + t);
55          }
56      }
57  
58      public void executeMessageWithoutExceptionTest(final Log log) {
59          try {
60              log.trace("Hello, Mum");
61              log.debug("Hello, Mum");
62              log.info("Hello, Mum");
63              log.warn("Hello, Mum");
64              log.error("Hello, Mum");
65              log.fatal("Hello, Mum");
66          } catch (final Throwable t) {
67              t.printStackTrace();
68              fail("Exception thrown: " + t);
69          }
70      }
71  
72      public void testIsEnabledClassLog() {
73          final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
74          executeIsEnabledTest(log);
75      }
76  
77      public void testIsEnabledNamedLog() {
78          final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
79          executeIsEnabledTest(log);
80      }
81  
82      public void testMessageWithExceptionClassLog() {
83          final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
84          executeMessageWithExceptionTest(log);
85      }
86  
87      public void testMessageWithExceptionNamedLog() {
88          final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
89          executeMessageWithExceptionTest(log);
90      }
91  
92      public void testMessageWithoutExceptionClassLog() {
93          final Log log = LogFactory.getLog(BasicOperationsTestCase.class);
94          executeMessageWithoutExceptionTest(log);
95      }
96  
97      public void testMessageWithoutExceptionNamedLog() {
98          final Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
99          executeMessageWithoutExceptionTest(log);
100     }
101 }