    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.net;
    18
    19  import junit.framework.TestCase;
    20  import org.apache.log4j.AppenderSkeleton;
    21  import org.apache.log4j.Logger;
    22  import org.apache.log4j.spi.LoggingEvent;
    23  import org.apache.log4j.xml.DOMConfigurator;
    24
    25  public class SocketAppenderTest extends TestCase {
    26
    27      /**
    28  	 *  Create new instance.
    29  	 */
    30      public SocketAppenderTest(final String testName) {
    31  	    super(testName);
    32      }
    33
    34      /* JUnit's setUp and tearDown */
    35
    36      protected void setUp() {
    37          DOMConfigurator.configure("input/xml/SocketAppenderTestConfig.xml");
    38
    39          logger = Logger.getLogger(SocketAppenderTest.class);
    40          secondary = (LastOnlyAppender) Logger.getLogger(
    41                  "org.apache.log4j.net.SocketAppenderTestDummy").getAppender("lastOnly");
    42      }
    43
    44      protected void tearDown() {
    45      }
    46
    47      /* Tests */
    48
    49      public void testFallbackErrorHandlerWhenStarting() {
    50          String msg = "testFallbackErrorHandlerWhenStarting";
    51          logger.debug(msg);
    52
    53          // above debug log will fail and shoul be redirected to secondary appender
    54          assertEquals("SocketAppender with FallbackErrorHandler", msg, secondary.getLastMessage());
    55      }
    56
    57      /* Fields */
    58
    59      private static Logger logger;
    60      private static LastOnlyAppender secondary;
    61
    62      /* Inner classes */
    63
    64      /**
    65       * Inner-class For debugging purposes only Saves last LoggerEvent
    66       */
    67      static public class LastOnlyAppender extends AppenderSkeleton {
    68          protected void append(LoggingEvent event) {
    69              this.lastEvent = event;
    70          }
    71
    72          public boolean requiresLayout() {
    73              return false;
    74          }
    75
    76          public void close() {
    77              this.closed = true;
    78          }
    79
    80          /**
    81           * @return last appended LoggingEvent's message
    82           */
    83          public String getLastMessage() {
    84              if (this.lastEvent != null)
    85                  return this.lastEvent.getMessage().toString();
    86              else
    87                  return "";
    88          }
    89
    90          private LoggingEvent lastEvent;
    91      };
    92
    93  }