    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 org.apache.log4j.spi.LoggingEvent;
    21
    22
    23  /**
    24   * Test for PatternLayout.
    25   *
    26   * @author Curt Arnold
    27   */
    28  public class PatternLayoutTest extends LayoutTest {
    29    /**
    30     * Construct new instance of PatternLayoutTest.
    31     *
    32     * @param testName test name.
    33     */
    34    public PatternLayoutTest(final String testName) {
    35      super(testName, "text/plain", true, null, null);
    36    }
    37
    38    /**
    39     * @{inheritDoc}
    40     */
    41    protected Layout createLayout() {
    42      return new PatternLayout("[%t] %p %c - %m%n");
    43    }
    44
    45    /**
    46     * Tests format.
    47     */
    48    public void testFormat() {
    49      Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
    50      LoggingEvent event =
    51        new LoggingEvent(
    52          "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
    53      PatternLayout layout = (PatternLayout) createLayout();
    54      String result = layout.format(event);
    55      StringBuffer buf = new StringBuffer(100);
    56      buf.append('[');
    57      buf.append(event.getThreadName());
    58      buf.append("] ");
    59      buf.append(event.getLevel().toString());
    60      buf.append(' ');
    61      buf.append(event.getLoggerName());
    62      buf.append(" - ");
    63      buf.append(event.getMessage());
    64      buf.append(System.getProperty("line.separator"));
    65      assertEquals(buf.toString(), result);
    66    }
    67
    68    /**
    69     * Tests getPatternFormat().
    70     */
    71    public void testGetPatternFormat() {
    72      PatternLayout layout = (PatternLayout) createLayout();
    73      assertEquals("[%t] %p %c - %m%n", layout.getConversionPattern());
    74    }
    75
    76    /**
    77     * Tests DEFAULT_CONVERSION_PATTERN constant.
    78     */
    79    public void testDefaultConversionPattern() {
    80      assertEquals("%m%n", PatternLayout.DEFAULT_CONVERSION_PATTERN);
    81    }
    82
    83    /**
    84     * Tests DEFAULT_CONVERSION_PATTERN constant.
    85     */
    86    public void testTTCCConversionPattern() {
    87      assertEquals(
    88        "%r [%t] %p %c %x - %m%n", PatternLayout.TTCC_CONVERSION_PATTERN);
    89    }
    90
    91    /**
    92     * Tests buffer downsizing code path.
    93     */
    94    public void testFormatResize() {
    95      Logger logger = Logger.getLogger("org.apache.log4j.xml.PatternLayoutTest");
    96      NDC.clear();
    97
    98      char[] msg = new char[2000];
    99
    100     for (int i = 0; i < msg.length; i++) {
    101       msg[i] = 'A';
    102     }
    103
    104     LoggingEvent event1 =
    105       new LoggingEvent(
    106         "org.apache.log4j.Logger", logger, Level.DEBUG, new String(msg), null);
    107     PatternLayout layout = (PatternLayout) createLayout();
    108     String result = layout.format(event1);
    109     LoggingEvent event2 =
    110       new LoggingEvent(
    111         "org.apache.log4j.Logger", logger, Level.WARN, "Hello, World", null);
    112     result = layout.format(event2);
    113     assertEquals("[", result.substring(0, 1));
    114   }
    115
    116   /**
    117    * Class to ensure that protected members are still available.
    118    */
    119   public static final class DerivedPatternLayout extends PatternLayout {
    120     /**
    121      * Constructs a new instance of DerivedPatternLayout.
    122      */
    123     public DerivedPatternLayout() {
    124     }
    125
    126     /**
    127      * Get BUF_SIZE.
    128      * @return return initial buffer size in characters.
    129      */
    130     public int getBufSize() {
    131       return BUF_SIZE;
    132     }
    133
    134     /**
    135      * Get MAX_CAPACITY.
    136      * @return maximum capacity in characters.
    137      */
    138     public int getMaxCapacity() {
    139       return MAX_CAPACITY;
    140     }
    141   }
    142 }