[logback-dev] svn commit: r1917 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/rolling test/java/ch/qos/logback/core/rolling test/java/ch/qos/logback/core/util test/witness/rolling

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Oct 30 19:43:22 CET 2008


Author: ceki
Date: Thu Oct 30 19:43:22 2008
New Revision: 1917

Added:
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.3
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.3
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.0
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.1
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.2
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.3
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.4
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingFileAppender.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/Compare.java
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.2
   logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2

Log:

- fixed LBCORE-21, 

  TimeBasedRollingPolicy will now check at initialization time
  to see if an existing target file needs to be rolled over
  
- further refactoring of TimebasedRollingPolicyTest

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingFileAppender.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingFileAppender.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingFileAppender.java	Thu Oct 30 19:43:22 2008
@@ -1,7 +1,7 @@
 /**
- * Logback: the reliable, generic, fast and flexible logging framework.
+ * Logback: the generic, reliable, fast and flexible logging framework.
  * 
- * Copyright (C) 1999-2006, QOS.ch
+ * 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
@@ -25,7 +25,6 @@
  * 
  * @author Heinz Richter
  * @author Ceki Gülcü
- * @since  1.3
  * */
 public class RollingFileAppender<E> extends FileAppender<E> {
   File activeFileCache;

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	Thu Oct 30 19:43:22 2008
@@ -14,8 +14,8 @@
 import java.util.concurrent.Future;
 
 import ch.qos.logback.core.rolling.helper.AsynchronousCompressor;
-import ch.qos.logback.core.rolling.helper.Compressor;
 import ch.qos.logback.core.rolling.helper.CompressionMode;
+import ch.qos.logback.core.rolling.helper.Compressor;
 import ch.qos.logback.core.rolling.helper.DateTokenConverter;
 import ch.qos.logback.core.rolling.helper.FileNamePattern;
 import ch.qos.logback.core.rolling.helper.RenameUtil;
@@ -24,9 +24,8 @@
 
 /**
  * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite
- * powerful. It allows the roll over to be made based on time. It is
- * possible to specify that the roll over occur once per day, per week or 
- * per month.
+ * powerful. It allows the roll over to be made based on time. It is possible to
+ * specify that the roll over occur once per day, per week or per month.
  * 
  * <p>For more information, please refer to the online manual at
  * http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
@@ -37,21 +36,21 @@
     TriggeringPolicy<E> {
   static final String FNP_NOT_SET = "The FileNamePattern option must be set before using TimeBasedRollingPolicy. ";
   static final String SEE_FNP_NOT_SET = "See also http://logback.qos.ch/codes.html#tbr_fnp_not_set";
-  static final int DEFAULT_MAX_HISTORY = 0;
+  static final int NO_DELETE_HISTORY = 0;
 
   RollingCalendar rc;
   long currentTime;
   long nextCheck;
   // indicate whether the time has been forced or not
   boolean isTimeForced = false;
-  Date lastCheck = new Date();
+  Date lastCheck = null;
   String elapsedPeriodsFileName;
   FileNamePattern activeFileNamePattern;
   RenameUtil util = new RenameUtil();
   String lastGeneratedFileName;
   Future<?> future;
 
-  int maxHistory = DEFAULT_MAX_HISTORY;
+  int maxHistory = NO_DELETE_HISTORY;
   TimeBasedCleaner tbCleaner;
 
   public void setCurrentTime(long timeInMillis) {
@@ -113,15 +112,32 @@
         + "' from file name pattern '" + fileNamePattern.getPattern() + "'.");
     rc.printPeriodicity(this);
 
-    // currentTime = System.currentTimeMillis();
-    lastCheck.setTime(getCurrentTime());
+    // lastCheck can be set by test classes
+    // if it has not been set, we set it here
+    if (lastCheck == null) {
+      lastCheck = new Date();
+      lastCheck.setTime(getCurrentTime());
+      if (getParentFileName() != null) {
+        File currentFile = new File(getParentFileName());
+        if (currentFile.exists() && currentFile.canRead()) {
+          lastCheck.setTime(currentFile.lastModified());
+        }
+      }
+    }
     nextCheck = rc.getNextTriggeringMillis(lastCheck);
-
-    if (maxHistory != DEFAULT_MAX_HISTORY) {
+    
+    if (maxHistory != NO_DELETE_HISTORY) {
       tbCleaner = new TimeBasedCleaner(fileNamePattern, rc, maxHistory);
     }
   }
 
+  
+  // allow Test classes to act on the lastCheck field to simulate old
+  // log files needing rollover
+  void setLastCheck(Date _lastCheck) {
+    this.lastCheck = _lastCheck;
+  }
+
   public void rollover() throws RolloverFailure {
 
     // when rollover is called the elapsed period's file has
@@ -140,7 +156,7 @@
     if (tbCleaner != null) {
       tbCleaner.clean(new Date(getCurrentTime()));
     }
-    
+
     // let's update the parent active file name
     setParentFileName(getNewActiveFileName());
 
@@ -183,19 +199,17 @@
    * file equals the file name for the current period as computed by the
    * <b>FileNamePattern</b> option.
    * 
-   * <p>
-   * The RollingPolicy must know wether it is responsible for changing the name
-   * of the active file or not. If the active file name is set by the user via
-   * the configuration file, then the RollingPolicy must let it like it is. If
-   * the user does not specify an active file name, then the RollingPolicy
+   * <p> The RollingPolicy must know wether it is responsible for changing the
+   * name of the active file or not. If the active file name is set by the user
+   * via the configuration file, then the RollingPolicy must let it like it is.
+   * If the user does not specify an active file name, then the RollingPolicy
    * generates one.
    * 
-   * <p>
-   * To be sure that the file name used by the parent class has been generated
-   * by the RollingPolicy and not specified by the user, we keep track of the
-   * last generated name object and compare its reference to the parent file
-   * name. If they match, then the RollingPolicy knows it's responsible for the
-   * change of the file name.
+   * <p> To be sure that the file name used by the parent class has been
+   * generated by the RollingPolicy and not specified by the user, we keep track
+   * of the last generated name object and compare its reference to the parent
+   * file name. If they match, then the RollingPolicy knows it's responsible for
+   * the change of the file name.
    * 
    */
   public String getNewActiveFileName() {
@@ -214,21 +228,13 @@
     long time = getCurrentTime();
 
     if (time >= nextCheck) {
-      // addInfo("Time to trigger roll-over");
-      // We set the elapsedPeriodsFileName before we set the 'lastCheck'
-      // variable
+      // We set the elapsedPeriodsFileName before we set the 'lastCheck' variable
       // The elapsedPeriodsFileName corresponds to the file name of the period
       // that just elapsed.
       elapsedPeriodsFileName = activeFileNamePattern.convertDate(lastCheck);
-      // addInfo("elapsedPeriodsFileName set to "+elapsedPeriodsFileName);
 
       lastCheck.setTime(time);
       nextCheck = rc.getNextTriggeringMillis(lastCheck);
-
-      Date x = new Date();
-      x.setTime(nextCheck);
-      // addInfo("Next check on "+ x);
-
       return true;
     } else {
       return false;

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java	Thu Oct 30 19:43:22 2008
@@ -16,7 +16,6 @@
 import java.sql.Date;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Calendar;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -69,31 +68,27 @@
   Calendar cal = Calendar.getInstance();
   long currentTime; // initialized in setUp()
   long nextRolloverThreshold; // initialized in setUp()
-  List<String> filenameList = new ArrayList<String>();
+  List<String> expectedFilenameList = new ArrayList<String>();
 
   @Before
   public void setUp() {
     context.setName("test");
-    cal.set(Calendar.MILLISECOND, 0);
+    cal.set(Calendar.MILLISECOND, 333);
     currentTime = cal.getTimeInMillis();
     recomputeRolloverThreshold(currentTime);
+    System.out.println("currentTime=" + sdf.format(new Date(currentTime)));
 
     // Delete .log files
-    {
-      File target = new File(Constants.OUTPUT_DIR_PREFIX + "test4.log");
-      target.mkdirs();
-      target.delete();
-    }
-    {
-      File target = new File(Constants.OUTPUT_DIR_PREFIX + "test5.log");
-      target.mkdirs();
-      target.delete();
-    }
-    {
-      File target = new File(Constants.OUTPUT_DIR_PREFIX + "test6.log");
-      target.mkdirs();
-      target.delete();
-    }
+    deleteStaleLogFile("test4.log");
+    deleteStaleLogFile("test4B.log");
+    deleteStaleLogFile("test5.log");
+    deleteStaleLogFile("test6.log");
+  }
+
+  void deleteStaleLogFile(String filename) {
+    File target = new File(Constants.OUTPUT_DIR_PREFIX + filename);
+    target.mkdirs();
+    target.delete();
   }
 
   @After
@@ -122,61 +117,35 @@
     rfa.start();
   }
 
-  void addFileName(String testId, Date date, boolean compression) {
-    String fn = Constants.OUTPUT_DIR_PREFIX + testId + sdf.format(date);
-    if (compression) {
-      fn += ".gz";
-    }
-    filenameList.add(fn);
-  }
-
-  String[] computeFilenames(String testStr, boolean compression, String lastFile) {
-    String[] filenames = new String[3];
-    int oneBeforeLast = filenames.length - 1;
-    for (int i = 0; i < oneBeforeLast; i++) {
-      filenames[i] = Constants.OUTPUT_DIR_PREFIX + testStr
-          + sdf.format(cal.getTime());
-      if (compression) {
-        filenames[i] += ".gz";
-      }
-      cal.add(Calendar.SECOND, 1);
-    }
-    if (lastFile != null) {
-      filenames[oneBeforeLast] = Constants.OUTPUT_DIR_PREFIX + lastFile;
-    } else {
-      filenames[oneBeforeLast] = Constants.OUTPUT_DIR_PREFIX + testStr
-          + sdf.format(cal.getTime());
-    }
-    return filenames;
-  }
-
   /**
    * Test rolling without compression, file option left blank, no stop/start
    */
   @Test
   public void noCompression_FileBlank_NoRestart_1() throws Exception {
-    String testId = "test1-";
+    String testId = "test1";
     initRFA(rfa1, null);
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "%d{"
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", currentTime, 0);
 
     // compute the current filename
-    addFileName(testId, getTimeForElapsedPeriod(), false);
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), false);
 
     incCurrentTime(1100);
     tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i < 3; i++) {
-      addFileNameIfNecessary(testId, false);
       rfa1.doAppend("Hello---" + i);
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
       incCurrentTime(500);
       tbrp1.setCurrentTime(currentTime);
     }
 
