    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.helpers.DateLayoutTest;
    21  import org.apache.log4j.spi.LoggingEvent;
    22
    23
    24  /**
    25   * Test for TTCCLayout.
    26   *
    27   * @author Curt Arnold
    28   */
    29  public class TTCCLayoutTest extends DateLayoutTest {
    30    /**
    31     * Construct new instance of TTCCLayoutTest.
    32     *
    33     * @param testName test name.
    34     */
    35    public TTCCLayoutTest(final String testName) {
    36      super(testName, "text/plain", true, null, null);
    37    }
    38
    39    /**
    40     * @{inheritDoc}
    41     */
    42    protected Layout createLayout() {
    43      return new TTCCLayout();
    44    }
    45
    46    /**
    47     * Tests format.
    48     */
    49    public void testFormat() {
    50      NDC.clear();
    51      NDC.push("NDC goes here");
    52
    53      Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
    54      LoggingEvent event =
    55        new LoggingEvent(
    56          "org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
    57      TTCCLayout layout = (TTCCLayout) createLayout();
    58      String result = layout.format(event);
    59      NDC.pop();
    60
    61      StringBuffer buf = new StringBuffer(100);
    62      layout.dateFormat(buf, event);
    63      buf.append('[');
    64      buf.append(event.getThreadName());
    65      buf.append("] ");
    66      buf.append(event.getLevel().toString());
    67      buf.append(' ');
    68      buf.append(event.getLoggerName());
    69      buf.append(' ');
    70      buf.append("NDC goes here");
    71      buf.append(" - ");
    72      buf.append(event.getMessage());
    73      buf.append(System.getProperty("line.separator"));
    74      assertEquals(buf.toString(), result);
    75    }
    76
    77    /**
    78     * Tests getThreadPrinting and setThreadPrinting.
    79     */
    80    public void testGetSetThreadPrinting() {
    81      TTCCLayout layout = new TTCCLayout();
    82      assertEquals(true, layout.getThreadPrinting());
    83      layout.setThreadPrinting(false);
    84      assertEquals(false, layout.getThreadPrinting());
    85      layout.setThreadPrinting(true);
    86      assertEquals(true, layout.getThreadPrinting());
    87    }
    88
    89    /**
    90     * Tests getCategoryPrefixing and setCategoryPrefixing.
    91     */
    92    public void testGetSetCategoryPrefixing() {
    93      TTCCLayout layout = new TTCCLayout();
    94      assertEquals(true, layout.getCategoryPrefixing());
    95      layout.setCategoryPrefixing(false);
    96      assertEquals(false, layout.getCategoryPrefixing());
    97      layout.setCategoryPrefixing(true);
    98      assertEquals(true, layout.getCategoryPrefixing());
    99    }
    100
    101   /**
    102    * Tests getContextPrinting and setContextPrinting.
    103    */
    104   public void testGetSetContextPrinting() {
    105     TTCCLayout layout = new TTCCLayout();
    106     assertEquals(true, layout.getContextPrinting());
    107     layout.setContextPrinting(false);
    108     assertEquals(false, layout.getContextPrinting());
    109     layout.setContextPrinting(true);
    110     assertEquals(true, layout.getContextPrinting());
    111   }
    112 }