logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r510836 - in /logging/log4j/trunk: src/java/org/apache/log4j/PropertyConfigurator.java tests/src/java/org/apache/log4j/CoreTestSuite.java tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
Date Fri, 23 Feb 2007 05:26:31 GMT
Author: carnold
Date: Thu Feb 22 21:26:30 2007
New Revision: 510836

URL: http://svn.apache.org/viewvc?view=rev&rev=510836
Log:
Bug 40944: PropertyConfigurator.configure(URL) does not close resource stream

Added:
    logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
Modified:
    logging/log4j/trunk/src/java/org/apache/log4j/PropertyConfigurator.java
    logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java

Modified: logging/log4j/trunk/src/java/org/apache/log4j/PropertyConfigurator.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/src/java/org/apache/log4j/PropertyConfigurator.java?view=diff&rev=510836&r1=510835&r2=510836
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/PropertyConfigurator.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/PropertyConfigurator.java Thu Feb 22 21:26:30
2007
@@ -312,16 +312,23 @@
   public void doConfigure(String configFileName, LoggerRepository repo) {
     Properties props = new Properties();
 
-    try {
-      FileInputStream istream = new FileInputStream(configFileName);
+      FileInputStream istream = null;
+      try {
+      istream = new FileInputStream(configFileName);
       props.load(istream);
-      istream.close();
-    } catch (IOException e) {
+    } catch (Exception e) {
       String errMsg =
         "Could not read configuration file [" + configFileName + "].";
       addError(new ErrorItem(errMsg, e));
       getLogger(repo).error(errMsg, e);
       return;
+    } finally {
+        if(istream != null) {
+            try {
+                istream.close();
+            } catch(IOException ignored) {
+            }
+        }
     }
 
     // If we reach here, then the config file is alright.
@@ -480,16 +487,23 @@
     getLogger(repository).debug(
       "Reading configuration from URL {}", configURL);
 
+    InputStream in = null;
     try {
-      InputStream in = configURL.openStream();
+      in = configURL.openStream();
       props.load(in);
-      in.close();
-    } catch (java.io.IOException e) {
+    } catch (Exception e) {
       String errMsg =
         "Could not read configuration file from URL [" + configURL + "].";
       addError(new ErrorItem(errMsg, e));
       getLogger(repository).error(errMsg, e);
       return;
+    } finally {
+        if (in != null) {
+            try {
+                in.close();
+            } catch(IOException ignored) {
+            }
+        }
     }
 
     doConfigure(props, repository);

Modified: logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java?view=diff&rev=510836&r1=510835&r2=510836
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java (original)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/CoreTestSuite.java Thu Feb 22 21:26:30
2007
@@ -49,6 +49,7 @@
         s.addTestSuite(org.apache.log4j.HTMLLayoutTest.class);
         s.addTestSuite(org.apache.log4j.PatternLayoutTest.class);
         s.addTestSuite(org.apache.log4j.spi.LoggingEventTest.class);
+        s.addTestSuite(org.apache.log4j.PropertyConfiguratorTest.class);
         return s;
     }
 }

Added: logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java?view=auto&rev=510836
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java (added)
+++ logging/log4j/trunk/tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java Thu
Feb 22 21:26:30 2007
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 java.io.*;
+import junit.framework.*;
+import org.apache.log4j.PropertyConfigurator;
+import java.net.URL;
+
+/**
+ * Test property configurator.
+ *
+ */
+public class PropertyConfiguratorTest extends TestCase {
+    public PropertyConfiguratorTest(final String testName) {
+        super(testName);
+    }
+
+    /**
+     * Test for bug 19108.
+     * Did not catch IllegalArgumentException on Properties.load
+     * and close input stream.
+     * @throws IOException if IOException creating properties file.
+     */
+    public void testBadUnicodeEscape() throws IOException {
+        String fileName = "output/badescape.properties";
+        FileWriter writer = new FileWriter(fileName);
+        writer.write("log4j.rootLogger=\\uXX41");
+        writer.close();
+        PropertyConfigurator.configure(fileName);
+        File file = new File(fileName);
+        assertTrue(file.delete()) ;
+        assertFalse(file.exists());
+    }
+
+    /**
+     * Test for bug 19108.
+     * configure(URL) never closed opened stream.
+     * @throws IOException if IOException creating properties file.
+     */
+        public void testURL() throws IOException {
+        File file = new File("output/unclosed.properties");
+        FileWriter writer = new FileWriter(file);
+        writer.write("log4j.rootLogger=debug");
+        writer.close();
+        URL url = file.toURL();
+        PropertyConfigurator.configure(url);
+        assertTrue(file.delete());
+        assertFalse(file.exists());
+    }
+
+    /**
+     * Test for bug 19108.
+     * configure(URL) did not catch IllegalArgumentException and
+     * did not close stream.
+     * @throws IOException if IOException creating properties file.
+     */
+        public void testURLBadEscape() throws IOException {
+        File file = new File("output/urlbadescape.properties");
+        FileWriter writer = new FileWriter(file);
+        writer.write("log4j.rootLogger=\\uXX41");
+        writer.close();
+        URL url = file.toURL();
+        PropertyConfigurator.configure(url);
+        assertTrue(file.delete());
+        assertFalse(file.exists());
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message