+    System.out.println(expectedFilenameList);
+
     int i = 0;
-    for (String fn : filenameList) {
+    for (String fn : expectedFilenameList) {
       assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test1." + i++));
+          + "witness/rolling/tbr-" + testId + "." + i++));
     }
   }
 
@@ -185,19 +154,21 @@
    */
   @Test
   public void noCompression_FileBlank_StopRestart_2() throws Exception {
-    String testId = "test2-";
+    String testId = "test2";
 
     initRFA(rfa1, null);
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "%d{"
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", currentTime, 0);
 
-    addFileName(testId, getTimeForElapsedPeriod(), false);
+    // a new file is created by virtue of rfa.start();
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), false);
+
     incCurrentTime(1100);
     tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i <= 2; i++) {
-      addFileNameIfNecessary(testId, false);
       rfa1.doAppend("Hello---" + i);
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
       incCurrentTime(500);
       tbrp1.setCurrentTime(currentTime);
     }
@@ -205,20 +176,20 @@
     rfa1.stop();
 
     initRFA(rfa2, null);
-    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test2-%d{"
+    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", tbrp1.getCurrentTime(), 0);
 
     for (int i = 0; i <= 2; i++) {
-      addFileNameIfNecessary(testId, false);
-      rfa2.doAppend("Hello---" + i);
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
+      rfa2.doAppend("World---" + i);
       incCurrentTime(100);
       tbrp2.setCurrentTime(currentTime);
     }
 
     int i = 0;
-    for (String fn : filenameList) {
+    for (String fn : expectedFilenameList) {
       assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test2." + i++));
+          + "witness/rolling/tbr-" + testId + "." + i++));
     }
   }
 
