001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.logging;
019
020 import junit.framework.TestCase;
021
022 /**
023 * Tests the basic logging operations to ensure that they all function
024 * without exception failure. In other words, that they do no fail by
025 * throwing exceptions.
026 * This is the minimum requirement for any well behaved logger
027 * and so this test should be run for each kind.
028 */
029 public class BasicOperationsTestCase extends TestCase
030 {
031 public void testIsEnabledClassLog()
032 {
033 Log log = LogFactory.getLog(BasicOperationsTestCase.class);
034 executeIsEnabledTest(log);
035 }
036
037 public void testIsEnabledNamedLog()
038 {
039 Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
040 executeIsEnabledTest(log);
041 }
042
043 public void executeIsEnabledTest(Log log)
044 {
045 try
046 {
047 log.isTraceEnabled();
048 log.isDebugEnabled();
049 log.isInfoEnabled();
050 log.isWarnEnabled();
051 log.isErrorEnabled();
052 log.isFatalEnabled();
053 }
054 catch (Throwable t)
055 {
056 t.printStackTrace();
057 fail("Exception thrown: " + t);
058 }
059 }
060
061
062 public void testMessageWithoutExceptionClassLog()
063 {
064 Log log = LogFactory.getLog(BasicOperationsTestCase.class);
065 executeMessageWithoutExceptionTest(log);
066 }
067
068 public void testMessageWithoutExceptionNamedLog()
069 {
070 Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
071 executeMessageWithoutExceptionTest(log);
072 }
073
074 public void executeMessageWithoutExceptionTest(Log log)
075 {
076 try
077 {
078 log.trace("Hello, Mum");
079 log.debug("Hello, Mum");
080 log.info("Hello, Mum");
081 log.warn("Hello, Mum");
082 log.error("Hello, Mum");
083 log.fatal("Hello, Mum");
084 }
085 catch (Throwable t)
086 {
087 t.printStackTrace();
088 fail("Exception thrown: " + t);
089 }
090 }
091
092 public void testMessageWithExceptionClassLog()
093 {
094 Log log = LogFactory.getLog(BasicOperationsTestCase.class);
095 executeMessageWithExceptionTest(log);
096 }
097
098 public void testMessageWithExceptionNamedLog()
099 {
100 Log log = LogFactory.getLog(BasicOperationsTestCase.class.getName());
101 executeMessageWithExceptionTest(log);
102 }
103
104 public void executeMessageWithExceptionTest(Log log)
105 {
106 try
107 {
108 log.trace("Hello, Mum", new ArithmeticException());
109 log.debug("Hello, Mum", new ArithmeticException());
110 log.info("Hello, Mum", new ArithmeticException());
111 log.warn("Hello, Mum", new ArithmeticException());
112 log.error("Hello, Mum", new ArithmeticException());
113 log.fatal("Hello, Mum", new ArithmeticException());
114 }
115 catch (Throwable t)
116 {
117 t.printStackTrace();
118 fail("Exception thrown: " + t);
119 }
120 }
121 }