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