@@ -227,35 +198,31 @@
    */
   @Test
   public void withCompression_FileBlank_NoRestart_3() throws Exception {
-    String testId = "test3-";
+    String testId = "test3";
     initRFA(rfa1, null);
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "%d{"
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}.gz", currentTime, 0);
 
-    String[] filenames = computeFilenames(testId, true, null);
-
-    addFileName(testId, getTimeForElapsedPeriod(), true);
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), true);
     incCurrentTime(1100);
     tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i < 3; i++) {
-      addFileNameIfNecessary(testId, true);
+      // when i == 2, file name should not have .gz extension
+      addExpectedFileNamedIfItsTime_ByDate(testId, i != 2);
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
     tbrp1.future.get(2000, TimeUnit.MILLISECONDS);
 
-    System.out.println(Arrays.toString(filenames));
-    System.out.println(filenameList.toString());
-
-    for (int i = 0; i < 2; i++) {
-      assertTrue(Compare.gzCompare(filenameList.get(i),
-          Constants.TEST_DIR_PREFIX + "witness/rolling/tbr-test3." + i + ".gz"));
+    int i = 0;
+    for (String fn : expectedFilenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-" + testId + "." + i + addGZIfNotLast(i)));
+      i++;
     }
-
-    assertTrue(Compare.compare(filenameList.get(2), Constants.TEST_DIR_PREFIX
-        + "witness/rolling/tbr-test3.2"));
   }
 
   /**
@@ -263,95 +230,89 @@
    */
   @Test
   public void noCompression_FileSet_StopRestart_4() throws Exception {
-    initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test4.log");
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test4-%d{"
+    String testId = "test4";
+    initRFA(rfa1, testId2FileName(testId));
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", currentTime, 0);
 
-    String[] filenames = computeFilenames("test4-", false, "test4.log");
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), false);
 
-    System.out.println("CT=" + sdf.format(new Date(currentTime)));
-    System.out.println("tbrp1 CT="
-        + sdf.format(new Date(tbrp1.getCurrentTime())));
-
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
-
-    System.out.println("tbrp1 CT="
-        + sdf.format(new Date(tbrp1.getCurrentTime())));
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i <= 2; i++) {
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
     rfa1.stop();
 
-    initRFA(rfa2, Constants.OUTPUT_DIR_PREFIX + "test4.log");
-    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test4-%d{"
-        + DATE_PATTERN + "}", tbrp1.getCurrentTime(), tbrp1.getCurrentTime());
+    initRFA(rfa2, testId2FileName(testId));
+    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
+        + DATE_PATTERN + "}", currentTime, currentTime);
 
     for (int i = 0; i <= 2; i++) {
       rfa2.doAppend("World---" + i);
-      tbrp2.setCurrentTime(addTime(tbrp2.getCurrentTime(), 100));
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
+      incCurrentTime(100);
+      tbrp2.setCurrentTime(currentTime);
     }
 
