
Author: ceki Date: Fri Jul 31 15:47:12 2009 New Revision: 2389 Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java Log: - cleaned up SizeBasedRollingTest Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java ============================================================================== --- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java (original) +++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java Fri Jul 31 15:47:12 2009 @@ -114,7 +114,7 @@ if (toRename.exists()) { util.rename(toRenameStr, fileNamePattern.convertInt(i + 1)); } else { - addInfo("Skipping roll=over for inexistent file " + toRenameStr); + addInfo("Skipping roll-over for inexistent file " + toRenameStr); } } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java Fri Jul 31 15:47:12 2009 @@ -8,6 +8,7 @@ import java.sql.Date; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.List; @@ -19,17 +20,17 @@ import ch.qos.logback.core.util.CoreTestConstants; /** - * Scaffolding for various rolling tests. Some assumptions are made: - * - rollover periodicity is 1 second (without precluding size based roll-over) + * Scaffolding for various rolling tests. Some assumptions are made: - rollover + * periodicity is 1 second (without precluding size based roll-over) * * @author Ceki Gülcü - * + * */ public class ScaffoldingForRollingTests { - + static final String DATE_PATTERN_WITH_SECONDS = "yyyy-MM-dd_HH_mm_ss"; SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_WITH_SECONDS); - + int diff = RandomUtil.getPositiveInt(); String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/"; EchoLayout<Object> layout = new EchoLayout<Object>(); @@ -39,7 +40,6 @@ long nextRolloverThreshold; // initialized in setUp() long currentTime; // initialized in setUp() Calendar cal = Calendar.getInstance(); - public void setUp() { context.setName("test"); @@ -49,35 +49,55 @@ System.out.println(randomOutputDir); } - void existenceCheck(String filename) { + public static void existenceCheck(String filename) { assertTrue("File " + filename + " does not exist", new File(filename) .exists()); } - - void contentCheck(int runLength, String prefix) throws IOException { - File outputDir = new File(randomOutputDir); - File[] fileArray = outputDir.listFiles(); + + public static File[] getFilesInDirectory(String outputDirStr) { + File outputDir = new File(outputDirStr); + return outputDir.listFiles(); + } + + public static void fileContentCheck(File[] fileArray, int runLength, + String prefix) throws IOException { List<String> stringList = new ArrayList<String>(); for (File file : fileArray) { FileToBufferUtil.readIntoList(file, stringList); } - + List<String> witnessList = new ArrayList<String>(); - - for(int i = 0; i < runLength; i++) { - witnessList.add(prefix+i); + + for (int i = 0; i < runLength; i++) { + witnessList.add(prefix + i); } assertEquals(witnessList, stringList); } - - void existenceCheck(List<String> filenameList) { + + public static void contentCheck(String outputDirStr, int runLength, + String prefix) throws IOException { + File[] fileArray = getFilesInDirectory(outputDirStr); + fileContentCheck(fileArray, runLength, prefix); + } + + public static void reverseOrderedContentCheck(String outputDirStr, + int runLength, String prefix) throws IOException { + File[] fileArray = getFilesInDirectory(outputDirStr); + File[] reversedArray = new File[fileArray.length]; + for (int i = 0; i < fileArray.length; i++) { + reversedArray[fileArray.length - 1 - i] = fileArray[i]; + } + System.out.println(Arrays.toString(reversedArray)); + fileContentCheck(reversedArray, runLength, prefix); + } + + public static void existenceCheck(List<String> filenameList) { for (String filename : filenameList) { assertTrue("File " + filename + " does not exist", new File(filename) .exists()); } } - - + String testId2FileName(String testId) { return randomOutputDir + testId + ".log"; } @@ -87,15 +107,15 @@ long delta = ct % 1000; nextRolloverThreshold = (ct - delta) + 1000; } - + boolean passThresholdTime(long nextRolloverThreshold) { return currentTime >= nextRolloverThreshold; } - + void incCurrentTime(long increment) { currentTime += increment; } - + Date getDateOfCurrentPeriodsStart() { long delta = currentTime % 1000; return new Date(currentTime - delta); Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java Fri Jul 31 15:47:12 2009 @@ -87,7 +87,7 @@ massageExpectedFilesToCorresponToCurrentTarget(file); existenceCheck(expectedFilenameList); - contentCheck(runLength, prefix); + contentCheck(randomOutputDir, runLength, prefix); } @Test @@ -116,7 +116,7 @@ } existenceCheck(expectedFilenameList); - contentCheck(runLength, prefix); + contentCheck(randomOutputDir, runLength, prefix); } void massageExpectedFilesToCorresponToCurrentTarget(String file) { Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java Fri Jul 31 15:47:12 2009 @@ -1,22 +1,14 @@ -/* - * Copyright 1999,2005 The Apache Software Foundation. - * - * Licensed 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 +/** + * Logback: the generic, reliable, fast and flexible logging framework. * - * http://www.apache.org/licenses/LICENSE-2.0 + * Copyright (C) 2000-2009, QOS.ch * - * 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. + * 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.rolling; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.File; @@ -29,8 +21,7 @@ import ch.qos.logback.core.ContextBase; import ch.qos.logback.core.Layout; import ch.qos.logback.core.layout.DummyLayout; -import ch.qos.logback.core.testUtil.Env; -import ch.qos.logback.core.util.Compare; +import ch.qos.logback.core.layout.EchoLayout; import ch.qos.logback.core.util.CoreTestConstants; /** @@ -41,19 +32,21 @@ * @author Sébastien Pennec * */ -public class SizeBasedRollingTest { - +public class SizeBasedRollingTest extends ScaffoldingForRollingTests { @Before + @Override public void setUp() { + super.setUp(); { File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.log"); + + "sizeBased-smoke.log"); target.mkdirs(); target.delete(); } { - File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "sbr-test3.log"); + File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + + "sbr-test3.log"); target.mkdirs(); target.delete(); } @@ -93,8 +86,6 @@ } catch (IllegalStateException e) { return; } - - // StatusPrinter.print(context.getStatusManager()); } /** @@ -104,14 +95,14 @@ public void smoke() throws Exception { Context context = new ContextBase(); - DummyLayout<Object> layout = new DummyLayout<Object>("0123456789"); + EchoLayout<Object> layout = new EchoLayout<Object>(); RollingFileAppender<Object> rfa = new RollingFileAppender<Object>(); rfa.setName("ROLLING"); rfa.setLayout(layout); rfa.setContext(context); - rfa.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.log"); - + // make the .log show first + rfa.setFile(randomOutputDir + "a-sizeBased-smoke.log"); + FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy(); swrp.setContext(context); SizeBasedTriggeringPolicy<Object> sbtp = new SizeBasedTriggeringPolicy<Object>(); @@ -119,11 +110,7 @@ sbtp.setMaxFileSize("100"); swrp.setMinIndex(0); -// swrp.setActiveFileName(Constants.TEST_DIR_PREFIX -// + "output/sizeBased-test2.log"); - - swrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.%i"); + swrp.setFileNamePattern(randomOutputDir + "sizeBased-smoke.%i"); swrp.setParent(rfa); swrp.start(); @@ -131,46 +118,19 @@ rfa.setTriggeringPolicy(sbtp); rfa.start(); - // Write exactly 10 bytes with each log - // for (int i = 0; i < 25; i++) { - // Thread.sleep(100); - // if (i < 10) { - // rfa.doAppend("Hello---" + i); - // //logger.debug("Hello---" + i); - // } else if (i < 100) { - // rfa.doAppend("Hello---" + i); - // //logger.debug("Hello--" + i); - // } - // } - - for (int i = 0; i < 45; i++) { + int runLength = 45; + String prefix = "hello"; + for (int i = 0; i < runLength; i++) { Thread.sleep(10); - rfa.doAppend("hello"); + rfa.doAppend(prefix+i); } - assertTrue(new File(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.log").exists()); - assertTrue(new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "sizeBased-test2.0") - .exists()); - assertTrue(new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "sizeBased-test2.1") - .exists()); - - // The File.length() method is not accurate under Windows - - if (!Env.isWindows()) { - - assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.log", CoreTestConstants.TEST_DIR_PREFIX - + "witness/rolling/sbr-test2.l")); - assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.0", CoreTestConstants.TEST_DIR_PREFIX - + "witness/rolling/sbr-test2.0")); - assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sizeBased-test2.1", CoreTestConstants.TEST_DIR_PREFIX - + "witness/rolling/sbr-test2.1")); - } + expectedFilenameList.add(randomOutputDir + "a-sizeBased-smoke.log"); + expectedFilenameList.add(randomOutputDir + "sizeBased-smoke.0"); + expectedFilenameList.add(randomOutputDir + "sizeBased-smoke.1"); + existenceCheck(expectedFilenameList); - // StatusPrinter.print(context.getStatusManager()); + reverseOrderedContentCheck(randomOutputDir, runLength, prefix); } /** @@ -179,11 +139,11 @@ @Test public void test3() throws Exception { Context context = new ContextBase(); - DummyLayout<Object> layout = new DummyLayout<Object>("0123456789"); + EchoLayout<Object> layout = new EchoLayout<Object>(); RollingFileAppender<Object> rfa = new RollingFileAppender<Object>(); rfa.setLayout(layout); rfa.setContext(context); - rfa.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "sbr-test3.log"); + rfa.setFile(randomOutputDir + "sbr-test3.log"); FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy(); fwrp.setContext(context); @@ -192,54 +152,29 @@ sbtp.setMaxFileSize("100"); fwrp.setMinIndex(0); - //fwrp.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/sbr-test3.log"); - fwrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX - + "sbr-test3.%i.gz"); + // fwrp.setActiveFileName(Constants.TEST_DIR_PREFIX + + // "output/sbr-test3.log"); + fwrp.setFileNamePattern(randomOutputDir + "sbr-test3.%i.gz"); fwrp.setParent(rfa); fwrp.start(); rfa.setRollingPolicy(fwrp); rfa.setTriggeringPolicy(sbtp); rfa.start(); - // Write exactly 10 bytes with each log - // for (int i = 0; i < 25; i++) { - // Thread.sleep(100); - // if (i < 10) { - // rfa.doAppend("Hello---" + i); - // //logger.debug("Hello---" + i); - // } else if (i < 100) { - // rfa.doAppend("Hello---" + i); - // //logger.debug("Hello--" + i); - // } - // } - - for (int i = 0; i < 25; i++) { + int runLength = 40; + String prefix = "hello"; + for (int i = 0; i < runLength; i++) { Thread.sleep(10); - rfa.doAppend("hello"); + rfa.doAppend("hello"+i); } - assertTrue(new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "sbr-test3.log") - .exists()); - assertTrue(new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "sbr-test3.0.gz") - .exists()); - assertTrue(new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "sbr-test3.1.gz") - .exists()); - - if (!Env.isWindows()) { - - assertTrue(Compare.compare( - CoreTestConstants.OUTPUT_DIR_PREFIX+"sbr-test3.log", - CoreTestConstants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.l")); - assertTrue(Compare.gzCompare( - CoreTestConstants.OUTPUT_DIR_PREFIX+"sbr-test3.0.gz", - CoreTestConstants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.0.gz")); - assertTrue(Compare.gzCompare( - CoreTestConstants.OUTPUT_DIR_PREFIX+"sbr-test3.1.gz", - CoreTestConstants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.1.gz")); - } + expectedFilenameList.add(randomOutputDir + "sbr-test3.log"); + expectedFilenameList.add(randomOutputDir + "sbr-test3.0.gz"); + expectedFilenameList.add(randomOutputDir + "sbr-test3.1.gz"); - // StatusPrinter.print(context.getStatusManager()); + existenceCheck(expectedFilenameList); + reverseOrderedContentCheck(randomOutputDir, runLength, prefix); + } - } Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java ============================================================================== --- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java (original) +++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java Fri Jul 31 15:47:12 2009 @@ -16,12 +16,21 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.List; +import java.util.zip.GZIPInputStream; public class FileToBufferUtil { static public void readIntoList(File file, List<String> stringList) throws IOException { + if(file.getName().endsWith(".gz")) { + gzFileReadIntoList(file, stringList); + } else { + regularReadIntoList(file, stringList); + } + } + + static public void regularReadIntoList(File file, List<String> stringList) throws IOException { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String line; @@ -30,4 +39,16 @@ } in.close(); } + + static public void gzFileReadIntoList(File file, List<String> stringList) throws IOException { + FileInputStream fis = new FileInputStream(file); + GZIPInputStream gzis = new GZIPInputStream(fis); + BufferedReader in = new BufferedReader(new InputStreamReader(gzis)); + String line; + while( (line = in.readLine()) != null) { + stringList.add(line); + } + in.close(); + } + }