svn commit: r1826 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core main/java/ch/qos/logback/core/util test/java/ch/qos/logback/core/appender test/java/ch/qos/logback/core/util

Author: ceki Date: Wed Oct 8 22:38:22 2008 New Revision: 1826 Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/FileUtil.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/FileUtilTest.java Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/FileAppender.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/FileAppenderTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java Log: LBCORE-42 FileAppender will now automatically create parent directories as needed. Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/FileAppender.java ============================================================================== --- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/FileAppender.java (original) +++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/FileAppender.java Wed Oct 8 22:38:22 2008 @@ -10,11 +10,13 @@ package ch.qos.logback.core; import java.io.BufferedWriter; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import ch.qos.logback.core.status.ErrorStatus; import ch.qos.logback.core.status.InfoStatus; +import ch.qos.logback.core.util.FileUtil; /** * FileAppender appends log events to a file. @@ -79,7 +81,7 @@ /** * If the value of <b>File</b> is not <code>null</code>, then - * {@link #setFile} is called with the values of <b>File</b> and <b>Append</b> + * {@link #openFile} is called with the values of <b>File</b> and <b>Append</b> * properties. */ public void start() { @@ -96,7 +98,7 @@ this)); } try { - setFile(); + openFile(); } catch (java.io.IOException e) { errors++; @@ -136,11 +138,18 @@ * @throws IOException * */ - public synchronized void setFile() throws IOException { + public synchronized void openFile() throws IOException { closeWriter(); + File file = new File(fileName); + if(FileUtil.mustCreateParentDirectories(file)) { + boolean result = FileUtil.createMissingParentDirectories(file); + if(!result) { + addError("Failed to create parent directories for ["+file.getAbsolutePath()+"]"); + } + } + this.writer = createWriter(new FileOutputStream(fileName, append)); - if (bufferedIO) { this.writer = new BufferedWriter(this.writer, bufferSize); } Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/FileUtil.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/FileUtil.java Wed Oct 8 22:38:22 2008 @@ -0,0 +1,36 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ +package ch.qos.logback.core.util; + +import java.io.File; + +public class FileUtil { + + + public static boolean mustCreateParentDirectories(File file) { + File parent = file.getParentFile(); + if(parent != null && !parent.exists()) { + return true; + } else { + return false; + } + } + + public static boolean createMissingParentDirectories(File file) { + File parent = file.getParentFile(); + if(parent == null || parent.exists()) { + throw new IllegalStateException(file + " should not have a null parent"); + } + if(parent.exists()) { + throw new IllegalStateException(file + " should not have existing parent directory"); + } + return parent.mkdirs(); + } +} Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/FileAppenderTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/FileAppenderTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/appender/FileAppenderTest.java Wed Oct 8 22:38:22 2008 @@ -9,11 +9,16 @@ */ package ch.qos.logback.core.appender; +import java.io.File; +import java.util.Random; + import ch.qos.logback.core.AppenderBase; import ch.qos.logback.core.ContextBase; import ch.qos.logback.core.FileAppender; import ch.qos.logback.core.layout.DummyLayout; import ch.qos.logback.core.layout.NopLayout; +import ch.qos.logback.core.util.Constants; +import ch.qos.logback.core.util.FileUtil; public class FileAppenderTest extends AbstractAppenderTest { @@ -45,14 +50,43 @@ } public void test() { + String filename = Constants.OUTPUT_DIR_PREFIX+"temp.log"; + FileAppender<Object> appender = new FileAppender<Object>(); appender.setLayout(new DummyLayout<Object>()); appender.setAppend(false); - appender.setFile("temp.log"); + appender.setFile(filename); appender.setName("temp.log"); appender.setContext(new ContextBase()); appender.start(); appender.doAppend(new Object()); + appender.stop(); + + File file = new File(filename); + assertTrue(file.exists()); + assertTrue("failed to delete "+file.getAbsolutePath(), file.delete()); + } + + public void testCreateParentFolders() { + int diff = new Random().nextInt(100); + String filename = Constants.OUTPUT_DIR_PREFIX+"/fat"+diff+"/testing.txt"; + File file = new File(filename); + FileAppender<Object> appender = new FileAppender<Object>(); + appender.setLayout(new DummyLayout<Object>()); + appender.setAppend(false); + appender.setFile(filename); + appender.setName("testCreateParentFolders"); + appender.setContext(new ContextBase()); + appender.start(); + appender.doAppend(new Object()); + appender.stop(); + assertFalse(FileUtil.mustCreateParentDirectories(file)); + assertTrue(file.exists()); + + // cleanup + assertTrue("failed to delete "+file.getAbsolutePath(), file.delete()); + File parent = file.getParentFile(); + assertTrue("failed to delete "+parent.getAbsolutePath(), parent.delete()); } } Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/FileUtilTest.java ============================================================================== --- (empty file) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/FileUtilTest.java Wed Oct 8 22:38:22 2008 @@ -0,0 +1,68 @@ +/** + * Logback: the generic, reliable, fast and flexible logging framework. + * + * Copyright (C) 2000-2008, QOS.ch + * + * This library is free software, you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation. + */ +package ch.qos.logback.core.util; + + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FileUtilTest { + + List<File> cleanupList = new ArrayList<File>(); + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + for(File f: cleanupList) { + f.delete(); + } + } + + + @Test + public void smoke() { + int diff = new Random().nextInt(100); + File file = new File(Constants.OUTPUT_DIR_PREFIX+"/fu"+diff+"/testing.txt"); + // these will be deleted later + cleanupList.add(file); + cleanupList.add(file.getParentFile()); + + assertTrue(FileUtil.mustCreateParentDirectories(file)); + assertTrue(FileUtil.createMissingParentDirectories(file)); + assertFalse(FileUtil.mustCreateParentDirectories(file)); + } + + @Test + public void smokeII() { + int diff = new Random().nextInt(100); + File file = new File(Constants.OUTPUT_DIR_PREFIX+"/fu"+diff+"/bla/testing.txt"); + // these will be deleted later + cleanupList.add(file); + cleanupList.add(file.getParentFile()); + cleanupList.add(file.getParentFile().getParentFile()); + + assertTrue(FileUtil.mustCreateParentDirectories(file)); + assertTrue(FileUtil.createMissingParentDirectories(file)); + assertFalse(FileUtil.mustCreateParentDirectories(file)); + } +} Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java Wed Oct 8 22:38:22 2008 @@ -9,6 +9,7 @@ */ package ch.qos.logback.core.util; +import junit.framework.JUnit4TestAdapter; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; @@ -20,6 +21,7 @@ TestSuite suite = new TestSuite(); suite.addTestSuite(DurationTest.class); suite.addTestSuite(FileSizeTest.class); + suite.addTest(new JUnit4TestAdapter(FileUtilTest.class)); suite.addTestSuite(OptionHelperTest.class); suite.addTestSuite(StatusPrinterTest.class); suite.addTestSuite(TimeUtilTest.class);
participants (1)
-
noreply.ceki@qos.ch