-    for (int i = 0; i < 3; i++) {
-      assertTrue(Compare.compare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test4." + i));
+    massageExpectedFilesToCorresponToCurrentTarget("test4.log");
+
+    int i = 0;
+    for (String fn : expectedFilenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-" + testId + "." + i++));
     }
   }
 
   @Test
   public void noCompression_FileSet_StopRestart_WithLongWait_4B()
       throws Exception {
-    initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test4B.log");
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test4B-%d{"
+    String testId = "test4B";
+    initRFA(rfa1, testId2FileName(testId));
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", currentTime, 0);
 
-    String[] filenames = computeFilenames("test4B-", false, "test4B.log");
-
-    System.out.println("CT=" + sdf.format(new Date(currentTime)));
-    System.out.println("tbrp1 CT="
-        + sdf.format(new Date(tbrp1.getCurrentTime())));
-
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), false);
 
-    System.out.println("tbrp1 CT="
-        + sdf.format(new Date(tbrp1.getCurrentTime())));
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i <= 2; i++) {
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
     rfa1.stop();
-    System.out.println("post stop tbrp1 CT="
-        + sdf.format(new Date(tbrp1.getCurrentTime())));
 
-    initRFA(rfa2, Constants.OUTPUT_DIR_PREFIX + "test4B.log");
-    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + "test4B-%d{"
-        + DATE_PATTERN + "}", tbrp1.getCurrentTime() + 3000, tbrp1
-        .getCurrentTime());
+    long fileTimestamp = currentTime;
+    incCurrentTime(2000);
 
