[logback-dev] svn commit: r2385 - in logback/trunk/logback-core/src/test/java/ch/qos/logback/core: rolling testUtil

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Jul 30 23:27:26 CEST 2009


Author: ceki
Date: Thu Jul 30 23:27:25 2009
New Revision: 2385

Added:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java
Modified:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java

Log:
Initial test case for SizeAndTimeBasedFileNamingAndTriggeringPolicy


Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java	Thu Jul 30 23:27:25 2009
@@ -7,6 +7,7 @@
 @Suite.SuiteClasses( { RenamingTest.class, SizeBasedRollingTest.class,
     TimeBasedRollingTest.class, TimeBasedRollingWithCleanTest.class,
     MultiThreadedRollingTest.class,
+    SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.class,
     ch.qos.logback.core.rolling.helper.PackageTest.class })
 public class PackageTest {
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFileNamingAndTriggeringPolicyTest.java	Thu Jul 30 23:27:25 2009
@@ -1,7 +1,20 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
 package ch.qos.logback.core.rolling;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.io.File;
+import java.io.IOException;
 import java.sql.Date;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -14,16 +27,15 @@
 import ch.qos.logback.core.Context;
 import ch.qos.logback.core.ContextBase;
 import ch.qos.logback.core.layout.EchoLayout;
+import ch.qos.logback.core.testUtil.FileToBufferUtil;
 import ch.qos.logback.core.testUtil.RandomUtil;
-import ch.qos.logback.core.util.Compare;
 import ch.qos.logback.core.util.CoreTestConstants;
 
 public class SizeAndTimeBasedFileNamingAndTriggeringPolicyTest {
   static final String DATE_PATTERN = "yyyy-MM-dd_HH_mm_ss";
 
   int diff = RandomUtil.getPositiveInt();
-  String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + "/" + diff
-      + "/";
+  String randomOutputDir = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "/";
 
   SizeAndTimeBasedFileNamingAndTriggeringPolicy<Object> satbfnatPolicy = new SizeAndTimeBasedFileNamingAndTriggeringPolicy<Object>();
 
@@ -42,15 +54,16 @@
   int fileSize = 0;
   int fileIndexCounter = 0;
   int sizeThreshold;
-  
+
   @Before
   public void setUp() {
     context.setName("test");
     cal.set(Calendar.MILLISECOND, 333);
     currentTime = cal.getTimeInMillis();
     recomputeRolloverThreshold(currentTime);
-    System.out.println("at setUp() currentTime="
-        + sdf.format(new Date(currentTime)));
+    System.out.println(randomOutputDir);
+    // System.out.println("at setUp() currentTime="
+    // + sdf.format(new Date(currentTime)));
 
   }
 
@@ -68,11 +81,12 @@
     }
   }
 
