[logback-dev] svn commit: r1677 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/rolling main/java/ch/qos/logback/core/rolling/helper test/java/ch/qos/logback/core/rolling test/java/ch/qos/logback/core/rolling/helper

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sat Apr 26 01:05:57 CEST 2008


Author: ceki
Date: Sat Apr 26 01:05:56 2008
New Revision: 1677

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionMode.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java
      - copied, changed from r1666, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compress.java
Removed:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compress.java
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/RollingPolicyBase.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/rolling/helper/CompressTest.java

Log:
- added support for asynchronous compression 

 see also 

  http://bugzilla.qos.ch/show_bug.cgi?id=55
  http://bugzilla.qos.ch/show_bug.cgi?id=149
  

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	Sat Apr 26 01:05:56 2008
@@ -11,7 +11,8 @@
 
 import java.io.File;
 
-import ch.qos.logback.core.rolling.helper.Compress;
+import ch.qos.logback.core.rolling.helper.CompressionMode;
+import ch.qos.logback.core.rolling.helper.Compressor;
 import ch.qos.logback.core.rolling.helper.FileNamePattern;
 import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
 import ch.qos.logback.core.rolling.helper.RenameUtil;
@@ -32,7 +33,6 @@
   int maxIndex;
   int minIndex;
   RenameUtil util = new RenameUtil();