-    System.out.println("tbrp2 CT="
-        + sdf.format(new Date(tbrp2.getCurrentTime())));
+    initRFA(rfa2, Constants.OUTPUT_DIR_PREFIX + "test4B.log");
+    initTRBP(rfa2, tbrp2, Constants.OUTPUT_DIR_PREFIX + testId +"-%d{"
+        + DATE_PATTERN + "}", currentTime, fileTimestamp);
 
     for (int i = 0; i <= 2; i++) {
       rfa2.doAppend("World---" + i);
-      System.out.println("in loop tbrp2 CT="
-          + sdf.format(new Date(tbrp2.getCurrentTime())));
-      tbrp2.setCurrentTime(addTime(tbrp2.getCurrentTime(), 500));
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
+      incCurrentTime(100);
+      tbrp2.setCurrentTime(currentTime);
     }
 
-    System.out.println("tbrp2 CT="
-        + sdf.format(new Date(tbrp2.getCurrentTime())));
+    massageExpectedFilesToCorresponToCurrentTarget("test4B.log");
 
-    if (1 == 1) {
-      return;
-    }
-    for (int i = 0; i < 3; i++) {
-      assertTrue(Compare.compare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test4." + i));
+    int i = 0;
+    for (String fn : expectedFilenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-test4B." + i++));
     }
+
   }
 
   /**
@@ -359,22 +320,30 @@
    */
   @Test
   public void noCompression_FileSet_NoRestart_5() throws Exception {
-    initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test5.log");
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test5-%d{"
+    String testId = "test5";
+
+    initRFA(rfa1, testId2FileName(testId));
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", currentTime, 0);
 
-    String[] filenames = computeFilenames("test5-", false, "test5.log");
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), false);
 
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i < 3; i++) {
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
+      addExpectedFileNamedIfItsTime_ByDate(testId, false);
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
-    for (int i = 0; i < 3; i++) {
-      assertTrue(Compare.compare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test5." + i));
+    massageExpectedFilesToCorresponToCurrentTarget("test5.log");
+
+    int i = 0;
+    for (String fn : expectedFilenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-test5." + i++));
     }
   }
 
@@ -383,47 +352,93 @@
    */
   @Test
   public void withCompression_FileSet_NoRestart_6() throws Exception {
-    initRFA(rfa1, Constants.OUTPUT_DIR_PREFIX + "test6.log");
-    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + "test6-%d{"
+
+    String testId = "test6";
+
+    initRFA(rfa1, testId2FileName(testId));
+    initTRBP(rfa1, tbrp1, Constants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}.gz", currentTime, 0);
 
-    String[] filenames = computeFilenames("test6-", true, "test6.log");
+    addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(), true);
 
-    tbrp1.setCurrentTime(addTime(currentTime, 1100));
+    incCurrentTime(1100);
+    tbrp1.setCurrentTime(currentTime);
 
     for (int i = 0; i < 3; i++) {
       rfa1.doAppend("Hello---" + i);
-      tbrp1.setCurrentTime(addTime(tbrp1.getCurrentTime(), 500));
+      addExpectedFileNamedIfItsTime_ByDate(testId, true);
+      incCurrentTime(500);
+      tbrp1.setCurrentTime(currentTime);
     }
 
     // wait for the compression task to finish
     tbrp1.future.get(1000, TimeUnit.MILLISECONDS);
 
-    for (int i = 0; i < 2; i++) {
-      assertTrue(Compare.gzCompare(filenames[i], Constants.TEST_DIR_PREFIX
-          + "witness/rolling/tbr-test6." + i + ".gz"));
-    }
+    massageExpectedFilesToCorresponToCurrentTarget("test6.log");
 
-    assertTrue(Compare.compare(filenames[2], Constants.TEST_DIR_PREFIX
-        + "witness/rolling/tbr-test6.2"));
+    int i = 0;
+    for (String fn : expectedFilenameList) {
+      assertTrue(Compare.compare(fn, Constants.TEST_DIR_PREFIX
+          + "witness/rolling/tbr-" + testId + "." + i + addGZIfNotLast(i)));
+      i++;
+    }
   }
 
   // =========================================================================
   // utility methods
   // =========================================================================
 
-  void addFileNameIfNecessary(String testId, boolean compression) {
+  String testId2FileName(String testId) {
+    return Constants.OUTPUT_DIR_PREFIX + testId + ".log";
+  }
+
+  void massageExpectedFilesToCorresponToCurrentTarget(String file) {
+    // we added one too many files by date
+    expectedFilenameList.remove(expectedFilenameList.size() - 1);
+    // since file is set, we have to add it
+    addExpectedFileName_ByFile(file);
+  }
+
+  String addGZIfNotLast(int i) {
+    int lastIndex = expectedFilenameList.size() - 1;
+    if (i != lastIndex) {
+      return ".gz";
+    } else {
+      return "";
+    }
+  }
+
+  void addExpectedFileName_ByDate(String testId, Date date, boolean gzExtension) {
+    String fn = Constants.OUTPUT_DIR_PREFIX + testId + "-" + sdf.format(date);
+    if (gzExtension) {
+      fn += ".gz";
+    }
+    expectedFilenameList.add(fn);
+  }
+
+  void addExpectedFileNamedIfItsTime_ByDate(String testId, boolean gzExtension) {
     if (passThresholdTime(nextRolloverThreshold)) {
-      addFileName(testId, getTimeForElapsedPeriod(), compression);
+      addExpectedFileName_ByDate(testId, getDateOfCurrentPeriodsStart(),
+          gzExtension);
       recomputeRolloverThreshold(currentTime);
     }
   }
 
-  Date getTimeForElapsedPeriod() {
+  void addExpectedFileName_ByFile(String filenameSuffix) {
+    String fn = Constants.OUTPUT_DIR_PREFIX + filenameSuffix;
+    expectedFilenameList.add(fn);
+  }
+
+  Date getDateOfCurrentPeriodsStart() {
     long delta = currentTime % 1000;
     return new Date(currentTime - delta);
   }
 
+  Date getDateOfPastPeriodsStart() {
+    long delta = currentTime % 1000;
+    return new Date(currentTime - delta - 1000);
+  }
+
   static long addTime(long currentTime, long timeToWait) {
     return currentTime + timeToWait;
   }
@@ -441,4 +456,9 @@
   void incCurrentTime(long increment) {
     currentTime += increment;
   }
+
+  void printLongAsDate(String msg, long time) {
+    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
+    System.out.println(msg + sdf.format(new Date(time)));
+  }
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/Compare.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/Compare.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/Compare.java	Thu Oct 30 19:43:22 2008
@@ -18,15 +18,41 @@
 import java.io.InputStreamReader;
 import java.util.zip.GZIPInputStream;
 
-
 public class Compare {
   static final int B1_NULL = -1;
   static final int B2_NULL = -2;
 
-  public static boolean compare(String file1, String file2)
-    throws FileNotFoundException, IOException {
+  public static boolean compare(String file1, String file2) throws FileNotFoundException, IOException {
+    if (file1.endsWith(".gz")) {
+      //System.out.println(file1 +" is a gz file");
+      return gzFileCompare(file1, file2);
+    } else {
+      return regularFileCompare(file1, file2);
+    }
+  }
+
+  static BufferedReader gzFileToBufferedReader(String file) throws IOException {
+    FileInputStream fis = new FileInputStream(file);
+    GZIPInputStream gzis = new GZIPInputStream(fis);
+    BufferedReader br = new BufferedReader(new InputStreamReader(gzis));
+    return br;
+  }
+  
+  public static boolean gzFileCompare(String file1, String file2) throws IOException {
+    BufferedReader in1 = gzFileToBufferedReader(file1);
+    BufferedReader in2 = gzFileToBufferedReader(file2);
+    return bufferCompare(in1, in2, file1, file2);
+  }
+
+  public static boolean regularFileCompare(String file1, String file2)
+      throws FileNotFoundException, IOException {
     BufferedReader in1 = new BufferedReader(new FileReader(file1));
     BufferedReader in2 = new BufferedReader(new FileReader(file2));
+    return bufferCompare(in1, in2, file1, file2);
+  }
+
+  public static boolean bufferCompare(BufferedReader in1, BufferedReader in2,
+      String file1, String file2) throws FileNotFoundException, IOException {
 
     String s1;
     int lineCounter = 0;
@@ -37,9 +63,8 @@
       String s2 = in2.readLine();
 
       if (!s1.equals(s2)) {
-        System.out.println(
-          "Files [" + file1 + "] and [" + file2 + "] differ on line "
-          + lineCounter);
+        System.out.println("Files [" + file1 + "] and [" + file2
+            + "] differ on line " + lineCounter);
         System.out.println("One reads:  [" + s1 + "].");
         System.out.println("Other reads:[" + s2 + "].");
         outputFile(file1);
@@ -51,8 +76,8 @@
 
     // the second file is longer
     if (in2.read() != -1) {
-      System.out.println(
-        "File [" + file2 + "] longer than file [" + file1 + "].");
+      System.out.println("File [" + file2 + "] longer than file [" + file1
+          + "].");
       outputFile(file1);
       outputFile(file2);
 
@@ -62,13 +87,13 @@
     return true;
   }
 
-  /** 
+  /**
    * 
    * Prints file on the console.
-   *
+   * 
    */
-  private static void outputFile(String file)
-    throws FileNotFoundException, IOException {
+  private static void outputFile(String file) throws FileNotFoundException,
+      IOException {
     BufferedReader in1 = new BufferedReader(new FileReader(file));
 
     String s1;
@@ -93,43 +118,44 @@
       System.out.println(s1);
     }
   }
-  
-    public static boolean gzCompare(String file1, String file2)
-      throws FileNotFoundException, IOException {
-      BufferedReader in1 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file1))));      
-      BufferedReader in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file2))));
 
