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;
    18
    19  import junit.framework.TestCase;
    20
    21  import java.io.File;
    22  import java.io.FileWriter;
    23  import java.io.IOException;
    24  import java.net.URL;
    25  import java.util.Properties;
    26
    27  /***
    28   * Test property configurator.
    29   *
    30   */
    31  public class PropertyConfiguratorTest extends TestCase {
    32      public PropertyConfiguratorTest(final String testName) {
    33          super(testName);
    34      }
    35
    36      /***
    37       * Test for bug 40944.
    38       * Did not catch IllegalArgumentException on Properties.load
    39       * and close input stream.
    40       * @throws IOException if IOException creating properties file.
    41       */
    42      public void testBadUnicodeEscape() throws IOException {
    43          String fileName = "output/badescape.properties";
    44          FileWriter writer = new FileWriter(fileName);
    45          writer.write("log4j.rootLogger=//uXX41");
    46          writer.close();
    47          PropertyConfigurator.configure(fileName);
    48          File file = new File(fileName);
    49          assertTrue(file.delete()) ;
    50          assertFalse(file.exists());
    51      }
    52
    53      /***
    54       * Test for bug 40944.
    55       * configure(URL) never closed opened stream.
    56       * @throws IOException if IOException creating properties file.
    57       */
    58          public void testURL() throws IOException {
    59          File file = new File("output/unclosed.properties");
    60          FileWriter writer = new FileWriter(file);
    61          writer.write("log4j.rootLogger=debug");
    62          writer.close();
    63          URL url = file.toURL();
    64          PropertyConfigurator.configure(url);
    65          assertTrue(file.delete());
    66          assertFalse(file.exists());
    67      }
    68
    69      /***
    70       * Test for bug 40944.
    71       * configure(URL) did not catch IllegalArgumentException and
    72       * did not close stream.
    73       * @throws IOException if IOException creating properties file.
    74       */
    75          public void testURLBadEscape() throws IOException {
    76          File file = new File("output/urlbadescape.properties");
    77          FileWriter writer = new FileWriter(file);
    78          writer.write("log4j.rootLogger=//uXX41");
    79          writer.close();
    80          URL url = file.toURL();
    81          PropertyConfigurator.configure(url);
    82          assertTrue(file.delete());
    83          assertFalse(file.exists());
    84      }
    85
    86      /***
    87       * Test processing of log4j.reset property, see bug 17531.
    88       *
    89       */
    90      public void testReset() {
    91          VectorAppender appender = new VectorAppender();
    92          appender.setName("A1");
    93          Logger.getRootLogger().addAppender(appender);
    94          Properties props = new Properties();
    95          props.put("log4j.reset", "true");
    96          PropertyConfigurator.configure(props);
    97          assertNull(Logger.getRootLogger().getAppender("A1"));
    98          LogManager.resetConfiguration();
    99      }
    100
    101 }