-  Compress compress = new Compress();
 
   /**
    * It's almost always a bad idea to have a large window size, say over 12.
@@ -47,8 +47,7 @@
   public void start() {
     // set the LR for our utility object
     util.setContext(this.context);
-    compress.setContext(this.context);
-
+   
     if (fileNamePatternStr != null) {
       fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);
       determineCompressionMode();
@@ -111,15 +110,23 @@
       }
 
       // move active file name to min
+      Compressor compressor;
       switch (compressionMode) {
-      case Compress.NONE:
-        util.rename(getNewActiveFileName(), fileNamePattern.convertInt(minIndex));
+      case NONE:
+        util.rename(getNewActiveFileName(), fileNamePattern
+            .convertInt(minIndex));
         break;
-      case Compress.GZ:
-        compress.GZCompress(getNewActiveFileName(), fileNamePattern.convertInt(minIndex));
+      case GZ:
+        compressor = new Compressor(CompressionMode.GZ,
+            getNewActiveFileName(), fileNamePattern.convertInt(minIndex));
+        compressor.setContext(this.context);
+        compressor.compress();
         break;
-      case Compress.ZIP:
-        compress.ZIPCompress(getNewActiveFileName(), fileNamePattern.convertInt(minIndex));
+      case ZIP:
+        compressor = new Compressor(CompressionMode.ZIP,
+            getNewActiveFileName(), fileNamePattern.convertInt(minIndex));
+        compressor.setContext(this.context);
+        compressor.compress();
         break;
       }
     }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingPolicyBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingPolicyBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/RollingPolicyBase.java	Sat Apr 26 01:05:56 2008
@@ -10,7 +10,7 @@
 package ch.qos.logback.core.rolling;
 
 import ch.qos.logback.core.FileAppender;
-import ch.qos.logback.core.rolling.helper.Compress;
+import ch.qos.logback.core.rolling.helper.CompressionMode;
 import ch.qos.logback.core.rolling.helper.FileNamePattern;
 import ch.qos.logback.core.spi.ContextAwareBase;
 
@@ -24,7 +24,7 @@
  * @author Ceki Gülcü
  */
 public abstract class RollingPolicyBase extends ContextAwareBase implements RollingPolicy {
-  protected int compressionMode = Compress.NONE;
+  protected CompressionMode compressionMode = CompressionMode.NONE;
   protected FileNamePattern fileNamePattern;
   protected String fileNamePatternStr;
   
@@ -42,13 +42,13 @@
   protected void determineCompressionMode() {
      if (fileNamePatternStr.endsWith(".gz")) {
       addInfo("Will use gz compression");
-      compressionMode = Compress.GZ;
+      compressionMode = CompressionMode.GZ;
     } else if (fileNamePatternStr.endsWith(".zip")) {
       addInfo("Will use zip compression");
-      compressionMode = Compress.ZIP;
+      compressionMode = CompressionMode.ZIP;
     } else {
       addInfo("No compression will be used");
-      compressionMode = Compress.NONE;
+      compressionMode = CompressionMode.NONE;
     }
   }
 

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	Sat Apr 26 01:05:56 2008
@@ -9,63 +9,63 @@
  */
 package ch.qos.logback.core.rolling;
 
-
 import java.io.File;
 import java.util.Date;
+import java.util.concurrent.Future;
 
-import ch.qos.logback.core.rolling.helper.Compress;
+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.DateTokenConverter;
 import ch.qos.logback.core.rolling.helper.FileNamePattern;
 import ch.qos.logback.core.rolling.helper.RenameUtil;
 import ch.qos.logback.core.rolling.helper.RollingCalendar;
 
 /**
- * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite 
- * powerful. It allows the rollover to be made based on time conditions. 
- * It is possible to specify that the rollover must occur each day, or month, for example.
+ * <code>TimeBasedRollingPolicy</code> is both easy to configure and quite
+ * powerful. It allows the rollover to be made based on time conditions. It is
+ * possible to specify that the rollover must occur each day, or month, for
+ * example.
  * 
- *  For more information about this policy, please refer to the online manual at
- *  http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
+ * For more information about this policy, please refer to the online manual at
+ * http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
  * 
  * @author Ceki G&uuml;lc&uuml;
  */
-public class TimeBasedRollingPolicy<E> extends RollingPolicyBase implements 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";
+public class TimeBasedRollingPolicy<E> extends RollingPolicyBase implements
+    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";
   RollingCalendar rc;
   long currentTime;
   long nextCheck;
-  //indicate whether the time has been forced or not
-  boolean isTimeForced=false;
+  // indicate whether the time has been forced or not
+  boolean isTimeForced = false;
   Date lastCheck = new Date();
   String elapsedPeriodsFileName;
   FileNamePattern activeFileNamePattern;
   RenameUtil util = new RenameUtil();
-  Compress compress = new Compress();
   String lastGeneratedFileName;
-  
+  Future<?> future;
+
   public void setCurrentTime(long timeInMillis) {
-	currentTime = timeInMillis;
-	isTimeForced = true;
+    currentTime = timeInMillis;
+    isTimeForced = true;
   }
-  
-  public long getCurrentTime(){
-	//if time is forced return the time set by user
-	if(isTimeForced){
-	  return currentTime;
-	}
-	else{
-	  return System.currentTimeMillis();
-	}
+
+  public long getCurrentTime() {
+    // if time is forced return the time set by user
+    if (isTimeForced) {
+      return currentTime;
+    } else {
+      return System.currentTimeMillis();
+    }
   }
-  
+
   public void start() {
     // set the LR for our utility object
     util.setContext(this.context);
-    compress.setContext(this.context);
-    
+
     // find out period from the filename pattern
     if (fileNamePatternStr != null) {
       fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);
@@ -79,103 +79,115 @@
     DateTokenConverter dtc = fileNamePattern.getDateTokenConverter();
 
     if (dtc == null) {
-      throw new IllegalStateException(
-        "FileNamePattern [" + fileNamePattern.getPattern()
-        + "] does not contain a valid DateToken");
+      throw new IllegalStateException("FileNamePattern ["
+          + fileNamePattern.getPattern()
+          + "] does not contain a valid DateToken");
     }
 
     int len = fileNamePatternStr.length();
-    switch(compressionMode) {
-    case Compress.GZ:
-      activeFileNamePattern =
-        new FileNamePattern(fileNamePatternStr.substring(0, len - 3), this.context);
-        
+    switch (compressionMode) {
+    case GZ:
+      activeFileNamePattern = new FileNamePattern(fileNamePatternStr.substring(
+          0, len - 3), this.context);
+
+      break;
+    case ZIP:
+      activeFileNamePattern = new FileNamePattern(fileNamePatternStr.substring(
+          0, len - 4), this.context);
       break;
-      case Compress.ZIP:
-        activeFileNamePattern =
-          new FileNamePattern(fileNamePatternStr.substring(0, len - 4), this.context);
-        break;
-       case Compress.NONE:
-        activeFileNamePattern = fileNamePattern;
-     }
-     addInfo("Will use the pattern "+activeFileNamePattern+" for the active file");
-   
+    case NONE:
+      activeFileNamePattern = fileNamePattern;
+    }
+    addInfo("Will use the pattern " + activeFileNamePattern
+        + " for the active file");
+
     rc = new RollingCalendar();
     rc.init(dtc.getDatePattern());
-    addInfo("The date pattern is '"+dtc.getDatePattern()+"' from file name pattern '"+
-        fileNamePattern.getPattern()+"'.");
+    addInfo("The date pattern is '" + dtc.getDatePattern()
+        + "' from file name pattern '" + fileNamePattern.getPattern() + "'.");
     rc.printPeriodicity(this);
 
-    //currentTime = System.currentTimeMillis();
+    // currentTime = System.currentTimeMillis();
     lastCheck.setTime(getCurrentTime());
     nextCheck = rc.getNextCheckMillis(lastCheck);
 
-    //Date nc = new Date();
-    //nc.setTime(nextCheck);
+    // Date nc = new Date();
+    // nc.setTime(nextCheck);
   }
 
   public void rollover() throws RolloverFailure {
-    //addInfo("roll-over called");
-    //addInfo("compressionMode: " + compressionMode);
 
-    if (getParentFileName() == null) {
-      switch (compressionMode) {
-      case Compress.NONE:
-        // nothing to do;
-        break;
-      case Compress.GZ:
-        addInfo("GZIP compressing ["+elapsedPeriodsFileName+"].");
-        compress.GZCompress(elapsedPeriodsFileName);
-        break;
-      case Compress.ZIP:
-        addInfo("ZIP compressing ["+elapsedPeriodsFileName+"]");
-        compress.ZIPCompress(elapsedPeriodsFileName);
-        break;
-      }
+    // when rollover is called the elapsed period's file has
+    // been already closed. This is a working assumption of this method.
+
+    if (getParentFileName() == null && compressionMode != CompressionMode.NONE) {
+      doCompression(false, elapsedPeriodsFileName, elapsedPeriodsFileName);
     } else {
-      switch (compressionMode) {
-      case Compress.NONE:
+      if (compressionMode == CompressionMode.NONE) {
         util.rename(getParentFileName(), elapsedPeriodsFileName);
-        break;
-      case Compress.GZ:
-        addInfo("GZIP compressing ["+elapsedPeriodsFileName+"]");
-        compress.GZCompress(getParentFileName(), elapsedPeriodsFileName);
-        break;
-      case Compress.ZIP:
-        addInfo("ZIP compressing ["+elapsedPeriodsFileName+"]");
-        compress.ZIPCompress(getParentFileName(), elapsedPeriodsFileName);
-        break;
+      } else {
+        doCompression(true, getParentFileName(), elapsedPeriodsFileName);
       }
     }
-    
-    //let's update the parent active file name
+
+    // let's update the parent active file name
     setParentFileName(getNewActiveFileName());
-    
+
+  }
+
+  void doCompression(boolean rename, String nameOfFile2Compress,
+      String nameOfCompressedFile) throws RolloverFailure {
+    Compressor compressor = null;
+
+    if (rename) {
+      String renameTarget = nameOfFile2Compress + System.nanoTime() + ".tmp";
+      util.rename(getParentFileName(), renameTarget);
+      nameOfFile2Compress = renameTarget;
+    }
+
+    switch (compressionMode) {
+    case GZ:
+      addInfo("GZIP compressing [" + nameOfFile2Compress + "].");
+      compressor = new Compressor(CompressionMode.GZ, nameOfFile2Compress,
+          nameOfCompressedFile);
+      compressor.setContext(this.context);
+      break;
+    case ZIP:
+      addInfo("ZIP compressing [" + nameOfFile2Compress + "]");
+      compressor = new Compressor(CompressionMode.ZIP, nameOfFile2Compress,
+          nameOfCompressedFile);
+      compressor.setContext(this.context);
+      break;
+    }
+
+    AsynchronousCompressor ac = new AsynchronousCompressor(compressor);
+    future = ac.compressAsynchronously();
+
   }
 
   /**
-   *
+   * 
    * The active log file is determined by the value of the parent's filename
-   * option. However, in case the file name is left blank,
-   * then, the active log file equals the file name for the current period
-   * as computed by the <b>FileNamePattern</b> option.
+   * option. However, in case the file name is left blank, then, the active log
+   * file equals the file name for the current period as computed by the
+   * <b>FileNamePattern</b> option.
    * 
-   * 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.
+   * 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.
    * 
-   * 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.
+   * 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() {
-    if (getParentFileName() == null || getParentFileName() == lastGeneratedFileName) {
+    if (getParentFileName() == null
+        || getParentFileName() == lastGeneratedFileName) {
       String newName = activeFileNamePattern.convertDate(lastCheck);
       addInfo("Generated a new name for RollingFileAppender: " + newName);
       lastGeneratedFileName = newName;
@@ -186,32 +198,33 @@
   }
 
   public boolean isTriggeringEvent(File activeFile, final E event) {
-    long time= getCurrentTime();
+    long time = getCurrentTime();
 
     if (time >= nextCheck) {
-      //addInfo("Time to trigger roll-over");
-      // We set the elapsedPeriodsFileName before we set the 'lastCheck' variable
+      // addInfo("Time to trigger roll-over");
+      // 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);
+      // addInfo("elapsedPeriodsFileName set to "+elapsedPeriodsFileName);
 
       lastCheck.setTime(time);
       nextCheck = rc.getNextCheckMillis(lastCheck);
 
       Date x = new Date();
       x.setTime(nextCheck);
-      //addInfo("Next check on "+ x);
+      // addInfo("Next check on "+ x);
 
       return true;
     } else {
       return false;
     }
-  }  
+  }
 
   @Override
   public String toString() {
     return "c.q.l.core.rolling.TimeBasedRollingPolicy";
   }
-  
+
 }

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java	Sat Apr 26 01:05:56 2008
@@ -0,0 +1,19 @@
+package ch.qos.logback.core.rolling.helper;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+public class AsynchronousCompressor {
+  Compressor compressor;
+  
+  public AsynchronousCompressor(Compressor compressor) {
+    this.compressor = compressor;
+  }
+  
+  public Future<?> compressAsynchronously() {
+    ExecutorService executor = Executors.newScheduledThreadPool(1);
+    return executor.submit(new CompressionRunnable(compressor));
+  }
+  
+}

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionMode.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionMode.java	Sat Apr 26 01:05:56 2008
@@ -0,0 +1,5 @@
+package ch.qos.logback.core.rolling.helper;
+
+public enum CompressionMode {
+  NONE, GZ, ZIP;
+}

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java	Sat Apr 26 01:05:56 2008
@@ -0,0 +1,14 @@
+package ch.qos.logback.core.rolling.helper;
+
+
+public class CompressionRunnable implements Runnable {
+
+  final Compressor compressor;
+  public CompressionRunnable(Compressor compressor) {
+    this.compressor = compressor;
+  }
+
+  public void run() {
+    compressor.compress();
+  }
+}

Copied: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java (from r1666, /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compress.java)
==============================================================================
--- /logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compress.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java	Sat Apr 26 01:05:56 2008
@@ -16,7 +16,6 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
-import ch.qos.logback.core.Context;
 import ch.qos.logback.core.spi.ContextAwareBase;
 import ch.qos.logback.core.status.ErrorStatus;
 import ch.qos.logback.core.status.WarnStatus;
@@ -28,37 +27,37 @@
  * 
  * @author Ceki G&uuml;lc&uuml;
  */
-public class Compress extends ContextAwareBase {
-  public static final int NONE = 0;
-  public static final int GZ = 1;
-  public static final int ZIP = 2;
-  /**
-   * String constant representing no compression. The value of this constant is
-   * "NONE".
-   */
-  public static final String NONE_STR = "NONE";
-
-  /**
-   * String constant representing compression in the GZIP format. The value of
-   * this constant is "GZ".
-   */
-  public static final String GZ_STR = "GZ";
-
-  /**
-   * String constant representing compression in the ZIP format. The value of
-   * this constant is "ZIP".
-   */
-  public static final String ZIP_STR = "ZIP";
-
-  Context context;
-
-  public void ZIPCompress(String nameOfFile2zip) {
-    // Here we rely on the fact that the two argument version of ZIPCompress
-    // automatically adds a .zip extension to the second argument
-    GZCompress(nameOfFile2zip, nameOfFile2zip);
+public class Compressor extends ContextAwareBase {
+
+  final CompressionMode compressionMode;
+  final String nameOfFile2Compress;
+  final String nameOfCompressedFile;
+
+  
+  public Compressor(CompressionMode compressionMode, String nameOfFile2Compress, String nameOfCompressedFile) {
+    this.compressionMode = compressionMode;
+    this.nameOfFile2Compress = nameOfFile2Compress;
+    this.nameOfCompressedFile = nameOfCompressedFile;
+  }
+  
+  public Compressor(CompressionMode compressionMode, String nameOfFile2Compress) {
+    // Here we rely on the fact that the two argument version of ZIPCompress/GZCompress
+    // automatically adds a .zip/.gz extension to the second argument
+    this(compressionMode, nameOfFile2Compress, nameOfFile2Compress);
   }
 
-  public void ZIPCompress(String nameOfFile2zip, String nameOfZippedFile) {
+  public void compress() {
+    switch(compressionMode) {
+    case GZ: 
+      gzCompress(nameOfFile2Compress, nameOfCompressedFile);
+      break;
+    case ZIP:
+      zipCompress(nameOfFile2Compress, nameOfCompressedFile);
+      break;
+    }
+  }
+
+  private void zipCompress(String nameOfFile2zip, String nameOfZippedFile) {
     File file2zip = new File(nameOfFile2zip);
 
     if (!file2zip.exists()) {
@@ -111,13 +110,7 @@
     }
   }
 
-  public void GZCompress(String nameOfFile2gz) {
-    // Here we rely on the fact that the two argument version of GZCompress
-    // automatically adds a .gz extension to the second argument
-    GZCompress(nameOfFile2gz, nameOfFile2gz);
-  }
-
-  public void GZCompress(String nameOfFile2gz, String nameOfgzedFile) {
+  private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
     File file2gz = new File(nameOfFile2gz);
 
     if (!file2gz.exists()) {

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	Sat Apr 26 01:05:56 2008
@@ -10,18 +10,19 @@
 
 package ch.qos.logback.core.rolling;
 
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
 import ch.qos.logback.core.Context;
 import ch.qos.logback.core.ContextBase;
 import ch.qos.logback.core.layout.EchoLayout;
 import ch.qos.logback.core.util.Compare;
 import ch.qos.logback.core.util.Constants;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import java.io.File;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
 
 /**
  * A rather exhaustive set of tests. Tests include leaving the file option
@@ -241,9 +242,7 @@
       tbrp.setCurrentTime(addTime(tbrp.getCurrentTime(), 500));
     }
 
-    // for (int i = 0; i < 3; i++) {
-    // System.out.println(i + " expected filename [" + filenames[i] + "].");
-    // }
+    tbrp.future.get(1000, TimeUnit.MILLISECONDS);
 
     for (int i = 0; i < 2; i++) {
       assertTrue(Compare.gzCompare(filenames[i], Constants.TEST_DIR_PREFIX
@@ -424,6 +423,9 @@
     // System.out.println(i + " expected filename [" + filenames[i] + "].");
     // }
 
+    // wait for the compression task to finish
+    tbrp.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"));
@@ -432,7 +434,7 @@
     assertTrue(Compare.compare(filenames[2], Constants.TEST_DIR_PREFIX
         + "witness/rolling/tbr-test6.2"));
   }
-  
+
   public static Test suite() {
     TestSuite suite = new TestSuite();
     suite.addTestSuite(TimeBasedRollingTest.class);

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java	Sat Apr 26 01:05:56 2008
@@ -16,108 +16,106 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
-import junit.framework.Test;
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
 import ch.qos.logback.core.Context;
 import ch.qos.logback.core.ContextBase;
 import ch.qos.logback.core.util.Compare;
 import ch.qos.logback.core.util.Constants;
 import ch.qos.logback.core.util.StatusPrinter;
 
-
 /**
  * @author Ceki Gulcu
  */
 public class CompressTest extends TestCase {
 
-  Compress compress;
   Context context = new ContextBase();
-  
-  public CompressTest(String arg0) {
-    super(arg0);
-    compress = new Compress();
-    compress.setContext(context);
-  }
 
   public void setUp() throws IOException {
-	  //Copy source files
-	  //Delete output files
-	  {
-		  File source = new File(Constants.TEST_DIR_PREFIX + "input/compress1.copy");
-		  File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress1.txt");
-		  copy(source, dest);
-		  File target = new File(Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz");
-		  target.mkdirs();
-		  target.delete();
-	  }
-	  {
-		  File source = new File(Constants.TEST_DIR_PREFIX + "input/compress2.copy");
-		  File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress2.txt");
-		  copy(source, dest);
-		  File target = new File(Constants.TEST_DIR_PREFIX + "output/compress2.txt.gz");
-		  target.mkdirs();
-		  target.delete();
-	  }
-	  {
-		  File source = new File(Constants.TEST_DIR_PREFIX + "input/compress3.copy");
-		  File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress3.txt");
-		  copy(source, dest);
-		  File target = new File(Constants.TEST_DIR_PREFIX + "output/compress3.txt.zip");
-		  target.mkdirs();
-		  target.delete();
-	  }
+    // Copy source files
+    // Delete output files
+    {
+      File source = new File(Constants.TEST_DIR_PREFIX + "input/compress1.copy");
+      File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress1.txt");
+                                                        
+      copy(source, dest);
+      File target = new File(Constants.TEST_DIR_PREFIX
+          + "output/compress1.txt.gz");
+      target.mkdirs();
+      target.delete();
+    }
+    {
+      File source = new File(Constants.TEST_DIR_PREFIX + "input/compress2.copy");
+      File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress2.txt");
+      copy(source, dest);
+      File target = new File(Constants.TEST_DIR_PREFIX
+          + "output/compress2.txt.gz");
+      target.mkdirs();
+      target.delete();
+    }
+    {
+      File source = new File(Constants.TEST_DIR_PREFIX + "input/compress3.copy");
+      File dest = new File(Constants.TEST_DIR_PREFIX + "input/compress3.txt");
+      copy(source, dest);
+      File target = new File(Constants.TEST_DIR_PREFIX
+          + "output/compress3.txt.zip");
+      target.mkdirs();
+      target.delete();
+    }
   }
 
   public void tearDown() {
   }
 
   public void test1() throws Exception {
-	compress.GZCompress(Constants.TEST_DIR_PREFIX + "input/compress1.txt", Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz");
-  
-	StatusPrinter.print(context.getStatusManager());
-	assertEquals(0, context.getStatusManager().getCount());
-	assertTrue(Compare.gzCompare(Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz",
-			Constants.TEST_DIR_PREFIX + "witness/compress1.txt.gz"));
+    Compressor compressor = new Compressor(CompressionMode.GZ,
+        Constants.TEST_DIR_PREFIX + "input/compress1.txt",
+        Constants.TEST_DIR_PREFIX + "output/compress1.txt.gz");
+    compressor.setContext(context);
+    compressor.compress();
+
+    StatusPrinter.print(context.getStatusManager());
+    assertEquals(0, context.getStatusManager().getCount());
+    assertTrue(Compare.gzCompare(Constants.TEST_DIR_PREFIX
+        + "output/compress1.txt.gz", Constants.TEST_DIR_PREFIX
+        + "witness/compress1.txt.gz"));
   }
 
   public void test2() throws Exception {
-	compress.GZCompress(Constants.TEST_DIR_PREFIX + "input/compress2.txt", Constants.TEST_DIR_PREFIX + "output/compress2.txt");
-    
-	StatusPrinter.print(context.getStatusManager());
-	assertEquals(0, context.getStatusManager().getCount());
-    assertTrue(Compare.gzCompare(Constants.TEST_DIR_PREFIX + "output/compress2.txt.gz",
-    		Constants.TEST_DIR_PREFIX + "witness/compress2.txt.gz"));
+    Compressor compressor = new Compressor(CompressionMode.GZ,
+        Constants.TEST_DIR_PREFIX + "input/compress2.txt",
+        Constants.TEST_DIR_PREFIX + "output/compress2.txt");
+    compressor.setContext(context);
+    compressor.compress();
+
+    StatusPrinter.print(context.getStatusManager());
+    assertEquals(0, context.getStatusManager().getCount());
+    assertTrue(Compare.gzCompare(Constants.TEST_DIR_PREFIX
+        + "output/compress2.txt.gz", Constants.TEST_DIR_PREFIX
+        + "witness/compress2.txt.gz"));
   }
 
   public void test3() throws Exception {
-    compress.ZIPCompress(Constants.TEST_DIR_PREFIX + "input/compress3.txt", Constants.TEST_DIR_PREFIX + "output/compress3.txt");
-	
-	StatusPrinter.print(context.getStatusManager());
-	assertEquals(0, context.getStatusManager().getCount());
+    Compressor compressor = new Compressor(CompressionMode.ZIP, 
+        Constants.TEST_DIR_PREFIX + "input/compress3.txt",
+        Constants.TEST_DIR_PREFIX + "output/compress3.txt");
+    compressor.setContext(context);
+    compressor.compress();
+    StatusPrinter.print(context.getStatusManager());
+    assertEquals(0, context.getStatusManager().getCount());
     // assertTrue(Compare.compare("output/compress3.txt.zip",
     // "witness/compress3.txt.zip"));
   }
 
-  public static Test suite() {
-    TestSuite suite = new TestSuite();
-    suite.addTest(new CompressTest("test1"));
-    suite.addTest(new CompressTest("test2"));
-    suite.addTest(new CompressTest("test3"));
-    return suite;
-  }
-  
   private void copy(File src, File dst) throws IOException {
-      InputStream in = new FileInputStream(src);
-      OutputStream out = new FileOutputStream(dst);
-      byte[] buf = new byte[1024];
-      int len;
-      while ((len = in.read(buf)) > 0) {
-          out.write(buf, 0, len);
-      }
-      in.close();
-      out.close();
+    InputStream in = new FileInputStream(src);
+    OutputStream out = new FileOutputStream(dst);
+    byte[] buf = new byte[1024];
+    int len;
+    while ((len = in.read(buf)) > 0) {
+      out.write(buf, 0, len);
+    }
+    in.close();
+    out.close();
   }
-  
-  
+
 }



More information about the logback-dev mailing list