CategoryTest 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 java.lang.reflect.Method;
    23
    24
    25  /**
    26   * Tests of Category.
    27   *
    28   * @author Curt Arnold
    29   * @since 1.2.14
    30   */
    31  public class CategoryTest extends TestCase {
    32    /**
    33     * Constructs new instance of test.
    34     * @param name test name.
    35     */
    36    public CategoryTest(final String name) {
    37      super(name);
    38    }
    39
    40    /**
    41     * Tests Category.forcedLog.
    42     */
    43    public void testForcedLog() {
    44      MockCategory category = new MockCategory("org.example.foo");
    45      category.setAdditivity(false);
    46      category.addAppender(new VectorAppender());
    47      category.info("Hello, World");
    48    }
    49
    50    /**
    51     * Tests that the return type of getChainedPriority is Priority.
    52     * @throws Exception thrown if Category.getChainedPriority can not be found.
    53     */
    54    public void testGetChainedPriorityReturnType() throws Exception {
    55      Method method = Category.class.getMethod("getChainedPriority", (Class[]) null);
    56      assertTrue(method.getReturnType() == Priority.class);
    57    }
    58
    59    /**
    60     * Tests l7dlog(Priority, String, Throwable).
    61     */
    62    public void testL7dlog() {
    63      Logger logger = Logger.getLogger("org.example.foo");
    64      logger.setLevel(Level.ERROR);
    65      Priority debug = Level.DEBUG;
    66      logger.l7dlog(debug, "Hello, World", null);
    67    }
    68
    69    /**
    70     * Tests l7dlog(Priority, String, Object[], Throwable).
    71     */
    72    public void testL7dlog4Param() {
    73      Logger logger = Logger.getLogger("org.example.foo");
    74      logger.setLevel(Level.ERROR);
    75      Priority debug = Level.DEBUG;
    76      logger.l7dlog(debug, "Hello, World", new Object[0], null);
    77    }
    78
    79    /**
    80     * Tests setPriority(Priority).
    81     * @deprecated
    82     */
    83    public void testSetPriority() {
    84      Logger logger = Logger.getLogger("org.example.foo");
    85      Priority debug = Level.DEBUG;
    86      logger.setPriority(debug);
    87    }
    88
    89    /**
    90     * Derived category to check method signature of forcedLog.
    91     */
    92    private static class MockCategory extends Logger {
    93      /**
    94       * Create new instance of MockCategory.
    95       * @param name category name
    96       */
    97      public MockCategory(final String name) {
    98        super(name);
    99        repository = new Hierarchy(this);
    100     }
    101
    102     /**
    103      * Request an info level message.
    104      * @param msg message
    105      */
    106     public void info(final String msg) {
    107       Priority info = Level.INFO;
    108       forcedLog(MockCategory.class.toString(), info, msg, null);
    109     }
    110   }
    111 }