Author: carnold
Date: Tue Dec 20 16:53:51 2005
New Revision: 358162

URL: http://svn.apache.org/viewcvs?rev=358162&view=rev
Log:
Bug 37965: SyslogAppender now API compatible, functionality questionable

Added:
    logging/log4j/trunk/tests/src/java/org/apache/log4j/VectorErrorHandler.java
    
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
Removed:
    logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogConstants.java
Modified:
    logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java
    logging/log4j/trunk/tests/build.xml

Modified: logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java
URL: 
http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java?rev=358162&r1=358161&r2=358162&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java 
(original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/net/SyslogAppender.java Tue 
Dec 20 16:53:51 2005
@@ -45,34 +45,123 @@
  * @author Ceki Gülcü
  * @author Anders Kristensen
  * @author Hermod Opstvedt
+ * @author Curt Arnold
  */
 public class SyslogAppender extends AppenderSkeleton {
+    // The following constants are extracted from a syslog.h file
+    // copyrighted by the Regents of the University of California
+    // I hope nobody at Berkley gets offended.
+
+    /** Kernel messages */
+    final static public int LOG_KERN     = 0;
+    /** Random user-level messages */
+    final static public int LOG_USER     = 1<<3;
+    /** Mail system */
+    final static public int LOG_MAIL     = 2<<3;
+    /** System daemons */
+    final static public int LOG_DAEMON   = 3<<3;
+    /** security/authorization messages */
+    final static public int LOG_AUTH     = 4<<3;
+    /** messages generated internally by syslogd */
+    final static public int LOG_SYSLOG   = 5<<3;
+
+    /** line printer subsystem */
+    final static public int LOG_LPR      = 6<<3;
+    /** network news subsystem */
+    final static public int LOG_NEWS     = 7<<3;
+    /** UUCP subsystem */
+    final static public int LOG_UUCP     = 8<<3;
+    /** clock daemon */
+    final static public int LOG_CRON     = 9<<3;
+    /** security/authorization  messages (private) */
+    final static public int LOG_AUTHPRIV = 10<<3;
+    /** ftp daemon */
+    final static public int LOG_FTP      = 11<<3;
+
+    // other codes through 15 reserved for system use
+    /** reserved for local use */
+    final static public int LOG_LOCAL0 = 16<<3;
+    /** reserved for local use */
+    final static public int LOG_LOCAL1 = 17<<3;
+    /** reserved for local use */
+    final static public int LOG_LOCAL2 = 18<<3;
+    /** reserved for local use */
+    final static public int LOG_LOCAL3 = 19<<3;
+    /** reserved for local use */
+    final static public int LOG_LOCAL4 = 20<<3;
+    /** reserved for local use */
+    final static public int LOG_LOCAL5 = 21<<3;
+    /** reserved for local use */
+    final static public int LOG_LOCAL6 = 22<<3;
+    /** reserved for local use*/
+    final static public int LOG_LOCAL7 = 23<<3;
+
+    /**
+     * Names of facilities.
+     */
+    private static final String[] FACILITIES =
+            new String[] {
+                    "kern", "user", "mail", "daemon",
+                    "auth", "syslog", "lpr", "news",
+                    "uucp", "cron", "authpriv","ftp",
+                    null, null, null, null,
+                    "local0", "local1", "local2", "local3",
+                    "local4", "local5", "local6", "local7"
+
+            };
+
+
   protected static final int SYSLOG_HOST_OI = 0;
   protected static final int FACILITY_OI = 1;
   static final String TAB = "    ";
-  int facility;
-  String facilityStr;
+  int syslogFacility = LOG_USER;
+  String facilityStr = "user";
+    /**
+     * In log4j 1.2, controlled whether facility name was included in message,
+     * but has no effect in current code.
+     * @deprecated since 1.3
+     */
+  boolean facilityPrinting = false;
+
   String localHostname;
   String syslogHost;
 
   //SyslogTracerPrintWriter stp;
-  SyslogWriter sw;
-  Calendar calendar = Calendar.getInstance();
-  long now = -1;
-  Date date = new Date();
-  StringBuffer timestamp = new StringBuffer();
-  protected FieldPosition pos = new FieldPosition(0);
+  private SyslogWriter sw;
+  private final Calendar calendar = Calendar.getInstance();
+  private long now = -1;
+  private Date date = new Date();
+  private StringBuffer timestamp = new StringBuffer();
+  private FieldPosition pos = new FieldPosition(0);
 
   // We must use US locale to get the correct month abreviation  
   private SimpleDateFormat sdf =
     new SimpleDateFormat("MMM dd hh:mm:ss", new DateFormatSymbols(Locale.US));
 
-  Layout layout;
+  private Layout layout;
   
   public SyslogAppender() {
       super(false);
   }
 
+  public
+  SyslogAppender(final Layout layout, final int syslogFacility) {
+      super(false);
+      this.layout = layout;
+      this.syslogFacility = syslogFacility;
+      String newFacilityStr = getFacilityString(syslogFacility);
+      if (newFacilityStr != null) {
+          facilityStr = newFacilityStr;
+      }
+  }
+
+   public
+   SyslogAppender(final Layout layout, final String syslogHost, final int 
syslogFacility) {
+      this(layout, syslogFacility);
+      setSyslogHost(syslogHost);
+   }
+
+
   /**
    * Release any resources held by this SyslogAppender.
    * @since 0.8.4
@@ -103,55 +192,41 @@
     }
   }
 
+    /**
+      * Returns the specified syslog facility as a lower-case String,
+      * e.g. "kern", "user", etc.
+      * @deprecated since 1.3
+    */
+    public
+    static
+    String getFacilityString(final int syslogFacility) {
+        String facilityStr = null;
+        if((syslogFacility & 0x7) == 0) {
+           int index = syslogFacility >> 3;
+           if(index >= 0 && index < FACILITIES.length) {
+               facilityStr = FACILITIES[index];
+           }
+        }
+        return facilityStr;
+    }
+
+
   /**
    * Returns the integer value corresponding to the named syslog facility,
    * or -1 if it couldn't be recognized.
    *
    * */
-  static int facilityStringToint(String facilityStr) {
-    if ("KERN".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_KERN;
-    } else if ("USER".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_USER;
-    } else if ("MAIL".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_MAIL;
-    } else if ("DAEMON".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_DAEMON;
-    } else if ("AUTH".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_AUTH;
-    } else if ("SYSLOG".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_SYSLOG;
-    } else if ("LPR".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LPR;
-    } else if ("NEWS".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_NEWS;
-    } else if ("UUCP".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_UUCP;
-    } else if ("CRON".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_CRON;
-    } else if ("AUTHPRIV".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_AUTHPRIV;
-    } else if ("FTP".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_FTP;
-    } else if ("LOCAL0".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL0;
-    } else if ("LOCAL1".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL1;
-    } else if ("LOCAL2".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL2;
-    } else if ("LOCAL3".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL3;
-    } else if ("LOCAL4".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL4;
-    } else if ("LOCAL5".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL5;
-    } else if ("LOCAL6".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL6;
-    } else if ("LOCAL7".equalsIgnoreCase(facilityStr)) {
-      return SyslogConstants.LOG_LOCAL7;
-    } else {
-      return -1;
+  public static int getFacility(final String facilityStr) {
+    int code = -1;
+    if (facilityStr != null) {
+        for(int i = 0; i < FACILITIES.length; i++) {
+            if (facilityStr.equalsIgnoreCase(FACILITIES[i])) {
+                code = i << 3;
+                break;
+            }
+        }
     }
+    return code;
   }
 
   /**
@@ -167,10 +242,10 @@
     }
     facilityStr = facilityStr.trim();
     getLogger().debug("Facility string set to be {}.", facilityStr);
-    facility = facilityStringToint(facilityStr);
-    getLogger().debug("Facility set to be "+ facility);
+    syslogFacility = getFacility(facilityStr);
+    getLogger().debug("Facility set to be "+ syslogFacility);
     
-    if (facility == -1) {
+    if (syslogFacility == -1) {
       String errMsg =
         "Unrecognized Facility option \"" + facilityStr
         + "\" SyslogAppender named [" + name + "].";
@@ -180,7 +255,7 @@
 
     if (syslogHost == null) {
       String errMsg =
-        "No syslog host is set for SyslogAppedender named \"" + this.name
+        "No syslog host is set for SyslogAppender named \"" + this.name
         + "\".";
       getLogger().error(errMsg);
       throw new IllegalStateException(errMsg);
@@ -188,7 +263,7 @@
 
     if (layout == null) {
       String errMsg =
-        "No Layout is set for SyslogAppedender named \"" + this.name
+        "No Layout is set for SyslogAppender named \"" + this.name
         + "\".";
       getLogger().error(errMsg);
       throw new IllegalStateException(errMsg);
@@ -197,8 +272,20 @@
     localHostname = getLocalHostname();
 
     sw = new SyslogWriter(syslogHost);
+    super.activateOptions();
   }
 
+    /**
+       The SyslogAppender requires a layout. Hence, this method returns
+       <code>true</code>.
+
+       @since 0.8.4 */
+    public
+    boolean requiresLayout() {
+      return true;
+    }
+
+
   /**
    * The <b>SyslogHost</b> option is the name of the the syslog host where log
    * output should go.
@@ -224,11 +311,19 @@
    * NTP, AUDIT, ALERT, CLOCK, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5,
    * LOCAL6, LOCAL7. Case is not important.
    *
-   * See [EMAIL PROTECTED] SyslogConstants} and RFC 3164 for more information 
about the
+   * See RFC 3164 for more information about the
    * <b>Facility</b> option.
    * */
-  public void setFacility(String facility) {
-    this.facilityStr = facility;
+  public void setFacility(final String facility) {
+    if (facility != null) {
+        syslogFacility = getFacility(facility);
+        if (syslogFacility == -1) {
+          System.err.println("["+facility +
+                      "] is an unknown syslog facility. Defaulting to 
[USER].");
+          syslogFacility = LOG_USER;
+        }
+        facilityStr = getFacilityString(syslogFacility);
+    }
   }
 
   /**
@@ -260,7 +355,7 @@
   }
 
   void writeInitialParts(LoggingEvent event) throws IOException {
-    int pri = facility+event.getLevel().getSyslogEquivalent();
+    int pri = syslogFacility +event.getLevel().getSyslogEquivalent();
     System.out.println(""+pri);
     sw.write("<");
     sw.write(String.valueOf(pri));
@@ -305,4 +400,27 @@
   public void setLayout(Layout layout) {
     this.layout = layout;
   }
+
+    /**
+     * If the <b>FacilityPrinting</b> option is set to true, the printed
+     * message will include the facility name of the application. It is
+     * <em>false</em> by default.
+     *
+     * @deprecated No effect in log4j 1.3
+     */
+    public
+    void setFacilityPrinting(boolean on) {
+      facilityPrinting = on;
+    }
+
+    /**
+     * Returns the value of the <b>FacilityPrinting</b> option.
+     *
+     * @deprecated No effect in log4j 1.3
+     */
+    public
+    boolean getFacilityPrinting() {
+      return facilityPrinting;
+    }
+
 }

Modified: logging/log4j/trunk/tests/build.xml
URL: 
http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/build.xml?rev=358162&r1=358161&r2=358162&view=diff
==============================================================================
--- logging/log4j/trunk/tests/build.xml (original)
+++ logging/log4j/trunk/tests/build.xml Tue Dec 20 16:53:51 2005
@@ -197,7 +197,8 @@
                                      Plugins,
                                      CachedDateFormat,
                                         Schema,
-                                        Encoding
+                                        Encoding,
+                                        Syslog
                                      "/>
 
 
@@ -737,6 +738,13 @@
     </junit>
   </target>
 
+  <target name="Syslog" depends="build">
+      <junit printsummary="yes" fork="yes" haltonfailure="${haltonfailure}">
+        <classpath refid="tests.classpath"/>
+        <formatter type="plain" usefile="false"/>
+        <test name="org.apache.log4j.net.SyslogAppenderTest" />
+      </junit>
+  </target>
 
        
   <!-- ================================================================= -->

Added: 
logging/log4j/trunk/tests/src/java/org/apache/log4j/VectorErrorHandler.java
URL: 
http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/VectorErrorHandler.java?rev=358162&view=auto
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/VectorErrorHandler.java 
(added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/VectorErrorHandler.java 
Tue Dec 20 16:53:51 2005
@@ -0,0 +1,182 @@
+/*
+ * Copyright 1999,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j;
+
+import org.apache.log4j.spi.LoggingEvent;
+
+import java.util.Vector;
+
+
+/**
+ * Utility class used in testing to capture errors dispatched
+ * by appenders.
+ *
+ * @author Curt Arnold
+ * @deprecated since ErrorHandler is deprecated
+ */
+public final class VectorErrorHandler
+  implements org.apache.log4j.spi.ErrorHandler {
+  /**
+   * Logger.
+   */
+  private Logger logger;
+
+  /**
+   * Appender.
+   */
+  private Appender appender;
+
+  /**
+   * Backup appender.
+   */
+  private Appender backupAppender;
+
+  /**
+   * Array of processed errors.
+   */
+  private final Vector errors = new Vector();
+
+  /**
+   * Default constructor.
+   */
+  public VectorErrorHandler() {
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void setLogger(final Logger logger) {
+    this.logger = logger;
+  }
+
+  /**
+   * Gets last logger specified by setLogger.
+   * @return logger.
+   */
+  public Logger getLogger() {
+    return logger;
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void activateOptions() {
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void error(
+    final String message, final Exception e, final int errorCode) {
+    error(message, e, errorCode, null);
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void error(final String message) {
+    error(message, null, -1, null);
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void error(
+    final String message, final Exception e, final int errorCode,
+    final LoggingEvent event) {
+    errors.addElement(
+      new Object[] { message, e, new Integer(errorCode), event });
+  }
+
+  /**
+   * Gets message from specified error.
+   *
+   * @param index index.
+   * @return message, may be null.
+   */
+  public String getMessage(final int index) {
+    return (String) ((Object[]) errors.elementAt(index))[0];
+  }
+
+  /**
+   * Gets exception from specified error.
+   *
+   * @param index index.
+   * @return exception.
+   */
+  public Exception getException(final int index) {
+    return (Exception) ((Object[]) errors.elementAt(index))[1];
+  }
+
+  /**
+   * Gets error code from specified error.
+   *
+   * @param index index.
+   * @return error code, -1 if not specified.
+   */
+  public int getErrorCode(final int index) {
+    return ((Integer) ((Object[]) errors.elementAt(index))[2]).intValue();
+  }
+
+  /**
+   * Gets logging event from specified error.
+   *
+   * @param index index.
+   * @return exception.
+   */
+  public LoggingEvent getEvent(final int index) {
+    return (LoggingEvent) ((Object[]) errors.elementAt(index))[3];
+  }
+
+  /**
+   * Gets number of errors captured.
+   * @return number of errors captured.
+   */
+  public int size() {
+    return errors.size();
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void setAppender(final Appender appender) {
+    this.appender = appender;
+  }
+
+  /**
+   * Get appender.
+   * @return appender, may be null.
+   */
+  public Appender getAppender() {
+    return appender;
+  }
+
+  /**
+   * [EMAIL PROTECTED]
+   */
+  public void setBackupAppender(final Appender appender) {
+    this.backupAppender = appender;
+  }
+
+  /**
+   * Get backup appender.
+   * @return backup appender, may be null.
+   */
+  public Appender getBackupAppender() {
+    return backupAppender;
+  }
+}

Added: 
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java
URL: 
http://svn.apache.org/viewcvs/logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java?rev=358162&view=auto
==============================================================================
--- 
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java 
(added)
+++ 
logging/log4j/trunk/tests/src/java/org/apache/log4j/net/SyslogAppenderTest.java 
Tue Dec 20 16:53:51 2005
@@ -0,0 +1,354 @@
+/*
+ * Copyright 1999,2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.net;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.AsyncAppender;
+import org.apache.log4j.Layout;
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+
+
+/**
+ *    Tests for SyslogAppender
+ *
+ * @author Curt Arnold
+ **/
+public class SyslogAppenderTest extends TestCase {
+  /**
+   * Create new instance of SyslogAppenderTest.
+   * @param testName test name
+   */
+  public SyslogAppenderTest(final String testName) {
+    super(testName);
+  }
+
+  /**
+    * Resets configuration after every test.
+  */
+  public void tearDown() {
+    LogManager.resetConfiguration();
+  }
+
+  /**
+   * Test default constructor.
+   * @deprecated Since getFacilityPrinting is deprecated
+   */
+  public void testDefaultConstructor() {
+    SyslogAppender appender = new SyslogAppender();
+    assertEquals("user", appender.getFacility());
+    assertEquals(false, appender.getFacilityPrinting());
+    assertNull(appender.getLayout());
+    assertNull(appender.getSyslogHost());
+    assertTrue(appender.requiresLayout());
+  }
+
+  /**
+   * Test two parameter constructor.
+   * @deprecated Since getFacilityPrinting is deprecated
+   */
+  public void testTwoParamConstructor() {
+    Layout layout = new PatternLayout();
+    SyslogAppender appender = new SyslogAppender(layout, 24);
+    assertEquals("daemon", appender.getFacility());
+    assertEquals(false, appender.getFacilityPrinting());
+    assertEquals(layout, appender.getLayout());
+    assertNull(appender.getSyslogHost());
+    assertTrue(appender.requiresLayout());
+  }
+
+  /**
+   * Test two parameter constructor with unexpected facility.
+   * @deprecated Since getFacilityPrinting is deprecated
+   */
+  public void testTwoParamConstructorBadFacility() {
+    Layout layout = new PatternLayout();
+    SyslogAppender appender = new SyslogAppender(layout, 25);
+    assertEquals("user", appender.getFacility());
+    assertEquals(false, appender.getFacilityPrinting());
+    assertEquals(layout, appender.getLayout());
+    assertNull(appender.getSyslogHost());
+    assertTrue(appender.requiresLayout());
+  }
+
+  /**
+   * Test three parameter constructor.
+   * @deprecated Since getFacilityPrinting is deprecated
+   */
+  public void testThreeParamConstructor() {
+    Layout layout = new PatternLayout();
+    SyslogAppender appender =
+      new SyslogAppender(layout, "syslog.example.org", 24);
+    assertEquals("daemon", appender.getFacility());
+    assertEquals(false, appender.getFacilityPrinting());
+    assertEquals(layout, appender.getLayout());
+    assertEquals("syslog.example.org", appender.getSyslogHost());
+    assertTrue(appender.requiresLayout());
+  }
+
+  /**
+   * Test getFacilityString for expected facility codes.
+   * @deprecated Since getFacilityString is deprecated
+   */
+  public void testGetFacilityString() {
+    String expected =
+      "kern user mail daemon auth syslog lpr news "
+      + "uucp cron authpriv ftp local0 local1 local2 local3 "
+      + "local4 local5 local6 local7 ";
+    StringBuffer actual = new StringBuffer();
+
+    for (int i = 0; i <= 11; i++) {
+      actual.append(SyslogAppender.getFacilityString(i << 3));
+      actual.append(' ');
+    }
+
+    for (int i = 16; i <= 23; i++) {
+      actual.append(SyslogAppender.getFacilityString(i << 3));
+      actual.append(' ');
+    }
+
+    assertEquals(expected, actual.toString());
+  }
+
+  /**
+   * Test getFacilityString for some unexpected facility codes.
+   * @deprecated Since getFacilityString is deprecated
+   */
+  public void testGetFacilityStringUnexpected() {
+    assertNull(SyslogAppender.getFacilityString(1));
+    assertNull(SyslogAppender.getFacilityString(12 << 3));
+  }
+
+  /**
+   * Test getFacility with a bogus facility name.
+   */
+  public void testGetFacilityBogus() {
+    assertEquals(-1, SyslogAppender.getFacility("bogus"));
+  }
+
+  /**
+   * Test getFacility with a null facility name.
+   */
+  public void testGetFacilityNull() {
+    assertEquals(-1, SyslogAppender.getFacility(null));
+  }
+
+  /**
+   * Test getFacility for expected system facility names.
+   */
+  public void testGetFacilitySystemNames() {
+    String[] names =
+      new String[] {
+        "kErn", "usEr", "MaIL", "daemOn", "auTh", "syslOg", "lPr", "newS",
+        "Uucp", "croN", "authprIv", "ftP"
+      };
+
+    for (int i = 0; i <= 11; i++) {
+      assertEquals(i << 3, SyslogAppender.getFacility(names[i]));
+    }
+  }
+
+  /**
+   * Test getFacility for expected system facility names.
+   */
+  public void testGetFacilityLocalNames() {
+    String[] names =
+      new String[] {
+        "lOcal0", "LOCAL1", "loCal2", "locAl3", "locaL4", "local5", "LOCal6",
+        "loCAL7"
+      };
+
+    for (int i = 0; i <= 7; i++) {
+      assertEquals((16 + i) << 3, SyslogAppender.getFacility(names[i]));
+    }
+  }
+
+  /**
+   * Test setFacilityPrinting.
+   * @deprecated Since get/setFacilityPrinting are deprecated
+   */
+  public void testSetFacilityPrinting() {
+    SyslogAppender appender = new SyslogAppender();
+    assertFalse(appender.getFacilityPrinting());
+    appender.setFacilityPrinting(true);
+    assertTrue(appender.getFacilityPrinting());
+    appender.setFacilityPrinting(false);
+    assertFalse(appender.getFacilityPrinting());
+  }
+
+  /**
+   * Test of SyslogAppender constants.
+   */
+  public void testConstants() {
+    assertEquals(0 << 3, SyslogAppender.LOG_KERN);
+    assertEquals(1 << 3, SyslogAppender.LOG_USER);
+    assertEquals(2 << 3, SyslogAppender.LOG_MAIL);
+    assertEquals(3 << 3, SyslogAppender.LOG_DAEMON);
+    assertEquals(4 << 3, SyslogAppender.LOG_AUTH);
+    assertEquals(5 << 3, SyslogAppender.LOG_SYSLOG);
+    assertEquals(6 << 3, SyslogAppender.LOG_LPR);
+    assertEquals(7 << 3, SyslogAppender.LOG_NEWS);
+    assertEquals(8 << 3, SyslogAppender.LOG_UUCP);
+    assertEquals(9 << 3, SyslogAppender.LOG_CRON);
+    assertEquals(10 << 3, SyslogAppender.LOG_AUTHPRIV);
+    assertEquals(11 << 3, SyslogAppender.LOG_FTP);
+    assertEquals(16 << 3, SyslogAppender.LOG_LOCAL0);
+    assertEquals(17 << 3, SyslogAppender.LOG_LOCAL1);
+    assertEquals(18 << 3, SyslogAppender.LOG_LOCAL2);
+    assertEquals(19 << 3, SyslogAppender.LOG_LOCAL3);
+    assertEquals(20 << 3, SyslogAppender.LOG_LOCAL4);
+    assertEquals(21 << 3, SyslogAppender.LOG_LOCAL5);
+    assertEquals(22 << 3, SyslogAppender.LOG_LOCAL6);
+    assertEquals(23 << 3, SyslogAppender.LOG_LOCAL7);
+  }
+
+  /**
+   * Test setFacility with null.
+   * Should have no effect.
+   */
+  public void testSetFacilityKern() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setFacility("kern");
+    appender.setFacility(null);
+    assertEquals("kern", appender.getFacility());
+  }
+
+  /**
+   * Test setFacility with null.
+   * Should have no effect.
+   */
+  public void testSetFacilityNull() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setFacility("kern");
+    appender.setFacility(null);
+    assertEquals("kern", appender.getFacility());
+  }
+
+  /**
+   * Test setFacility with bogus value.
+   * Should reset to user.
+   */
+  public void testSetFacilityBogus() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setFacility("kern");
+    appender.setFacility("bogus");
+    assertEquals("user", appender.getFacility());
+  }
+
+  /**
+   * Tests calling setFacility after appender has been activated.
+   * @deprecated Since setErrorHandler is deprecated
+   */
+  public void testSetFacilityAfterActivation() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setName("foo");
+    appender.setThreshold(Level.INFO);
+    appender.setSyslogHost("localhost");
+    appender.setFacility("user");
+    appender.setLayout(new PatternLayout("%m%n"));
+
+    org.apache.log4j.VectorErrorHandler errorHandler =
+      new org.apache.log4j.VectorErrorHandler();
+    appender.setErrorHandler(errorHandler);
+    appender.activateOptions();
+    appender.setFacility("kern");
+    assertEquals("kern", appender.getFacility());
+  }
+
+  /**
+   * Tests that append method drops messages below threshold.
+   * Can't reach isSevereAsThreshold call in SyslogAppender.append
+   * since it is checked in AppenderSkeleton.doAppend.
+   */
+  public void testAppendBelowThreshold() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setThreshold(Level.ERROR);
+    appender.setSyslogHost("localhost");
+    appender.setLayout(new PatternLayout("%m%n"));
+    appender.activateOptions();
+
+    Logger logger = Logger.getRootLogger();
+    logger.addAppender(appender);
+    logger.info(
+      "Should not be logged by SyslogAppenderTest.testAppendBelowThreshold.");
+  }
+
+  /**
+   * Tests that append method drops messages below threshold.
+   * @deprecated Since setErrorHandler is deprecated
+   */
+  public void testAppendNoHost() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setName("foo");
+    appender.setThreshold(Level.INFO);
+
+    org.apache.log4j.VectorErrorHandler errorHandler =
+      new org.apache.log4j.VectorErrorHandler();
+    appender.setErrorHandler(errorHandler);
+    appender.setLayout(new PatternLayout("%m%n"));
+
+    //
+    //   log4j 1.2 would not throw exception on activateOptions
+    //     but would call ErrorHandler on first log attempt
+    //
+    try {
+      appender.activateOptions();
+    } catch (IllegalStateException ex) {
+      return;
+    }
+
+    fail("Expected IllegalStateException");
+  }
+
+  /**
+   * Tests append method under normal conditions.
+   * @deprecated Since setErrorHandler is deprecated
+   */
+  public void testAppend() {
+    SyslogAppender appender = new SyslogAppender();
+    appender.setName("foo");
+    appender.setThreshold(Level.INFO);
+    appender.setSyslogHost("localhost");
+    appender.setFacility("user");
+    appender.setLayout(new PatternLayout("%m%n"));
+
+    org.apache.log4j.VectorErrorHandler errorHandler =
+      new org.apache.log4j.VectorErrorHandler();
+    appender.setErrorHandler(errorHandler);
+    appender.activateOptions();
+
+    //
+    //  wrap SyslogAppender with an Async since appender may
+    //    hang if syslogd is not accepting network messages
+    //
+    AsyncAppender asyncAppender = new AsyncAppender();
+    asyncAppender.addAppender(appender);
+    asyncAppender.activateOptions();
+
+    Logger logger = Logger.getRootLogger();
+    logger.addAppender(asyncAppender);
+
+    Exception e =
+      new Exception("Expected exception from SyslogAppenderTest.testAppend");
+    logger.info(
+      "Expected message from log4j unit test SyslogAppenderTest.testAppend.", 
e);
+    assertEquals(0, errorHandler.size());
+  }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to