LoggingEventTest 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.spi;
    19
    20  import junit.framework.TestCase;
    21
    22  import org.apache.log4j.Level;
    23  import org.apache.log4j.Logger;
    24  import org.apache.log4j.MDC;
    25  import org.apache.log4j.NDC;
    26  import org.apache.log4j.util.SerializationTestHelper;
    27  import org.apache.log4j.Priority;
    28  import org.apache.log4j.Category;
    29
    30
    31  /**
    32   * Tests LoggingEvent.
    33   *
    34   * @author Curt Arnold
    35   */
    36  public class LoggingEventTest extends TestCase {
    37    /**
    38     * Create LoggingEventTest.
    39     *
    40     * @param name test name.
    41     */
    42    public LoggingEventTest(final String name) {
    43      super(name);
    44    }
    45
    46    /**
    47     * Serialize a simple logging event and check it against
    48     * a witness.
    49     * @throws Exception if exception during test.
    50     */
    51    public void testSerializationSimple() throws Exception {
    52      Logger root = Logger.getRootLogger();
    53      LoggingEvent event =
    54        new LoggingEvent(
    55          root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
    56  //    event.prepareForDeferredProcessing();
    57
    58      int[] skip = new int[] { 352, 353, 354, 355, 356 };
    59      SerializationTestHelper.assertSerializationEquals(
    60        "witness/serialization/simple.bin", event, skip, 237);
    61    }
    62
    63    /**
    64     * Serialize a logging event with an exception and check it against
    65     * a witness.
    66     * @throws Exception if exception during test.
    67     *
    68     */
    69    public void testSerializationWithException() throws Exception {
    70      Logger root = Logger.getRootLogger();
    71      Exception ex = new Exception("Don't panic");
    72      LoggingEvent event =
    73        new LoggingEvent(
    74          root.getClass().getName(), root, Level.INFO, "Hello, world.", ex);
    75  //    event.prepareForDeferredProcessing();
    76
    77      int[] skip = new int[] { 352, 353, 354, 355, 356 };
    78      SerializationTestHelper.assertSerializationEquals(
    79        "witness/serialization/exception.bin", event, skip, 237);
    80    }
    81
    82    /**
    83     * Serialize a logging event with an exception and check it against
    84     * a witness.
    85     * @throws Exception if exception during test.
    86     *
    87     */
    88    public void testSerializationWithLocation() throws Exception {
    89      Logger root = Logger.getRootLogger();
    90      LoggingEvent event =
    91        new LoggingEvent(
    92          root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
    93      event.getLocationInformation();
    94  //    event.prepareForDeferredProcessing();
    95
    96      int[] skip = new int[] { 352, 353, 354, 355, 356 };
    97      SerializationTestHelper.assertSerializationEquals(
    98        "witness/serialization/location.bin", event, skip, 237);
    99    }
    100
    101   /**
    102    * Serialize a logging event with ndc.
    103    * @throws Exception if exception during test.
    104    *
    105    */
    106   public void testSerializationNDC() throws Exception {
    107     Logger root = Logger.getRootLogger();
    108     NDC.push("ndc test");
    109
    110     LoggingEvent event =
    111       new LoggingEvent(
    112         root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
    113 //    event.prepareForDeferredProcessing();
    114
    115     int[] skip = new int[] { 352, 353, 354, 355, 356 };
    116     SerializationTestHelper.assertSerializationEquals(
    117       "witness/serialization/ndc.bin", event, skip, 237);
    118     }
    119
    120   /**
    121    * Serialize a logging event with mdc.
    122    * @throws Exception if exception during test.
    123    *
    124    */
    125   public void testSerializationMDC() throws Exception {
    126     Logger root = Logger.getRootLogger();
    127     MDC.put("mdckey", "mdcvalue");
    128
    129     LoggingEvent event =
    130       new LoggingEvent(
    131         root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
    132 //    event.prepareForDeferredProcessing();
    133
    134     int[] skip = new int[] { 352, 353, 354, 355, 356 };
    135     SerializationTestHelper.assertSerializationEquals(
    136       "witness/serialization/mdc.bin", event, skip, 237);
    137   }
    138
    139   /**
    140    * Deserialize a simple logging event.
    141    * @throws Exception if exception during test.
    142    *
    143    */
    144   public void testDeserializationSimple() throws Exception {
    145     Object obj =
    146       SerializationTestHelper.deserializeStream(
    147         "witness/serialization/simple.bin");
    148     assertTrue(obj instanceof LoggingEvent);
    149
    150     LoggingEvent event = (LoggingEvent) obj;
    151     assertEquals("Hello, world.", event.getMessage());
    152     assertEquals(Level.INFO, event.getLevel());
    153   }
    154
    155   /**
    156    * Deserialize a logging event with an exception.
    157    * @throws Exception if exception during test.
    158    *
    159    */
    160   public void testDeserializationWithException() throws Exception {
    161     Object obj =
    162       SerializationTestHelper.deserializeStream(
    163         "witness/serialization/exception.bin");
    164     assertTrue(obj instanceof LoggingEvent);
    165
    166     LoggingEvent event = (LoggingEvent) obj;
    167     assertEquals("Hello, world.", event.getMessage());
    168     assertEquals(Level.INFO, event.getLevel());
    169   }
    170
    171   /**
    172    * Deserialize a logging event with an exception.
    173    * @throws Exception if exception during test.
    174    *
    175    */
    176   public void testDeserializationWithLocation() throws Exception {
    177     Object obj =
    178       SerializationTestHelper.deserializeStream(
    179         "witness/serialization/location.bin");
    180     assertTrue(obj instanceof LoggingEvent);
    181
    182     LoggingEvent event = (LoggingEvent) obj;
    183     assertEquals("Hello, world.", event.getMessage());
    184     assertEquals(Level.INFO, event.getLevel());
    185   }
    186
    187     /**
    188      * Tests LoggingEvent.fqnOfCategoryClass.
    189      */
    190   public void testFQNOfCategoryClass() {
    191       Category root = Logger.getRootLogger();
    192       Priority info = Level.INFO;
    193       String catName = Logger.class.toString();
    194       LoggingEvent event =
    195         new LoggingEvent(
    196           catName, root, info, "Hello, world.", null);
    197       assertEquals(catName, event.fqnOfCategoryClass);
    198   }
    199
    200     /**
    201      * Tests LoggingEvent.level.
    202      * @deprecated
    203      */
    204   public void testLevel() {
    205       Category root = Logger.getRootLogger();
    206       Priority info = Level.INFO;
    207       String catName = Logger.class.toString();
    208       LoggingEvent event =
    209         new LoggingEvent(
    210           catName, root, 0L,  info, "Hello, world.", null);
    211       Priority error = Level.ERROR;
    212       event.level = error;
    213       assertEquals(Level.ERROR, event.level);
    214   }
    215
    216     /**
    217      * Tests LoggingEvent.getLocationInfo() when no FQCN is specified.
    218      * See bug 41186.
    219      */
    220   public void testLocationInfoNoFQCN() {
    221       Category root = Logger.getRootLogger();
    222 	  Priority level = Level.INFO;
    223       LoggingEvent event =
    224         new LoggingEvent(
    225           null, root, 0L,  level, "Hello, world.", null);
    226       LocationInfo info = event.getLocationInformation();
    227 	  //
    228 	  //  log4j 1.2 returns an object, its layout doesn't check for nulls.
    229 	  //  log4j 1.3 returns a null.
    230 	  //
    231 	  assertNotNull(info);
    232 	  if (info != null) {
    233 	     assertEquals("?", info.getLineNumber());
    234 		 assertEquals("?", info.getClassName());
    235 		 assertEquals("?", info.getFileName());
    236 		 assertEquals("?", info.getMethodName());
    237 	  }
    238   }
    239
    240     /**
    241      * Message object that throws a RuntimeException on toString().
    242      * See bug 37182.
    243      */
    244     private static class BadMessage {
    245         public BadMessage() {
    246         }
    247
    248         public String toString() {
    249             throw new RuntimeException();
    250         }
    251     }
    252
    253     /**
    254      * Tests that an runtime exception or error during toString
    255      * on the message parameter does not propagate to caller.
    256      * See bug 37182.
    257      */
    258     public void testBadMessage() {
    259         Category root = Logger.getRootLogger();
    260         Priority info = Level.INFO;
    261         String catName = Logger.class.toString();
    262         BadMessage msg = new BadMessage();
    263         LoggingEvent event =
    264           new LoggingEvent(
    265             catName, root, 0L,  info, msg, null);
    266         //  would result in exception in earlier versions
    267         event.getRenderedMessage();
    268     }
    269
    270
    271 }