You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2013/01/18 08:06:43 UTC

svn commit: r1435044 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/ core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/ src/changes/

Author: rgoers
Date: Fri Jan 18 07:06:43 2013
New Revision: 1435044

URL: http://svn.apache.org/viewvc?rev=1435044&view=rev
Log:
LOG4J2-152 - RollingFileAppender's FileRenameAction was throwing a NullPointerException if no directory was specified on the target file name.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameActionTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java?rev=1435044&r1=1435043&r2=1435044&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameAction.java Fri Jan 18 07:06:43 2013
@@ -76,7 +76,7 @@ public class FileRenameAction extends Ab
     public static boolean execute(final File source, final File destination, final boolean renameEmptyFiles) {
         if (renameEmptyFiles || source.length() > 0) {
             final File parent = destination.getParentFile();
-            if (!parent.exists()) {
+            if (parent != null && !parent.exists()) {
                 if (!parent.mkdirs()) {
                     LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
                     return false;
@@ -102,6 +102,12 @@ public class FileRenameAction extends Ab
                         destination.getAbsolutePath(), iex.getMessage());
                 }
             }
+        } else {
+            try {
+                source.delete();
+            } catch (Exception ex) {
+                LOGGER.error("Unable to delete empty file " + source.getAbsolutePath());
+            }
         }
 
         return false;

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameActionTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameActionTest.java?rev=1435044&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameActionTest.java (added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/appender/rolling/helper/FileRenameActionTest.java Fri Jan 18 07:06:43 2013
@@ -0,0 +1,128 @@
+/*
+ * 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.logging.log4j.core.appender.rolling.helper;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.PrintStream;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class FileRenameActionTest {
+
+    private static final String DIR = "target/fileRename";
+
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        File file = new File(DIR);
+        file.mkdirs();
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        deleteDir();
+    }
+
+    @After
+    public void after() {
+        deleteFiles();
+    }
+
+    @Test
+    public void testRename1() throws Exception {
+        File file = new File("target/fileRename/fileRename.log");
+        PrintStream pos = new PrintStream(file);
+        for (int i = 0; i < 100; ++i) {
+            pos.println("This is line " + i);
+        }
+        pos.close();
+
+        File dest = new File("target/fileRename/newFile.log");
+        FileRenameAction action = new FileRenameAction(file, dest, false);
+        action.execute();
+        assertTrue("Renamed file does not exist", dest.exists());
+        assertTrue("Old file exists", !file.exists());
+    }
+
+    @Test
+    public void testEmpty() throws Exception {
+        File file = new File("target/fileRename/fileRename.log");
+        PrintStream pos = new PrintStream(file);
+        pos.close();
+
+        File dest = new File("target/fileRename/newFile.log");
+        FileRenameAction action = new FileRenameAction(file, dest, false);
+        action.execute();
+        assertTrue("Renamed file does not exist", !dest.exists());
+        assertTrue("Old file does not exist", !file.exists());
+    }
+
+
+    @Test
+    public void testNoParent() throws Exception {
+        File file = new File("fileRename.log");
+        PrintStream pos = new PrintStream(file);
+        for (int i = 0; i < 100; ++i) {
+            pos.println("This is line " + i);
+        }
+        pos.close();
+
+        File dest = new File("newFile.log");
+        try {
+            FileRenameAction action = new FileRenameAction(file, dest, false);
+            action.execute();
+            assertTrue("Renamed file does not exist", dest.exists());
+            assertTrue("Old file exists", !file.exists());
+        } finally {
+            try {
+                dest.delete();
+                file.delete();
+            } catch (Exception ex) {
+                System.out.println("Unable to cleanup files written to main directory");
+            }
+        }
+    }
+
+
+    private static void deleteDir() {
+        final File dir = new File(DIR);
+        if (dir.exists()) {
+            final File[] files = dir.listFiles();
+            for (final File file : files) {
+                file.delete();
+            }
+            dir.delete();
+        }
+    }
+
+    private static void deleteFiles() {
+        final File dir = new File(DIR);
+        if (dir.exists()) {
+            final File[] files = dir.listFiles();
+            for (final File file : files) {
+                file.delete();
+            }
+        }
+    }
+}

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1435044&r1=1435043&r2=1435044&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Fri Jan 18 07:06:43 2013
@@ -23,6 +23,10 @@
 
   <body>
     <release version="2.0-beta4" date="TBD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-152" dev="rgoers" type="fix" due-to="Remko Popma">
+        RollingFileAppender's FileRenameAction was throwing a NullPointerException if no directory was specified
+        on the target file name.
+      </action>
       <action issue="LOG4J2-150" dev="rgoers" type="fix">
         Convert all System.getProperty calls to use PropertiesUtil to suppress SecurityExceptions.
       </action>