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
    18  package org.apache.log4j;
    19
    20  import junit.framework.TestCase;
    21
    22  import java.io.File;
    23
    24  import java.lang.reflect.Method;
    25
    26
    27  /***
    28   *
    29   * FileAppender tests.
    30   *
    31   * @author Curt Arnold
    32   */
    33  public class FileAppenderTest extends TestCase {
    34    /***
    35     * Tests that any necessary directories are attempted to
    36     * be created if they don't exist.  See bug 9150.
    37     *
    38     */
    39    public void testDirectoryCreation() {
    40      //
    41      //   known to fail on JDK 1.1
    42      if (!System.getProperty("java.version").startsWith("1.1.")) {
    43        File newFile = new File("output/newdir/temp.log");
    44        newFile.delete();
    45
    46        File newDir = new File("output/newdir");
    47        newDir.delete();
    48
    49        org.apache.log4j.FileAppender wa = new org.apache.log4j.FileAppender();
    50        wa.setFile("output/newdir/temp.log");
    51        wa.setLayout(new PatternLayout("%m%n"));
    52        wa.activateOptions();
    53
    54        assertTrue(new File("output/newdir/temp.log").exists());
    55      }
    56    }
    57
    58    /***
    59     * Tests that the return type of getThreshold is Priority.
    60     * @throws Exception
    61     */
    62    public void testGetThresholdReturnType() throws Exception {
    63      Method method = FileAppender.class.getMethod("getThreshold", (Class[]) null);
    64      assertTrue(method.getReturnType() == Priority.class);
    65    }
    66
    67    /***
    68     * Tests getThreshold and setThreshold.
    69     */
    70    public void testgetSetThreshold() {
    71      FileAppender appender = new FileAppender();
    72      Priority debug = Level.DEBUG;
    73      Priority all = Level.ALL;
    74      assertNull(appender.getThreshold());
    75      appender.setThreshold(debug);
    76      assertTrue(appender.getThreshold() == debug);
    77    }
    78
    79    /***
    80     * Tests isAsSevereAsThreshold.
    81     */
    82    public void testIsAsSevereAsThreshold() {
    83      FileAppender appender = new FileAppender();
    84      Priority debug = Level.DEBUG;
    85      assertTrue(appender.isAsSevereAsThreshold(debug));
    86    }
    87  }