TestLogXF 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  package org.apache.log4j;
    18
    19  import junit.framework.TestCase;
    20
    21
    22  /**
    23   * Unit test for LogXF.
    24   */
    25  public class TestLogXF extends TestCase {
    26      /**
    27       * Logger.
    28       */
    29      private final Logger logger = Logger.getLogger(
    30              "org.apache.log4j.formatter.TestLogXF");
    31
    32      /**
    33       * Create the test case
    34       *
    35       * @param testName name of the test case
    36       */
    37      public TestLogXF(String testName) {
    38          super(testName);
    39      }
    40
    41
    42      /**
    43       * Post test clean up.
    44       */
    45      public void tearDown() {
    46          LogManager.resetConfiguration();
    47      }
    48
    49      private static class BadStringifier {
    50          private BadStringifier() {}
    51          public static BadStringifier INSTANCE = new BadStringifier();
    52          public String toString() {
    53              throw new NullPointerException();
    54          }
    55      }
    56
    57
    58      /**
    59       * Test LogXF.entering with null class and method.
    60       */
    61      public void testEnteringNullNull() {
    62          LogCapture capture = new LogCapture(Level.DEBUG);
    63          logger.setLevel(Level.DEBUG);
    64          LogXF.entering(logger, null, null);
    65          assertEquals("null.null ENTRY", capture.getMessage());
    66      }
    67
    68
    69      /**
    70       * Test LogXF.entering with null class, method and parameter.
    71       */
    72      public void testEnteringNullNullNull() {
    73          LogCapture capture = new LogCapture(Level.DEBUG);
    74          logger.setLevel(Level.DEBUG);
    75          LogXF.entering(logger, null, null, (String) null);
    76          assertEquals("null.null ENTRY null", capture.getMessage());
    77      }
    78
    79      /**
    80       * Test LogXF.entering with null class, method and parameters.
    81       */
    82      public void testEnteringNullNullNullArray() {
    83          LogCapture capture = new LogCapture(Level.DEBUG);
    84          logger.setLevel(Level.DEBUG);
    85          LogXF.entering(logger, null, null, (Object[]) null);
    86          assertEquals("null.null ENTRY {}", capture.getMessage());
    87      }
    88
    89      /**
    90       * Test LogXF.entering with class and method.
    91       */
    92      public void testEntering() {
    93          LogCapture capture = new LogCapture(Level.DEBUG);
    94          logger.setLevel(Level.DEBUG);
    95          LogXF.entering(logger, "SomeClass", "someMethod");
    96          assertEquals("SomeClass.someMethod ENTRY", capture.getMessage());
    97      }
    98
    99      /**
    100      * Test LogXF.entering with class, method and parameter.
    101      */
    102     public void testEnteringWithParam() {
    103         LogCapture capture = new LogCapture(Level.DEBUG);
    104         logger.setLevel(Level.DEBUG);
    105         LogXF.entering(logger, "SomeClass", "someMethod", "someParam");
    106         assertEquals("SomeClass.someMethod ENTRY someParam", capture.getMessage());
    107     }
    108
    109     /**
    110      * Test LogXF.entering with class, method and bad parameter.
    111      */
    112     public void testEnteringWithBadParam() {
    113         LogCapture capture = new LogCapture(Level.DEBUG);
    114         logger.setLevel(Level.DEBUG);
    115         LogXF.entering(logger, "SomeClass", "someMethod", BadStringifier.INSTANCE);
    116         assertEquals("SomeClass.someMethod ENTRY ?", capture.getMessage());
    117     }
    118
    119     /**
    120      * Test LogXF.entering with class, method and bad parameters.
    121      */
    122     public void testEnteringWithBadParams() {
    123         LogCapture capture = new LogCapture(Level.DEBUG);
    124         logger.setLevel(Level.DEBUG);
    125         LogXF.entering(logger, "SomeClass", "someMethod", new Object[]{"param1",BadStringifier.INSTANCE});
    126         assertEquals("SomeClass.someMethod ENTRY {param1,?}", capture.getMessage());
    127     }
    128
    129
    130     /**
    131      * Test LogXF.exiting with null class and method.
    132      */
    133     public void testExitingNullNull() {
    134         LogCapture capture = new LogCapture(Level.DEBUG);
    135         logger.setLevel(Level.DEBUG);
    136         LogXF.exiting(logger, null, null);
    137         assertEquals("null.null RETURN", capture.getMessage());
    138     }
    139
    140
    141     /**
    142      * Test LogXF.exiting with null class, method and parameter.
    143      */
    144     public void testExitingNullNullNull() {
    145         LogCapture capture = new LogCapture(Level.DEBUG);
    146         logger.setLevel(Level.DEBUG);
    147         LogXF.exiting(logger, null, null, (String) null);
    148         assertEquals("null.null RETURN null", capture.getMessage());
    149     }
    150
    151
    152     /**
    153      * Test LogXF.exiting with class and method.
    154      */
    155     public void testExiting() {
    156         LogCapture capture = new LogCapture(Level.DEBUG);
    157         logger.setLevel(Level.DEBUG);
    158         LogXF.exiting(logger, "SomeClass", "someMethod");
    159         assertEquals("SomeClass.someMethod RETURN", capture.getMessage());
    160     }
    161
    162     /**
    163      * Test LogXF.exiting with class, method and return value.
    164      */
    165     public void testExitingWithValue() {
    166         LogCapture capture = new LogCapture(Level.DEBUG);
    167         logger.setLevel(Level.DEBUG);
    168         LogXF.exiting(logger, "SomeClass", "someMethod", "someValue");
    169         assertEquals("SomeClass.someMethod RETURN someValue", capture.getMessage());
    170     }
    171
    172     /**
    173      * Test LogXF.exiting with class, method and bad return value.
    174      */
    175     public void testExitingWithBadValue() {
    176         LogCapture capture = new LogCapture(Level.DEBUG);
    177         logger.setLevel(Level.DEBUG);
    178         LogXF.exiting(logger, "SomeClass", "someMethod", BadStringifier.INSTANCE);
    179         assertEquals("SomeClass.someMethod RETURN ?", capture.getMessage());
    180     }
    181
    182
    183     /**
    184      * Test LogXF.throwing with null class, method and throwable.
    185      */
    186     public void testThrowingNullNullNull() {
    187         LogCapture capture = new LogCapture(Level.DEBUG);
    188         logger.setLevel(Level.DEBUG);
    189         LogXF.throwing(logger, null, null, null);
    190         assertEquals("null.null THROW", capture.getMessage());
    191     }
    192
    193
    194     /**
    195      * Test LogXF.exiting with class and method.
    196      */
    197     public void testThrowing() {
    198         LogCapture capture = new LogCapture(Level.DEBUG);
    199         logger.setLevel(Level.DEBUG);
    200         LogXF.throwing(logger, "SomeClass", "someMethod", new IllegalArgumentException());
    201         assertEquals("SomeClass.someMethod THROW", capture.getMessage());
    202     }
    203
    204 }