LevelTest xref

    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      private static final long serialVersionUID = 1L;
    93        /**
    94         * Create an instance of CustomLevel.
    95         */
    96      public CustomLevel() {
    97        super(
    98          Level.INFO.level, Level.INFO.levelStr, Level.INFO.syslogEquivalent);
    99      }
    100   }
    101
    102     /**
    103      * Tests Level.TRACE_INT.
    104      */
    105   public void testTraceInt() {
    106       assertEquals(5000, Level.TRACE_INT);
    107   }
    108
    109     /**
    110      * Tests Level.TRACE.
    111      */
    112   public void testTrace() {
    113       assertEquals("TRACE", Level.TRACE.toString());
    114       assertEquals(5000, Level.TRACE.toInt());
    115       assertEquals(7, Level.TRACE.getSyslogEquivalent());
    116   }
    117
    118     /**
    119      * Tests Level.toLevel(Level.TRACE_INT).
    120      */
    121   public void testIntToTrace() {
    122       Level trace = Level.toLevel(5000);
    123       assertEquals("TRACE", trace.toString());
    124   }
    125
    126     /**
    127      * Tests Level.toLevel("TRACE");
    128      */
    129   public void testStringToTrace() {
    130         Level trace = Level.toLevel("TRACE");
    131         assertEquals("TRACE", trace.toString());
    132   }
    133
    134     /**
    135      * Tests that Level extends Priority.
    136      */
    137   public void testLevelExtendsPriority() {
    138       assertTrue(Priority.class.isAssignableFrom(Level.class));
    139   }
    140
    141     /**
    142      * Tests Level.OFF.
    143      */
    144   public void testOFF() {
    145     assertTrue(Level.OFF instanceof Level);
    146   }
    147
    148     /**
    149      * Tests Level.FATAL.
    150      */
    151     public void testFATAL() {
    152       assertTrue(Level.FATAL instanceof Level);
    153     }
    154
    155     /**
    156      * Tests Level.ERROR.
    157      */
    158     public void testERROR() {
    159       assertTrue(Level.ERROR instanceof Level);
    160     }
    161
    162     /**
    163      * Tests Level.WARN.
    164      */
    165     public void testWARN() {
    166       assertTrue(Level.WARN instanceof Level);
    167     }
    168
    169     /**
    170      * Tests Level.INFO.
    171      */
    172     public void testINFO() {
    173       assertTrue(Level.INFO instanceof Level);
    174     }
    175
    176     /**
    177      * Tests Level.DEBUG.
    178      */
    179     public void testDEBUG() {
    180       assertTrue(Level.DEBUG instanceof Level);
    181     }
    182
    183     /**
    184      * Tests Level.TRACE.
    185      */
    186     public void testTRACE() {
    187       assertTrue(Level.TRACE instanceof Level);
    188     }
    189
    190     /**
    191      * Tests Level.ALL.
    192      */
    193     public void testALL() {
    194       assertTrue(Level.ALL instanceof Level);
    195     }
    196
    197     /**
    198      * Tests Level.serialVersionUID.
    199      */
    200     public void testSerialVersionUID() {
    201       assertEquals(3491141966387921974L, Level.serialVersionUID);
    202     }
    203
    204     /**
    205      * Tests Level.toLevel(Level.All_INT).
    206      */
    207   public void testIntToAll() {
    208       Level level = Level.toLevel(Level.ALL_INT);
    209       assertEquals("ALL", level.toString());
    210   }
    211
    212     /**
    213      * Tests Level.toLevel(Level.FATAL_INT).
    214      */
    215   public void testIntToFatal() {
    216       Level level = Level.toLevel(Level.FATAL_INT);
    217       assertEquals("FATAL", level.toString());
    218   }
    219
    220
    221     /**
    222      * Tests Level.toLevel(Level.OFF_INT).
    223      */
    224   public void testIntToOff() {
    225       Level level = Level.toLevel(Level.OFF_INT);
    226       assertEquals("OFF", level.toString());
    227   }
    228
    229     /**
    230      * Tests Level.toLevel(17, Level.FATAL).
    231      */
    232   public void testToLevelUnrecognizedInt() {
    233       Level level = Level.toLevel(17, Level.FATAL);
    234       assertEquals("FATAL", level.toString());
    235   }
    236
    237     /**
    238      * Tests Level.toLevel(null, Level.FATAL).
    239      */
    240   public void testToLevelNull() {
    241       Level level = Level.toLevel(null, Level.FATAL);
    242       assertEquals("FATAL", level.toString());
    243   }
    244
    245     /**
    246      * Test that dotless lower I + "nfo" is recognized as INFO.
    247      */
    248   public void testDotlessLowerI() {
    249       Level level = Level.toLevel("\u0131nfo");
    250       assertEquals("INFO", level.toString());
    251   }
    252
    253     /**
    254      * Test that dotted lower I + "nfo" is recognized as INFO
    255      * even in Turkish locale.
    256      */
    257   public void testDottedLowerI() {
    258       Locale defaultLocale = Locale.getDefault();
    259       Locale turkey = new Locale("tr", "TR");
    260       Locale.setDefault(turkey);
    261       Level level = Level.toLevel("info");
    262       Locale.setDefault(defaultLocale);
    263       assertEquals("INFO", level.toString());
    264   }
    265
    266
    267 }