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.log4j;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.log4j.util.SerializationTestHelper;
23  import java.util.Locale;
24  
25  
26  /***
27   * Tests of Level.
28   *
29   * @author Curt Arnold
30   * @since 1.2.12
31   */
32  public class LevelTest extends TestCase {
33    /***
34     * Constructs new instance of test.
35     * @param name test name.
36     */
37    public LevelTest(final String name) {
38      super(name);
39    }
40  
41    /***
42     * Serialize Level.INFO and check against witness.
43     * @throws Exception if exception during test.
44     *
45     */
46    public void testSerializeINFO() throws Exception {
47      int[] skip = new int[] {  };
48      SerializationTestHelper.assertSerializationEquals(
49        "witness/serialization/info.bin", Level.INFO, skip, Integer.MAX_VALUE);
50    }
51  
52    /***
53     * Deserialize witness and see if resolved to Level.INFO.
54     * @throws Exception if exception during test.
55     */
56    public void testDeserializeINFO() throws Exception {
57      Object obj =
58        SerializationTestHelper.deserializeStream(
59          "witness/serialization/info.bin");
60      assertTrue(obj instanceof Level);
61      Level info = (Level) obj;
62      assertEquals("INFO", info.toString());
63      //
64      //  JDK 1.1 doesn't support readResolve necessary for the assertion
65      if (!System.getProperty("java.version").startsWith("1.1.")) {
66         assertTrue(obj == Level.INFO);
67      }
68    }
69  
70    /***
71     * Tests that a custom level can be serialized and deserialized
72     * and is not resolved to a stock level.
73     *
74     * @throws Exception if exception during test.
75     */
76    public void testCustomLevelSerialization() throws Exception {
77      CustomLevel custom = new CustomLevel();
78      Object obj = SerializationTestHelper.serializeClone(custom);
79      assertTrue(obj instanceof CustomLevel);
80  
81      CustomLevel clone = (CustomLevel) obj;
82      assertEquals(Level.INFO.level, clone.level);
83      assertEquals(Level.INFO.levelStr, clone.levelStr);
84      assertEquals(Level.INFO.syslogEquivalent, clone.syslogEquivalent);
85    }
86  
87    /***
88     * Custom level to check that custom levels are
89     * serializable, but not resolved to a plain Level.
90     */
91    private static class CustomLevel extends Level {
92        /***
93         * Create an instance of CustomLevel.
94         */
95      public CustomLevel() {
96        super(
97          Level.INFO.level, Level.INFO.levelStr, Level.INFO.syslogEquivalent);
98      }
99    }
100 
101     /***
102      * Tests Level.TRACE_INT.
103      */
104   public void testTraceInt() {
105       assertEquals(5000, Level.TRACE_INT);
106   }
107 
108     /***
109      * Tests Level.TRACE.
110      */
111   public void testTrace() {
112       assertEquals("TRACE", Level.TRACE.toString());
113       assertEquals(5000, Level.TRACE.toInt());
114       assertEquals(7, Level.TRACE.getSyslogEquivalent());
115   }
116 
117     /***
118      * Tests Level.toLevel(Level.TRACE_INT).
119      */
120   public void testIntToTrace() {
121       Level trace = Level.toLevel(5000);
122       assertEquals("TRACE", trace.toString());
123   }
124 
125     /***
126      * Tests Level.toLevel("TRACE");
127      */
128   public void testStringToTrace() {
129         Level trace = Level.toLevel("TRACE");
130         assertEquals("TRACE", trace.toString());
131   }
132 
133     /***
134      * Tests that Level extends Priority.
135      */
136   public void testLevelExtendsPriority() {
137       assertTrue(Priority.class.isAssignableFrom(Level.class));
138   }
139 
140     /***
141      * Tests Level.OFF.
142      */
143   public void testOFF() {
144     assertTrue(Level.OFF instanceof Level);
145   }
146 
147     /***
148      * Tests Level.FATAL.
149      */
150     public void testFATAL() {
151       assertTrue(Level.FATAL instanceof Level);
152     }
153 
154     /***
155      * Tests Level.ERROR.
156      */
157     public void testERROR() {
158       assertTrue(Level.ERROR instanceof Level);
159     }
160 
161     /***
162      * Tests Level.WARN.
163      */
164     public void testWARN() {
165       assertTrue(Level.WARN instanceof Level);
166     }
167 
168     /***
169      * Tests Level.INFO.
170      */
171     public void testINFO() {
172       assertTrue(Level.INFO instanceof Level);
173     }
174 
175     /***
176      * Tests Level.DEBUG.
177      */
178     public void testDEBUG() {
179       assertTrue(Level.DEBUG instanceof Level);
180     }
181 
182     /***
183      * Tests Level.TRACE.
184      */
185     public void testTRACE() {
186       assertTrue(Level.TRACE instanceof Level);
187     }
188 
189     /***
190      * Tests Level.ALL.
191      */
192     public void testALL() {
193       assertTrue(Level.ALL instanceof Level);
194     }
195 
196     /***
197      * Tests Level.serialVersionUID.
198      */
199     public void testSerialVersionUID() {
200       assertEquals(3491141966387921974L, Level.serialVersionUID);
201     }
202 
203     /***
204      * Tests Level.toLevel(Level.All_INT).
205      */
206   public void testIntToAll() {
207       Level level = Level.toLevel(Level.ALL_INT);
208       assertEquals("ALL", level.toString());
209   }
210 
211     /***
212      * Tests Level.toLevel(Level.FATAL_INT).
213      */
214   public void testIntToFatal() {
215       Level level = Level.toLevel(Level.FATAL_INT);
216       assertEquals("FATAL", level.toString());
217   }
218 
219 
220     /***
221      * Tests Level.toLevel(Level.OFF_INT).
222      */
223   public void testIntToOff() {
224       Level level = Level.toLevel(Level.OFF_INT);
225       assertEquals("OFF", level.toString());
226   }
227 
228     /***
229      * Tests Level.toLevel(17, Level.FATAL).
230      */
231   public void testToLevelUnrecognizedInt() {
232       Level level = Level.toLevel(17, Level.FATAL);
233       assertEquals("FATAL", level.toString());
234   }
235 
236     /***
237      * Tests Level.toLevel(null, Level.FATAL).
238      */
239   public void testToLevelNull() {
240       Level level = Level.toLevel(null, Level.FATAL);
241       assertEquals("FATAL", level.toString());
242   }
243 
244     /***
245      * Test that dotless lower I + "nfo" is recognized as INFO.
246      */
247   public void testDotlessLowerI() {
248       Level level = Level.toLevel("\u0131nfo");
249       assertEquals("INFO", level.toString());
250   }
251 
252     /***
253      * Test that dotted lower I + "nfo" is recognized as INFO
254      * even in Turkish locale.
255      */
256   public void testDottedLowerI() {
257       Locale defaultLocale = Locale.getDefault();
258       Locale turkey = new Locale("tr", "TR");
259       Locale.setDefault(turkey);
260       Level level = Level.toLevel("info");
261       Locale.setDefault(defaultLocale);
262       assertEquals("INFO", level.toString());
263   }
264 
265 
266 }