View Javadoc

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