-      String s1;
-      int lineCounter = 0;
+  public static boolean gzCompare(String file1, String file2)
+      throws FileNotFoundException, IOException {
+    BufferedReader in1 = new BufferedReader(new InputStreamReader(
+        new GZIPInputStream(new FileInputStream(file1))));
+    BufferedReader in2 = new BufferedReader(new InputStreamReader(
+        new GZIPInputStream(new FileInputStream(file2))));
 
-      while ((s1 = in1.readLine()) != null) {
-        lineCounter++;
+    String s1;
+    int lineCounter = 0;
 
-        String s2 = in2.readLine();
-
-        if (!s1.equals(s2)) {
-          System.out.println(
-            "Files [" + file1 + "] and [" + file2 + "] differ on line "
-            + lineCounter);
-          System.out.println("One reads:  [" + s1 + "].");
-          System.out.println("Other reads:[" + s2 + "].");
-          outputFile(file1);
-          outputFile(file2);
+    while ((s1 = in1.readLine()) != null) {
+      lineCounter++;
 
-          return false;
-        }
-      }
+      String s2 = in2.readLine();
 
-      // the second file is longer
-      if (in2.read() != -1) {
-        System.out.println(
-          "File [" + file2 + "] longer than file [" + file1 + "].");
+      if (!s1.equals(s2)) {
+        System.out.println("Files [" + file1 + "] and [" + file2
+            + "] differ on line " + lineCounter);
+        System.out.println("One reads:  [" + s1 + "].");
+        System.out.println("Other reads:[" + s2 + "].");
         outputFile(file1);
         outputFile(file2);
 
         return false;
       }
+    }
 
-      return true;
+    // the second file is longer
+    if (in2.read() != -1) {
+      System.out.println("File [" + file2 + "] longer than file [" + file1
+          + "].");
+      outputFile(file1);
+      outputFile(file2);
+
+      return false;
     }
+
+    return true;
+  }
 }

