[logback-dev] [GIT] Logback: the generic, reliable, fast and flexible logging framework. branch, master, updated. v_0.9.29-22-g524e0fc

added by portage for gitosis-gentoo git-noreply at pixie.qos.ch
Tue Sep 6 22:19:07 CEST 2011


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Logback: the generic, reliable, fast and flexible logging framework.".

The branch, master has been updated
       via  524e0fc13ecc4fb24d1e2515adc47d8711cf3654 (commit)
      from  6f2735660d8c650b7be9e682c2fe85764592a340 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=524e0fc13ecc4fb24d1e2515adc47d8711cf3654
http://github.com/ceki/logback/commit/524e0fc13ecc4fb24d1e2515adc47d8711cf3654

commit 524e0fc13ecc4fb24d1e2515adc47d8711cf3654
Author: Ceki Gulcu <ceki at qos.ch>
Date:   Tue Sep 6 22:00:31 2011 +0200

    fixing LBCORE-147 and adding relavant test cases

diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java b/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java
index 9e9d7b1..c2d4da5 100644
--- a/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java
+++ b/logback-classic/src/main/java/ch/qos/logback/classic/spi/PackagingDataCalculator.java
@@ -86,7 +86,7 @@ public class PackagingDataCalculator {
       if (callerClass != null && stepClassname.equals(callerClass.getName())) {
         lastExactClassLoader = callerClass.getClassLoader();
         if (firsExactClassLoader == null) {
-          firsExactClassLoader = callerClass.getClassLoader();
+          firsExactClassLoader = lastExactClassLoader;
         }
         ClassPackagingData pi = calculateByExactType(callerClass);
         step.setClassPackagingData(pi);
diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
index 0c2c44a..bda2ad2 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
@@ -35,7 +35,7 @@ public class SizeAndTimeBasedFNATP<E> extends
     // in super.start()
     super.start();
 
-    archiveRemover = new SizeAndTimeBasedArchiveRemover(tbrp.fileNamePattern, rc, getCurrentTime());
+    archiveRemover = new SizeAndTimeBasedArchiveRemover(tbrp.fileNamePattern, rc);
     archiveRemover.setContext(context);
 
     // we need to get the correct value of currentPeriodsCounter.
diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java
index 7fbcf50..128de48 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/DefaultArchiveRemover.java
@@ -16,28 +16,63 @@ package ch.qos.logback.core.rolling.helper;
 import java.io.File;
 import java.util.Date;
 
+import ch.qos.logback.core.CoreConstants;
 import ch.qos.logback.core.pattern.Converter;
 import ch.qos.logback.core.pattern.LiteralConverter;
 import ch.qos.logback.core.spi.ContextAwareBase;
 
 abstract public class DefaultArchiveRemover extends ContextAwareBase implements
-    ArchiveRemover {
+        ArchiveRemover {
+
+  static protected final long UNINITIALIZED = -1;
+  // aim for 64 days, except in case of hourly rollover
+  static protected final long INACTIVITY_TOLERANCE_IN_MILLIS = 64L * (long) CoreConstants.MILLIS_IN_ONE_DAY;
+  static final int MAX_VALUE_FOR_INACTIVITY_PERIODS = 14 * 24; // 14 days in case of hourly rollover
 
   final FileNamePattern fileNamePattern;
   final RollingCalendar rc;
   int periodOffsetForDeletionTarget;
   final boolean parentClean;
-  long lastHeartBeat;
+  long lastHeartBeat = UNINITIALIZED;
 
   public DefaultArchiveRemover(FileNamePattern fileNamePattern,
-      RollingCalendar rc, long currentTime) {
+                               RollingCalendar rc) {
     this.fileNamePattern = fileNamePattern;
     this.rc = rc;
     this.parentClean = computeParentCleaningFlag(fileNamePattern);
-    this.lastHeartBeat = currentTime;
   }
 
 
+  int computeElapsedPeriodsSinceLastClean(long nowInMillis) {
+    long periodsElapsed = 0;
+    if (lastHeartBeat == UNINITIALIZED) {
+      periodsElapsed = rc.periodsElapsed(nowInMillis, nowInMillis + INACTIVITY_TOLERANCE_IN_MILLIS);
+    } else {
+      periodsElapsed = rc.periodsElapsed(lastHeartBeat, nowInMillis);
+      if (periodsElapsed < 1) {
+        addWarn("Unexpected periodsElapsed value " + periodsElapsed);
+        periodsElapsed = 1;
+      }
+    }
+    if (periodsElapsed > MAX_VALUE_FOR_INACTIVITY_PERIODS)
+      periodsElapsed = MAX_VALUE_FOR_INACTIVITY_PERIODS;
+    return (int) periodsElapsed;
+  }
+
+  public void clean(Date now) {
+    long nowInMillis = now.getTime();
+    int periodsElapsed = computeElapsedPeriodsSinceLastClean(nowInMillis);
+    lastHeartBeat = nowInMillis;
+    if (periodsElapsed > 1) {
+      addInfo("periodsElapsed = " + periodsElapsed);
+    }
+    for (int i = 0; i < periodsElapsed; i++) {
+      if (periodsElapsed > 1) addInfo("i = " + i);
+      cleanByPeriodOffset(now, periodOffsetForDeletionTarget - i);
+    }
+  }
+
+  abstract void cleanByPeriodOffset(Date now, int periodOffset);
 
   boolean computeParentCleaningFlag(FileNamePattern fileNamePattern) {
     DateTokenConverter dtc = fileNamePattern.getDateTokenConverter();
@@ -80,7 +115,7 @@ abstract public class DefaultArchiveRemover extends ContextAwareBase implements
    * Will remove the directory passed as parameter if empty. After that, if the
    * parent is also becomes empty, remove the parent dir as well but at most 3
    * times.
-   * 
+   *
    * @param dir
    * @param depth
    */
@@ -90,7 +125,7 @@ abstract public class DefaultArchiveRemover extends ContextAwareBase implements
       return;
     }
     if (dir.isDirectory() && FileFilterUtil.isEmptyDirectory(dir)) {
-      addInfo("deleting folder [" + dir +"]");
+      addInfo("deleting folder [" + dir + "]");
       dir.delete();
       removeFolderIfEmpty(dir.getParentFile(), depth + 1);
     }
diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java
index bece6c3..329ba8b 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java
@@ -65,7 +65,7 @@ public class FileFilterUtil {
 
   /**
    * Return the set of files matching the stemRegex as found in 'directory'. A
-   * stemRegex does not contain any slash characters or any folder seperators.
+   * stemRegex does not contain any slash characters or any folder separators.
    * 
    * @param file
    * @param stemRegex
diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java
index cd6850c..d4bd02b 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/RollingCalendar.java
@@ -140,7 +140,7 @@ public class RollingCalendar extends GregorianCalendar {
     }
   }
 
-  public int periodsElapsed(long start, long end) {
+  public long periodsElapsed(long start, long end) {
     if (start > end)
       throw new IllegalArgumentException("Start cannot come before end");
 
@@ -148,17 +148,17 @@ public class RollingCalendar extends GregorianCalendar {
     switch (periodicityType) {
 
       case TOP_OF_MILLISECOND:
-        return (int) diff;
+        return diff;
       case TOP_OF_SECOND:
-        return (int) diff / CoreConstants.MILLIS_IN_ONE_SECOND;
+        return diff / CoreConstants.MILLIS_IN_ONE_SECOND;
       case TOP_OF_MINUTE:
-        return (int) diff / CoreConstants.MILLIS_IN_ONE_MINUTE;
+        return diff / CoreConstants.MILLIS_IN_ONE_MINUTE;
       case TOP_OF_HOUR:
         return (int) diff / CoreConstants.MILLIS_IN_ONE_HOUR;
       case TOP_OF_DAY:
-        return (int) diff / CoreConstants.MILLIS_IN_ONE_DAY;
+        return diff / CoreConstants.MILLIS_IN_ONE_DAY;
       case TOP_OF_WEEK:
-        return (int) diff / CoreConstants.MILLIS_IN_ONE_WEEK;
+        return diff / CoreConstants.MILLIS_IN_ONE_WEEK;
       case TOP_OF_MONTH:
         return diffInMonths(start, end);
       default:
@@ -166,7 +166,7 @@ public class RollingCalendar extends GregorianCalendar {
     }
   }
 
-  public static int diffInMonths(Long startTime, Long endTime) {
+  public static int diffInMonths(long startTime, long endTime) {
     if (startTime > endTime)
       throw new IllegalArgumentException("startTime cannot be larger than endTime");
     Calendar startCal = Calendar.getInstance();
diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SizeAndTimeBasedArchiveRemover.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SizeAndTimeBasedArchiveRemover.java
index 1bb90d4..858ca47 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SizeAndTimeBasedArchiveRemover.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/SizeAndTimeBasedArchiveRemover.java
@@ -19,12 +19,12 @@ import java.util.Date;
 public class SizeAndTimeBasedArchiveRemover extends DefaultArchiveRemover {
 
   public SizeAndTimeBasedArchiveRemover(FileNamePattern fileNamePattern,
-      RollingCalendar rc, long currentTime) {
-    super(fileNamePattern, rc, currentTime);
+      RollingCalendar rc) {
+    super(fileNamePattern, rc);
   }
 
-  public void clean(Date now) {
-    Date dateOfPeriodToClean = rc.getRelativeDate(now, periodOffsetForDeletionTarget);
+  public void cleanByPeriodOffset(Date now, int periodOffset) {
+    Date dateOfPeriodToClean = rc.getRelativeDate(now, periodOffset);
 
     String regex = fileNamePattern.toRegex(dateOfPeriodToClean);
     String stemRegex = FileFilterUtil.afterLastSlash(regex);
@@ -47,4 +47,5 @@ public class SizeAndTimeBasedArchiveRemover extends DefaultArchiveRemover {
     }
   }
 
+
 }
diff --git a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java
index 01ff76e..a79f481 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/TimeBasedArchiveRemover.java
@@ -25,21 +25,7 @@ public class TimeBasedArchiveRemover extends DefaultArchiveRemover {
 
   public TimeBasedArchiveRemover(FileNamePattern fileNamePattern,
                                  RollingCalendar rc, long currentTime) {
-    super(fileNamePattern, rc, currentTime);
-  }
-
-
-  public void clean(Date now) {
-    long nowInMillis = now.getTime();
-    int periodsElapsed = rc.periodsElapsed(lastHeartBeat, nowInMillis);
-    if(periodsElapsed < 1) {
-      addWarn("Unexpected periodsElapsed value "+periodsElapsed);
-      periodsElapsed = 1;
-    }
-    lastHeartBeat = nowInMillis;
-    for(int i=0; i < periodsElapsed; i++) {
-      cleanByPeriodOffset(now, periodOffsetForDeletionTarget-i);
-    }
+    super(fileNamePattern, rc);
   }
 
   protected void cleanByPeriodOffset(Date now, int periodOffset) {
diff --git a/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemovalTest.java b/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemovalTest.java
deleted file mode 100644
index 7f75422..0000000
--- a/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemovalTest.java
+++ /dev/null
@@ -1,391 +0,0 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- *   or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.core.rolling;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.io.File;
-import java.io.FileFilter;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import static ch.qos.logback.core.CoreConstants.DAILY_DATE_PATTERN;
-import ch.qos.logback.core.util.StatusPrinter;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-
-import ch.qos.logback.core.Context;
-import ch.qos.logback.core.ContextBase;
-import ch.qos.logback.core.encoder.EchoEncoder;
-import ch.qos.logback.core.testUtil.RandomUtil;
-import ch.qos.logback.core.util.CoreTestConstants;
-
-public class TimeBasedRollingWithArchiveRemovalTest {
-
-  Context context = new ContextBase();
-  EchoEncoder<Object> encoder = new EchoEncoder<Object>();
-
-  static final String MONTHLY_DATE_PATTERN = "yyyy-MM";
-  static final String MONTHLY_CROLOLOG_DATE_PATTERN = "yyyy/MM";
-
-  static final String DAILY_CROLOLOG_DATE_PATTERN = "yyyy/MM/dd";
-
-  static final long MILLIS_IN_MINUTE = 60 * 1000;
-  static final long MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
-  static final long MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
-  static final long MILLIS_IN_MONTH = (long) ((365.0/12) * MILLIS_IN_DAY);
-
-  int diff = RandomUtil.getPositiveInt();
-  protected String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff
-      + "/";
-  int slashCount;
-
-  // by default tbfnatp is an instance of
-  // DefaultTimeBasedFileNamingAndTriggeringPolicy
-  TimeBasedFileNamingAndTriggeringPolicy<Object> tbfnatp = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
-
-  @Before
-  public void setUp()  {
-    context.setName("test");
-  }
-
-  @After
-  public void tearDown() throws Exception {
-  }
-
-  int computeSlashCount(String datePattern) {
-    int fromIndex = 0;
-    int count = 0;
-    while (true) {
-      int i = datePattern.indexOf('/', fromIndex);
-      if (i == -1) {
-        break;
-      } else {
-        count++;
-        fromIndex = i + 1;
-        if (fromIndex >= datePattern.length()) {
-          break;
-        }
-      }
-    }
-    return count;
-  }
-
-  @Test
-  public void montlyRollover() throws Exception {
-    slashCount = computeSlashCount(MONTHLY_DATE_PATTERN);
-    // large maxPeriod, a 3 times as many number of periods to simulate
-    doRollover(randomOutputDir + "clean-%d{" + MONTHLY_DATE_PATTERN + "}.txt",
-        MILLIS_IN_MONTH, 20, 20 * 3);
-    check(expectedCountWithoutFolders(20));
-  }
-
-  @Test
-  public void montlyRolloverOverManyPeriods() throws Exception {
-    System.out.println("randomOutputDir=" + randomOutputDir);
-    // small maxHistory, many periods
-    slashCount = computeSlashCount(MONTHLY_CROLOLOG_DATE_PATTERN);
-
-    int numPeriods = 40;
-    int maxHistory = 2;
-
-    doRollover(randomOutputDir + "/%d{" + MONTHLY_CROLOLOG_DATE_PATTERN
-        + "}/clean.txt.zip", MILLIS_IN_MONTH, maxHistory, numPeriods);
-    int beginPeriod = Calendar.getInstance().get(Calendar.MONTH);
-    boolean extraFolder = extraFolder(numPeriods, 12, beginPeriod, maxHistory);
-    System.out.println("xxxx slashCount="+slashCount+", extraFolder="+extraFolder);
-    StatusPrinter.print(context);
-    check(expectedCountWithFolders(2, extraFolder));
-  }
-
-  @Test
-  public void dailyRollover() throws Exception {
-    slashCount = computeSlashCount(DAILY_DATE_PATTERN);
-    doRollover(
-        randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt.zip",
-        MILLIS_IN_DAY, 5, 5 * 3);
-    check(expectedCountWithoutFolders(5));
-  }
-
-  @Test
-  public void dailyCronologRollover() throws Exception {
-    slashCount = computeSlashCount(DAILY_CROLOLOG_DATE_PATTERN);
-    doRollover(randomOutputDir + "/%d{" + DAILY_CROLOLOG_DATE_PATTERN
-        + "}/clean.txt.zip", MILLIS_IN_DAY, 8, 8 * 3);
-    int expectedDirMin = 9 + slashCount;
-    int expectDirMax = expectedDirMin + 1 + 1; // plus 1 of stepping into a
-    // new month, and another 1 into
-    // a new year
-    expectedFileAndDirCount(9, expectedDirMin, expectDirMax);
-  }
-
-  @Test
-  public void dailySizeBasedRollover() throws Exception {
-    SizeAndTimeBasedFNATP<Object> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<Object>();
-    sizeAndTimeBasedFNATP.setMaxFileSize("10000");
-    tbfnatp = sizeAndTimeBasedFNATP;
-
-    slashCount = computeSlashCount(DAILY_DATE_PATTERN);
-    doRollover(
-        randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}-clean.%i.zip",
-        MILLIS_IN_DAY, 5, 5 * 4);
-
-    // make .zip optional so that if for one reason or another, no size-based
-    // rollover occurs on the last period, that the last period is still
-    // accounted
-    // for
-    checkPatternCompliance(5 + 1 + slashCount,
-        "\\d{4}-\\d{2}-\\d{2}-clean(\\.\\d)(.zip)?");
-  }
-
-  @Test
-  public void dailyChronologSizeBasedRollover() throws Exception {
-    SizeAndTimeBasedFNATP<Object> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<Object>();
-    sizeAndTimeBasedFNATP.setMaxFileSize("10000");
-    tbfnatp = sizeAndTimeBasedFNATP;
-
-    slashCount = 1;
-    doRollover(
-        randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}/clean.%i.zip",
-        MILLIS_IN_DAY, 5, 5 * 4);
-    checkDirPatternCompliance(6);
-  }
-
-
-  @Ignore
-  @Test
-  public void dailyChronologSizeBasedRolloverWhenLogFilenameDoesNotContainDirectory() throws Exception {
-    SizeAndTimeBasedFNATP<Object> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<Object>();
-    sizeAndTimeBasedFNATP.setMaxFileSize("10000");
-    tbfnatp = sizeAndTimeBasedFNATP;
-
-    slashCount = 1;
-    doRollover(
-        "clean.%d{" + DAILY_DATE_PATTERN + "}.%i.zip",
-        MILLIS_IN_DAY, 5, 5 * 4);
-    checkDirPatternCompliance(6);
-  }
-
-  void doRollover(String fileNamePattern, long periodDurationInMillis,
-      int maxHistory, int simulatedNumberOfPeriods) throws Exception {
-    long currentTime = System.currentTimeMillis();
-
-    RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
-    rfa.setContext(context);
-    rfa.setEncoder(encoder);
-    // rfa.setFile(Constants.OUTPUT_DIR_PREFIX + "clean.txt");
-    TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
-    tbrp.setContext(context);
-    tbrp.setFileNamePattern(fileNamePattern);
-
-    tbrp.setMaxHistory(maxHistory);
-    tbrp.setParent(rfa);
-    tbrp.timeBasedFileNamingAndTriggeringPolicy = tbfnatp;
-    tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
-    tbrp.start();
-    rfa.setRollingPolicy(tbrp);
-    rfa.start();
-
-    // lots of ticks per period
-    int ticksPerPeriod = 512;
-    long runLength = simulatedNumberOfPeriods * ticksPerPeriod;
-
-    for (long i = 0; i < runLength; i++) {
-      rfa
-          .doAppend("Hello ----------------------------------------------------------"
-              + i);
-      tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(addTime(tbrp.timeBasedFileNamingAndTriggeringPolicy
-          .getCurrentTime(), periodDurationInMillis / ticksPerPeriod));
-
-      // wait every now and then for the compression job
-      // otherwise, we might rollover a file for a given period before a previous
-      // period's compressor had a chance to run
-      if(i % (ticksPerPeriod/2) == 0) {
-        waitForCompression(tbrp);
-      }
-    }
-    waitForCompression(tbrp);
-    rfa.stop();
-  }
-
-  void waitForCompression(TimeBasedRollingPolicy<Object> tbrp)
-      throws InterruptedException, ExecutionException, TimeoutException {
-    if (tbrp.future != null && !tbrp.future.isDone()) {
-      tbrp.future.get(800, TimeUnit.MILLISECONDS);
-    }
-  }
-
-  void findAllFoldersRecursively(File dir, List<File> fileList) {
-    if (dir.isDirectory()) {
-      File[] match = dir.listFiles(new FileFilter() {
-        public boolean accept(File f) {
-          return (f.isDirectory());
-        }
-      });
-      for (File f : match) {
-        fileList.add(f);
-        findAllFoldersRecursively(f, fileList);
-      }
-    }
-  }
-
-  void findAllInFolderRecursivelyByStringContains(File dir,
-      List<File> fileList, final String pattern) {
-    if (dir.isDirectory()) {
-      File[] match = dir.listFiles(new FileFilter() {
-        public boolean accept(File f) {
-          return (f.isDirectory() || f.getName().contains(pattern));
-        }
-      });
-      for (File f : match) {
-        fileList.add(f);
-        if (f.isDirectory()) {
-          findAllInFolderRecursivelyByStringContains(f, fileList, pattern);
-        }
-      }
-    }
-  }
-
-  void findFilesInFolderRecursivelyByPatterMatch(File dir, List<File> fileList,
-      final String pattern) {
-    if (dir.isDirectory()) {
-      File[] match = dir.listFiles(new FileFilter() {
-        public boolean accept(File f) {
-          return (f.isDirectory() || f.getName().matches(pattern));
-        }
-      });
-      for (File f : match) {
-        if (f.isDirectory()) {
-          findFilesInFolderRecursivelyByPatterMatch(f, fileList, pattern);
-        } else {
-          fileList.add(f);
-        }
-      }
-    }
-  }
-
-  void findFoldersInFolderRecursively(File dir, List<File> fileList) {
-    if (dir.isDirectory()) {
-      File[] match = dir.listFiles(new FileFilter() {
-        public boolean accept(File f) {
-          return f.isDirectory();
-        }
-      });
-      for (File f : match) {
-        fileList.add(f);
-        findFoldersInFolderRecursively(f, fileList);
-      }
-    }
-  }
-
-  int expectedCountWithoutFolders(int maxHistory) {
-    // maxHistory plus the currently active file
-    return maxHistory + 1;
-  }
-
-  // sometimes, after a number of periods, there is an extra folder
-  // from the previous "era" because the latest period - maxHistory, enters the
-  // bound of the previous era. For example, with a maxHistory of 2, on 2009-09,
-  // after 40 periods, the current period is 2013-01. Going back two months, the
-  // year is 2012, and not 2013 (the current year).
-  boolean extraFolder(int numPeriods, int periodsPerEra, int beginPeriod,
-      int maxHistory) {
-    // beginPeriod is 0 for JAN, 1 for FEB etc
-    int valueOfLastMonth = ((beginPeriod) + numPeriods) % periodsPerEra;
-    return (valueOfLastMonth < maxHistory);
-  }
-
-  int expectedCountWithFolders(int maxHistory, boolean extraFolder) {
-    // each slash adds a new directory
-    // + one file and one directory per archived log file
-    int result = (maxHistory + 1) * 2 + slashCount;
-    if (extraFolder)
-      result++;
-    return result;
-  }
-
-  void check(int expectedCount) {
-    File dir = new File(randomOutputDir);
-    List<File> fileList = new ArrayList<File>();
-    findAllInFolderRecursivelyByStringContains(dir, fileList, "clean");
-    assertEquals(expectedCount, fileList.size());
-  }
-
-  void expectedFileAndDirCount(int expectedFileAndDirCount,
-      int expectedDirCountMin, int expectedDirCountMax) {
-    File dir = new File(randomOutputDir);
-    List<File> fileList = new ArrayList<File>();
-    findFilesInFolderRecursivelyByPatterMatch(dir, fileList, "clean");
-
-    List<File> dirList = new ArrayList<File>();
-    findAllFoldersRecursively(dir, dirList);
-    assertTrue("expectedDirCountMin=" + expectedDirCountMin
-        + ", expectedDirCountMax=" + expectedDirCountMax + " actual value="
-        + dirList.size(), expectedDirCountMin <= dirList.size()
-        && dirList.size() <= expectedDirCountMax);
-  }
-
-  void checkPatternCompliance(int expectedClassCount, String regex) {
-    File dir = new File(randomOutputDir);
-    List<File> fileList = new ArrayList<File>();
-    findFilesInFolderRecursivelyByPatterMatch(dir, fileList, regex);
-    Set<String> set = groupByClass(fileList, regex);
-    assertEquals(expectedClassCount, set.size());
-  }
-
-  void checkDirPatternCompliance(int expectedClassCount) {
-    File dir = new File(randomOutputDir);
-    List<File> fileList = new ArrayList<File>();
-    findFoldersInFolderRecursively(dir, fileList);
-    for (File f : fileList) {
-      assertTrue(f.list().length >= 1);
-    }
-    assertEquals(expectedClassCount, fileList.size());
-  }
-
-  // reduce file names differing by index number into the same group
-  // for example, 2009-11-01-clean.0.zip, 2009-11-01-clean.1.zip and
-  // 2009-11-01-clean-2 are reduced into the same string (group)
-  // 2009-11-01-clean
-  Set<String> groupByClass(List<File> fileList, String regex) {
-    Pattern p = Pattern.compile(regex);
-    Set<String> set = new HashSet<String>();
-    for (File f : fileList) {
-      String n = f.getName();
-      Matcher m = p.matcher(n);
-      m.matches();
-      int begin = m.start(1);
-      String reduced = n.substring(0, begin);
-      set.add(reduced);
-    }
-    System.out.println(set);
-    return set;
-  }
-
-  static long addTime(long currentTime, long timeToWait) {
-    return currentTime + timeToWait;
-  }
-
-}
diff --git a/logback-core/src/test/scala/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemoval_STest.scala b/logback-core/src/test/scala/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemoval_STest.scala
index a1e99a6..a428f45 100644
--- a/logback-core/src/test/scala/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemoval_STest.scala
+++ b/logback-core/src/test/scala/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemoval_STest.scala
@@ -28,23 +28,29 @@ class TimeBasedRollingWithArchiveRemoval_STest {
   var encoder: EchoEncoder[AnyRef] = new EchoEncoder[AnyRef]
 
   val MONTHLY_DATE_PATTERN: String = "yyyy-MM"
-  val MONTHLY_CROLOLOG_DATE_PATTERN: String = "yyyy/MM"
-  final val DAILY_CROLOLOG_DATE_PATTERN: String = "yyyy/MM/dd"
+
+  val MONTHLY_CRONOLOG_DATE_PATTERN: String = "yyyy/MM"
+  final val DAILY_CRONOLOG_DATE_PATTERN: String = "yyyy/MM/dd"
 
   val MILLIS_IN_MINUTE: Long = 60 * 1000
   val MILLIS_IN_HOUR: Long = 60 * MILLIS_IN_MINUTE
   val MILLIS_IN_DAY: Long = 24 * MILLIS_IN_HOUR
   val MILLIS_IN_MONTH: Long = ((365.0 / 12) * MILLIS_IN_DAY).asInstanceOf[Long]
 
-  var diff: Int = RandomUtil.getPositiveInt
-  var randomOutputDir: String = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/"
+  var diff: Int = _
+  var randomOutputDir: String = _
   var slashCount: Int = 0
 
   // by default tbfnatp is an instance of DefaultTimeBasedFileNamingAndTriggeringPolicy
   var tbfnatp: TimeBasedFileNamingAndTriggeringPolicy[AnyRef] = new DefaultTimeBasedFileNamingAndTriggeringPolicy[AnyRef]
 
+  val now = System.currentTimeMillis
+
+
   @Before def setUp {
     context.setName("test")
+    diff = RandomUtil.getPositiveInt
+    randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/"
   }
 
   def computeSlashCount(datePattern: String): Int = {
@@ -52,20 +58,29 @@ class TimeBasedRollingWithArchiveRemoval_STest {
     else datePattern.foldLeft(0)((count, c) => if (c == '/') count + 1 else count)
   }
 
+  def genMontlyRollover(maxHistory: Int, simulatedNumberOfPeriods: Int, startInactivity: Int, numInactivityPeriods: Int) {
+    slashCount = computeSlashCount(MONTHLY_DATE_PATTERN)
+    doRollover(now, randomOutputDir + "clean-%d{" + MONTHLY_DATE_PATTERN + "}.txt", MILLIS_IN_MONTH, maxHistory, simulatedNumberOfPeriods, startInactivity, numInactivityPeriods)
+    check(expectedCountWithoutFolders(maxHistory))
+  }
+
   @Test
   def montlyRollover {
-    slashCount = computeSlashCount(MONTHLY_DATE_PATTERN)
-    doRollover(randomOutputDir + "clean-%d{" + MONTHLY_DATE_PATTERN + "}.txt", MILLIS_IN_MONTH, 20, 20 * 3)
-    check(expectedCountWithoutFolders(20))
+    genMontlyRollover(maxHistory = 20, simulatedNumberOfPeriods = 20 * 3, startInactivity = 0, numInactivityPeriods = 0)
+    setUp
+    genMontlyRollover(maxHistory = 6, simulatedNumberOfPeriods = 70, startInactivity = 30, numInactivityPeriods = 1)
+    setUp
+    genMontlyRollover(maxHistory = 6, simulatedNumberOfPeriods = 10, startInactivity = 3, numInactivityPeriods = 4)
+
   }
 
   @Test def monthlyRolloverOverManyPeriods {
     System.out.println("randomOutputDir=" + randomOutputDir)
-    slashCount = computeSlashCount(MONTHLY_CROLOLOG_DATE_PATTERN)
+    slashCount = computeSlashCount(MONTHLY_CRONOLOG_DATE_PATTERN)
     var numPeriods: Int = 40
     var maxHistory: Int = 2
 
-    val (startTime, endTime) = doRollover(randomOutputDir + "/%d{" + MONTHLY_CROLOLOG_DATE_PATTERN + "}/clean.txt.zip", MILLIS_IN_MONTH, maxHistory, numPeriods)
+    val (startTime, endTime) = doRollover(now, randomOutputDir + "/%d{" + MONTHLY_CRONOLOG_DATE_PATTERN + "}/clean.txt.zip", MILLIS_IN_MONTH, maxHistory, numPeriods)
     val differenceInMonths = RollingCalendar.diffInMonths(startTime, endTime)
     var indexOfStartPeriod: Int = Calendar.getInstance.get(Calendar.MONTH)
     val withExtraFolder = extraFolder(differenceInMonths, 12, indexOfStartPeriod, maxHistory)
@@ -75,16 +90,28 @@ class TimeBasedRollingWithArchiveRemoval_STest {
 
   @Test def dailyRollover {
     slashCount = computeSlashCount(DAILY_DATE_PATTERN)
-    doRollover(randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt.zip", MILLIS_IN_DAY, 5, 5 * 3)
+    doRollover(now, randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt.zip", MILLIS_IN_DAY, 5, 5 * 3, startInactivity = 6, numInactivityPeriods = 3)
+    StatusPrinter.print(context)
     check(expectedCountWithoutFolders(5))
   }
 
+  @Test def dailyRolloverWithSecondPhase {
+    slashCount = computeSlashCount(DAILY_DATE_PATTERN)
+    val maxHistory = 5
+    val simulatedNumberOfPeriods = maxHistory * 2
+    val (startTime, endTime) = doRollover(now, randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt", MILLIS_IN_DAY, maxHistory, maxHistory * 2)
+    doRollover(endTime + MILLIS_IN_DAY * 10, randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt", MILLIS_IN_DAY, maxHistory, maxHistory)
+    check(expectedCountWithoutFolders(maxHistory))
+  }
+
+
   @Test def dailyCronologRollover {
-    slashCount = computeSlashCount(DAILY_CROLOLOG_DATE_PATTERN)
-    doRollover(randomOutputDir + "/%d{" + DAILY_CROLOLOG_DATE_PATTERN + "}/clean.txt.zip", MILLIS_IN_DAY, 8, 8 * 3)
+    slashCount = computeSlashCount(DAILY_CRONOLOG_DATE_PATTERN)
+    doRollover(now, randomOutputDir + "/%d{" + DAILY_CRONOLOG_DATE_PATTERN + "}/clean.txt.zip", MILLIS_IN_DAY, 8, 8 * 3)
     var expectedDirMin: Int = 9 + slashCount
     var expectDirMax: Int = expectedDirMin + 1 + 1
     expectedFileAndDirCount(9, expectedDirMin, expectDirMax)
+
   }
 
   @Test def dailySizeBasedRollover {
@@ -92,7 +119,7 @@ class TimeBasedRollingWithArchiveRemoval_STest {
     sizeAndTimeBasedFNATP.setMaxFileSize("10000")
     tbfnatp = sizeAndTimeBasedFNATP
     slashCount = computeSlashCount(DAILY_DATE_PATTERN)
-    doRollover(randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}-clean.%i.zip", MILLIS_IN_DAY, 5, 5 * 4)
+    doRollover(now, randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}-clean.%i.zip", MILLIS_IN_DAY, 5, 5 * 4)
     checkPatternCompliance(5 + 1 + slashCount, "\\d{4}-\\d{2}-\\d{2}-clean(\\.\\d)(.zip)?")
   }
 
@@ -101,10 +128,22 @@ class TimeBasedRollingWithArchiveRemoval_STest {
     sizeAndTimeBasedFNATP.setMaxFileSize("10000")
     tbfnatp = sizeAndTimeBasedFNATP
     slashCount = 1
-    doRollover(randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}/clean.%i.zip", MILLIS_IN_DAY, 5, 5 * 4)
+    doRollover(now, randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}/clean.%i.zip", MILLIS_IN_DAY, 5, 5 * 4)
     checkDirPatternCompliance(6)
   }
 
+  @Test def dailyChronologSizeBasedRolloverWithSecondPhase {
+    var sizeAndTimeBasedFNATP: SizeAndTimeBasedFNATP[AnyRef] = new SizeAndTimeBasedFNATP[AnyRef]
+    sizeAndTimeBasedFNATP.setMaxFileSize("10000")
+    tbfnatp = sizeAndTimeBasedFNATP
+    slashCount = 1
+    val maxHistory = 5
+    val simulatedNumberOfPeriods = maxHistory * 4
+    val (startTime, endTime) = doRollover(now, randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}/clean.%i", MILLIS_IN_DAY, maxHistory, 3)
+    doRollover(endTime+MILLIS_IN_DAY*7, randomOutputDir + "/%d{" + DAILY_DATE_PATTERN + "}/clean.%i", MILLIS_IN_DAY, maxHistory, simulatedNumberOfPeriods)
+    checkDirPatternCompliance(maxHistory+1)
+  }
+
 
   // this test requires changing the current working directory which is impossible in Java
   @Ignore
@@ -113,7 +152,7 @@ class TimeBasedRollingWithArchiveRemoval_STest {
     sizeAndTimeBasedFNATP.setMaxFileSize("10000")
     tbfnatp = sizeAndTimeBasedFNATP
     slashCount = 1
-    doRollover("clean.%d{" + DAILY_DATE_PATTERN + "}.%i.zip", MILLIS_IN_DAY, 5, 5 * 4)
+    doRollover(now, "clean.%d{" + DAILY_DATE_PATTERN + "}.%i.zip", MILLIS_IN_DAY, 5, 5 * 4)
     checkDirPatternCompliance(6)
   }
 
@@ -124,7 +163,7 @@ class TimeBasedRollingWithArchiveRemoval_STest {
 
   def expectedCountWithFolders(maxHistory: Int, extraFolder: Boolean): Int = {
     val numLogFiles = (maxHistory + 1)
-    val numLogFilesAndFolders = numLogFiles*2
+    val numLogFilesAndFolders = numLogFiles * 2
     var result: Int = numLogFilesAndFolders + slashCount
     if (extraFolder) result += 1
     result
@@ -140,8 +179,7 @@ class TimeBasedRollingWithArchiveRemoval_STest {
     }
   }
 
-  def doRollover(fileNamePattern: String, periodDurationInMillis: Long, maxHistory: Int, simulatedNumberOfPeriods: Int): (Long, Long) = {
-    var currentTime: Long = System.currentTimeMillis
+  def doRollover(currentTime: Long, fileNamePattern: String, periodDurationInMillis: Long, maxHistory: Int, simulatedNumberOfPeriods: Int, startInactivity: Int = 0, numInactivityPeriods: Int = 0): (Long, Long) = {
     val startTime = currentTime
     var rfa: RollingFileAppender[AnyRef] = new RollingFileAppender[AnyRef]
     rfa.setContext(context)
@@ -158,15 +196,20 @@ class TimeBasedRollingWithArchiveRemoval_STest {
     rfa.start
     var ticksPerPeriod: Int = 512
     var runLength = simulatedNumberOfPeriods * ticksPerPeriod
+    val startInactivityIndex: Int = 1 + startInactivity * ticksPerPeriod
+    val endInactivityIndex = startInactivity + numInactivityPeriods * ticksPerPeriod
+
 
     for (i <- 0 to runLength) {
-      rfa.doAppend("Hello ----------------------------------------------------------" + i)
+      if (i < startInactivityIndex || i > endInactivityIndex) {
+        rfa.doAppend("Hello ----------------------------------------------------------" + i)
+      }
       tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(addTime(tbrp.timeBasedFileNamingAndTriggeringPolicy.getCurrentTime, periodDurationInMillis / ticksPerPeriod))
       if (i % (ticksPerPeriod / 2) == 0) {
         waitForCompression(tbrp)
       }
-
     }
+
     waitForCompression(tbrp)
     rfa.stop
     (startTime, tbrp.timeBasedFileNamingAndTriggeringPolicy.getCurrentTime)

-----------------------------------------------------------------------

Summary of changes:
 .../classic/spi/PackagingDataCalculator.java       |    2 +-
 .../core/rolling/SizeAndTimeBasedFNATP.java        |    2 +-
 .../core/rolling/helper/DefaultArchiveRemover.java |   47 ++-
 .../core/rolling/helper/FileFilterUtil.java        |    2 +-
 .../core/rolling/helper/RollingCalendar.java       |   14 +-
 .../helper/SizeAndTimeBasedArchiveRemover.java     |    9 +-
 .../rolling/helper/TimeBasedArchiveRemover.java    |   16 +-
 .../TimeBasedRollingWithArchiveRemovalTest.java    |  391 --------------------
 .../TimeBasedRollingWithArchiveRemoval_STest.scala |   83 ++++-
 9 files changed, 120 insertions(+), 446 deletions(-)
 delete mode 100644 logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithArchiveRemovalTest.java


hooks/post-receive
-- 
Logback: the generic, reliable, fast and flexible logging framework.


More information about the logback-dev mailing list