-  void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp,
-      String filenamePattern, int sizeThreshold, long givenTime, long lastCheck) {
+  void initTRBP(RollingFileAppender<Object> rfa,
+      TimeBasedRollingPolicy<Object> tbrp, String filenamePattern,
+      int sizeThreshold, long givenTime, long lastCheck) {
 
     tbrp.setContext(context);
-    satbfnatPolicy.setMaxFileSize(""+sizeThreshold);
+    satbfnatPolicy.setMaxFileSize("" + sizeThreshold);
     tbrp.setTimeBasedTriggering(satbfnatPolicy);
     tbrp.setFileNamePattern(filenamePattern);
     tbrp.setParent(rfa);
@@ -88,81 +102,113 @@
   @Test
   public void noCompression_FileBSet_NoRestart_1() throws Exception {
     String testId = "test1";
-    System.out.println(randomOutputDir);
     String file = randomOutputDir + "toto.log";
     initRFA(rfa1, file);
     sizeThreshold = 300;
     initTRBP(rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN
         + "}-%i.txt", sizeThreshold, currentTime, 0);
 
-    addExpectedFileName(testId, getDateOfCurrentPeriodsStart(), fileIndexCounter, false);
+    addExpectedFileName(testId, getDateOfCurrentPeriodsStart(),
+        fileIndexCounter, false);
 
     incCurrentTime(100);
     tbrp1.timeBasedTriggering.setCurrentTime(currentTime);
 
-    for (int i = 0; i < 100; i++) {
-      String msg = "Hello -----------------" + i;
+    int runLength = 100;
+    String prefix = "Hello -----------------";
+    
+    for (int i = 0; i < runLength; i++) {
+      String msg = prefix + i;
       rfa1.doAppend(msg);
       addExpectedFileNamedIfItsTime(testId, msg, false);
       incCurrentTime(20);
       tbrp1.timeBasedTriggering.setCurrentTime(currentTime);
     }
-    
 
     massageExpectedFilesToCorresponToCurrentTarget(file);
-    int i = 0;
-    for (String fn : expectedFilenameList) {
-      System.out.println(fn);
-      //assertTrue(Compare.compare(fn, CoreTestConstants.TEST_DIR_PREFIX
-      //    + "witness/rolling/satb-test1." + i++));
+    existenceCheck(expectedFilenameList);
+    contentCheck(runLength, prefix);
+   
+  }
+
+  void existenceCheck(String filename) {
+    assertTrue("File " + filename + " does not exist", new File(filename)
+        .exists());
+  }
+
+  void contentCheck(int runLength, String prefix) throws IOException {
+    File outputDir = new File(randomOutputDir);
+    File[] fileArray = outputDir.listFiles();
+    List<String> stringList = new ArrayList<String>();
+    for (File file : fileArray) {
+      FileToBufferUtil.readIntoList(file, stringList);
+    }
+    
+    List<String> witnessList = new ArrayList<String>();
+    
+    for(int i = 0; i < runLength; i++) {
+      witnessList.add(prefix+i);
+    }
+    assertEquals(witnessList, stringList);
+  }
+
+  void existenceCheck(List<String> filenameList) {
+    for (String filename : filenameList) {
+      assertTrue("File " + filename + " does not exist", new File(filename)
+          .exists());
     }
   }
-  
+
   void massageExpectedFilesToCorresponToCurrentTarget(String file) {
     // we added one too many files by date
     expectedFilenameList.remove(expectedFilenameList.size() - 1);
     expectedFilenameList.add(file);
   }
-  
+
   boolean passThresholdTime(long nextRolloverThreshold) {
     return currentTime >= nextRolloverThreshold;
   }
-  
-  void addExpectedFileNamedIfItsTime(String testId, String msg, boolean gzExtension) {
+
+  void addExpectedFileNamedIfItsTime(String testId, String msg,
+      boolean gzExtension) {
     fileSize += msg.getBytes().length;
-    
+
     if (passThresholdTime(nextRolloverThreshold)) {
       fileIndexCounter = 0;
       fileSize = 0;
-      addExpectedFileName(testId, getDateOfCurrentPeriodsStart(), fileIndexCounter,
-          gzExtension);
+      addExpectedFileName(testId, getDateOfCurrentPeriodsStart(),
+          fileIndexCounter, gzExtension);
       recomputeRolloverThreshold(currentTime);
       return;
     }
-    
-    // windows can delay file size changes
-    if((fileIndexCounter <= 1) && fileSize > sizeThreshold) {
-      addExpectedFileName(testId, getDateOfCurrentPeriodsStart(), ++fileIndexCounter,
-          gzExtension);
+
+    // windows can delay file size changes, so we only allow for
+    // fileIndexCounter 0 and 1
+    if ((fileIndexCounter < 1) && fileSize > sizeThreshold) {
+      addExpectedFileName(testId, getDateOfCurrentPeriodsStart(),
+          ++fileIndexCounter, gzExtension);
       fileSize = 0;
       return;
     }
-    
+
   }
 
-  void addExpectedFileName(String testId, Date date, int fileIndexCounter, boolean gzExtension) {
-    String fn = CoreTestConstants.OUTPUT_DIR_PREFIX + testId + "-" + sdf.format(date)+"-"+fileIndexCounter+".txt";
+  void addExpectedFileName(String testId, Date date, int fileIndexCounter,
+      boolean gzExtension) {
+
+    String fn = randomOutputDir + testId + "-" + sdf.format(date) + "-"
+        + fileIndexCounter + ".txt";
     if (gzExtension) {
       fn += ".gz";
     }
     expectedFilenameList.add(fn);
   }
-  
+
   Date getDateOfCurrentPeriodsStart() {
     long delta = currentTime % 1000;
     return new Date(currentTime - delta);
   }
-  
+
   void incCurrentTime(long increment) {
     currentTime += increment;
   }

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 Jul 30 23:27:25 2009
@@ -40,13 +40,13 @@
  * with witness files.
  * 
  * <pre>
- *               Compression     file option    Stop/Restart 
- *    Test1      NO              BLANK           NO
- *    Test2      YES             BLANK           NO
- *    Test3      NO              BLANK           YES
- *    Test4      NO              SET             YES 
- *    Test5      NO              SET             NO
- *    Test6      YES             SET             NO
+ *                Compression     file option    Stop/Restart 
+ *     Test1      NO              BLANK           NO
+ *     Test2      YES             BLANK           NO
+ *     Test3      NO              BLANK           YES
+ *     Test4      NO              SET             YES 
+ *     Test5      NO              SET             NO
+ *     Test6      YES             SET             NO
  * </pre>
  * 
  * @author Ceki G&uuml;lc&uuml;
@@ -60,10 +60,10 @@
   Context context = new ContextBase();
 
   RollingFileAppender<Object> rfa1 = new RollingFileAppender<Object>();
-  TimeBasedRollingPolicy tbrp1 = new TimeBasedRollingPolicy();
+  TimeBasedRollingPolicy<Object> tbrp1 = new TimeBasedRollingPolicy<Object>();
 
   RollingFileAppender<Object> rfa2 = new RollingFileAppender<Object>();
-  TimeBasedRollingPolicy tbrp2 = new TimeBasedRollingPolicy();
+  TimeBasedRollingPolicy<Object> tbrp2 = new TimeBasedRollingPolicy<Object>();
 
   Calendar cal = Calendar.getInstance();
   long currentTime; // initialized in setUp()
@@ -76,7 +76,8 @@
     cal.set(Calendar.MILLISECOND, 333);
     currentTime = cal.getTimeInMillis();
     recomputeRolloverThreshold(currentTime);
-    System.out.println("at setUp() currentTime=" + sdf.format(new Date(currentTime)));
+    System.out.println("at setUp() currentTime="
+        + sdf.format(new Date(currentTime)));
 
     // Delete .log files
     deleteStaleLogFile("test4.log");
@@ -103,11 +104,12 @@
     }
   }
 
-  void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy tbrp,
+  void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp,
       String filenamePattern, long givenTime, long lastCheck) {
     tbrp.setContext(context);
     tbrp.setFileNamePattern(filenamePattern);
     tbrp.setParent(rfa);
+    tbrp.timeBasedTriggering = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
     tbrp.timeBasedTriggering.setCurrentTime(givenTime);
     if (lastCheck != 0) {
       tbrp.timeBasedTriggering.setDateInCurrentPeriod(new Date(lastCheck));
@@ -222,7 +224,7 @@
           + "witness/rolling/tbr-" + testId + "." + i++));
     }
   }
-  
+
   /**
    * Without compression, file option set, with stop/restart
    */
@@ -293,7 +295,7 @@
     incCurrentTime(2000);
 
     initRFA(rfa2, CoreTestConstants.OUTPUT_DIR_PREFIX + "test4B.log");
-    initTRBP(rfa2, tbrp2, CoreTestConstants.OUTPUT_DIR_PREFIX + testId +"-%d{"
+    initTRBP(rfa2, tbrp2, CoreTestConstants.OUTPUT_DIR_PREFIX + testId + "-%d{"
         + DATE_PATTERN + "}", currentTime, fileTimestamp);
 
     for (int i = 0; i <= 2; i++) {
@@ -407,7 +409,8 @@
   }
 
   void addExpectedFileName_ByDate(String testId, Date date, boolean gzExtension) {
-    String fn = CoreTestConstants.OUTPUT_DIR_PREFIX + testId + "-" + sdf.format(date);
+    String fn = CoreTestConstants.OUTPUT_DIR_PREFIX + testId + "-"
+        + sdf.format(date);
     if (gzExtension) {
       fn += ".gz";
     }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/TimeBasedRollingWithCleanTest.java	Thu Jul 30 23:27:25 2009
@@ -72,12 +72,13 @@
     rfa.setContext(context);
     rfa.setLayout(layout);
     // rfa.setFile(Constants.OUTPUT_DIR_PREFIX + "clean.txt");
-    TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
+    TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
     tbrp.setContext(context);
     tbrp.setFileNamePattern(fileNamePattern);
 
     tbrp.setMaxHistory(maxHistory);
     tbrp.setParent(rfa);
+    tbrp.timeBasedTriggering = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
     tbrp.timeBasedTriggering.setCurrentTime(currentTime);
     tbrp.start();
     rfa.setRollingPolicy(tbrp);

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/FileToBufferUtil.java	Thu Jul 30 23:27:25 2009
@@ -0,0 +1,33 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+
+package ch.qos.logback.core.testUtil;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.List;
+
+public class FileToBufferUtil {
+
+  static public void readIntoList(File file, List<String> stringList)
+      throws IOException {
+
+    FileInputStream fis = new FileInputStream(file);
+    BufferedReader in = new BufferedReader(new InputStreamReader(fis));
+    String line;
+    while( (line = in.readLine()) != null) {
+      stringList.add(line);
+    }
+    in.close();
+  }
+}


More information about the logback-dev mailing list