Modified: logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.2
==============================================================================
--- logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.2	(original)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.2	Thu Oct 30 19:43:22 2008
@@ -1,4 +1,2 @@
 Hello---2
-Hello---0
-Hello---1
-Hello---2
+World---0
\ No newline at end of file

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.3
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test2.3	Thu Oct 30 19:43:22 2008
@@ -0,0 +1,2 @@
+World---1
+World---2
\ No newline at end of file

Modified: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2
==============================================================================
--- logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2	(original)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.2	Thu Oct 30 19:43:22 2008
@@ -1,4 +1,2 @@
 Hello---2
 World---0
-World---1
-World---2

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.3
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4.3	Thu Oct 30 19:43:22 2008
@@ -0,0 +1,2 @@
+World---1
+World---2

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.0
==============================================================================

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.1
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.1	Thu Oct 30 19:43:22 2008
@@ -0,0 +1,2 @@
+Hello---0
+Hello---1
\ No newline at end of file

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.2
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.2	Thu Oct 30 19:43:22 2008
@@ -0,0 +1 @@
+Hello---2

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.3
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.3	Thu Oct 30 19:43:22 2008
@@ -0,0 +1 @@
+World---0
\ No newline at end of file

Added: logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.4
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/witness/rolling/tbr-test4B.4	Thu Oct 30 19:43:22 2008
@@ -0,0 +1,2 @@
+World---1
+World---2
\ No newline at end of file


More information about the logback-dev mailing list