KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > logging > log4j > XLevelTest


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.geronimo.system.logging.log4j;
18
19 import junit.framework.TestCase;
20 import org.apache.log4j.Level;
21
22 /**
23  * Tests the {@link XLevel} class.
24  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
25  */

26 public final class XLevelTest extends TestCase {
27     private static final int TRACE_SYSLOG_EQ = 7;
28     private static final String JavaDoc TRACE_STRING = "TRACE";
29     private static final int TRACE_INT = Level.DEBUG_INT - 1;
30     private static final int MOCK_INT = TRACE_INT - 99;
31
32     private final static class MockLevel extends Level {
33         MockLevel() {
34             super(MOCK_INT, "MOCK", 99);
35         }
36     }
37
38     private final static Level MOCK = new MockLevel();
39
40     /**
41      * Tests the constants declared in the {@link XLevel}.
42      * The {@link XLevel#TRACE_INT} value is tested to be defined as {@link Level#DEBUG_INT}-1.
43      * The {@link XLevel#TRACE} value is tested to be an instance of {@link XLevel},
44      * have a name "TRACE" and syslog level equivalent of <code>7</code>.
45      */

46     public void testConstants() {
47         assertEquals("XLevel.TRACE_INT is defined as Level.DEBUG_INT-1", XLevel.TRACE_INT, TRACE_INT);
48         final Object JavaDoc o = XLevel.TRACE;
49         assertTrue("XLevel.TRACE is an instance of XLevel", o instanceof XLevel);
50         final XLevel traceLevel = (XLevel) o;
51         assertEquals("XLevel.TRACE syslog equivalent is indeed " + TRACE_SYSLOG_EQ, traceLevel.getSyslogEquivalent(), TRACE_SYSLOG_EQ);
52         assertEquals("XLevel.TRACE name is TRACE", traceLevel.toString(), TRACE_STRING);
53         assertEquals("XLevel.TRACE int level is " + TRACE_INT, traceLevel.toInt(), TRACE_INT);
54     }
55
56     /**
57      * Tests {@link XLevel#toLevel(java.lang.String, org.apache.log4j.Level) method.
58      * Tests that default value is returned if null value is passed.
59      * Tests that {@link XLevel#TRACE} is returned if the name is equal to "TRACE",
60      * irrespective of case.
61      * Tests that in all other cases the conversion is deferred to {@link Level#toLevel(java.lang.String, org.apache.log4j.Level)}.
62      * Tests that if conversion fails the specified default value is returned.
63      */

64     public void testToLevelWithDefault() {
65         assertSame("Default value is indeed returned if null name is passed", XLevel.toLevel(null, MOCK), MOCK);
66         assertSame("XLevel.TRACE is returned if the name is equal to \"TRACE\" irrespecctive of case", XLevel.toLevel("trAce"), XLevel.TRACE);
67         Level levelResults = Level.toLevel("MOCK", MOCK);
68         Level xLevelResults = XLevel.toLevel("MOCK", MOCK);
69         assertSame("In all other cases conversion is deferred to Level", xLevelResults, levelResults);
70         assertSame("If conversion fails the default value is returned", xLevelResults, MOCK);
71     }
72
73     /**
74      * Tests {@link XLevel#toLevel(java.lang.String) method.
75      * Tests that this method simply delegates the work to
76      * {@link XLevel#toLevel(java.lang.String, org.apache.log4j.Level) with default value
77      * specified as {@link XLevel#TRACE}.
78      */

79     public void testToLevel() {
80         assertSame("XLevel.TRACE is returned if null name is passed", XLevel.toLevel(null), XLevel.TRACE);
81         assertSame("XLevel.TRACE is returned if the name is equal to \"TRACE\" irrespective of case", XLevel.toLevel("trAce"), XLevel.TRACE);
82         Level levelResults = Level.toLevel("MOCK", XLevel.TRACE);
83         Level xLevelResults = XLevel.toLevel("MOCK");
84         assertSame("In all other cases conversion is deferred to Level", xLevelResults, levelResults);
85         assertSame("If conversion fails XLevel.TRACE is returned", xLevelResults, XLevel.TRACE);
86     }
87
88     /**
89      * Tests {@link XLevel#toLevel(int) method.
90      * Tests that if level is equal to {@link XLevel#TRACE_INT} {@link XLevel#TRACE} is returned.
91      * Tests that in other cases the conversion is deferred to {@link Level#toLevel(int)}
92      * Tests that if conversion fails the {@link Level#DEBUG} is returned.
93      */

94     public void testToLevelInt() {
95         assertSame("XLevel.TRACE is returned is " + TRACE_INT + "is passed", XLevel.toLevel(TRACE_INT), XLevel.TRACE);
96         Level levelResults = Level.toLevel(MOCK_INT);
97         Level xLevelResults = XLevel.toLevel(MOCK_INT);
98         assertSame("In all other cases conversion is deferred to Level", xLevelResults, levelResults);
99         assertSame("If conversion fails Level.DEBUG is returned", xLevelResults, Level.DEBUG);
100     }
101
102 }
103
Popular Tags