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

noreply.seb at qos.ch noreply.seb at qos.ch
Thu Sep 7 18:58:28 CEST 2006


Author: seb
Date: Thu Sep  7 18:58:28 2006
New Revision: 522

Added:
   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/SizeBasedTriggeringPolicyTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TriggeringPolicyBase.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java

Log:
- modified maxFileSize attribute type to String
- added maxFileSizeAsLong attribute
- updated tests and package tests
- created a test for the StringToLong method

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicy.java	Thu Sep  7 18:58:28 2006
@@ -26,27 +26,59 @@
    */
   public static final long DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB
   
-  long maxFileSize = DEFAULT_MAX_FILE_SIZE; 
+  String maxFileSize = Long.toString(DEFAULT_MAX_FILE_SIZE); 
+  long maxFileSizeAsLong;
 
   public SizeBasedTriggeringPolicy() {
   }
 
-  public SizeBasedTriggeringPolicy(final long maxFileSize) {
-      this.maxFileSize = maxFileSize;
+  public SizeBasedTriggeringPolicy(final String maxFileSize) {
+      setMaxFileSize(maxFileSize);
   }
 
-
   public boolean isTriggeringEvent(final File file, final Object event) {
     //System.out.println("Size"+file.length());
-    return (file.length() >= maxFileSize);
+    return (file.length() >= maxFileSizeAsLong);
   }
 
-  public long getMaxFileSize() {
+  public String getMaxFileSize() {
     return maxFileSize;
   }
 
-  public void setMaxFileSize(long l) {
-    maxFileSize = l;
+  public void setMaxFileSize(String maxFileSize) {
+    this.maxFileSize = maxFileSize;
+    this.maxFileSizeAsLong = toFileSize(maxFileSize);
   }
   
+  long toFileSize(String value) {
+    if(value == null)
+      return DEFAULT_MAX_FILE_SIZE;
+
+    String s = value.trim().toUpperCase();
+    long multiplier = 1;
+    int index;
+
+    if((index = s.indexOf("KB")) != -1) {
+      multiplier = 1024;
+      s = s.substring(0, index);
+    }
+    else if((index = s.indexOf("MB")) != -1) {
+      multiplier = 1024*1024;
+      s = s.substring(0, index);
+    }
+    else if((index = s.indexOf("GB")) != -1) {
+      multiplier = 1024*1024*1024;
+      s = s.substring(0, index);
+    }
+    if(s != null) {
+      try {
+        return Long.valueOf(s).longValue() * multiplier;
+      }
+      catch (NumberFormatException e) {
+        addError("[" + s + "] is not in proper int form.");
+        addError("[" + value + "] not in expected format.", e);
+      }
+    }
+    return DEFAULT_MAX_FILE_SIZE;
+  } 
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TriggeringPolicyBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TriggeringPolicyBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TriggeringPolicyBase.java	Thu Sep  7 18:58:28 2006
@@ -10,6 +10,8 @@
 
 package ch.qos.logback.core.rolling;
 
+import ch.qos.logback.core.spi.ContextAwareBase;
+
 
 /**
  * SizeBasedTriggeringPolicy looks at size of the file being
@@ -18,7 +20,7 @@
  * @author Ceki Gülcü
  *
  */
-abstract public class TriggeringPolicyBase implements TriggeringPolicy {
+abstract public class TriggeringPolicyBase extends ContextAwareBase implements TriggeringPolicy {
   
   private boolean start;
 

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/AllTest.java	Thu Sep  7 18:58:28 2006
@@ -14,6 +14,7 @@
     suite.addTest(ch.qos.logback.core.pattern.AllTest.suite());
     suite.addTest(ch.qos.logback.core.appender.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.rolling.helper.PackageTest.suite());
+    suite.addTest(ch.qos.logback.core.rolling.PackageTest.suite());
     suite.addTest(ch.qos.logback.core.joran.spi.PackageTest.suite());
     return suite;
   }

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/PackageTest.java	Thu Sep  7 18:58:28 2006
@@ -0,0 +1,16 @@
+package ch.qos.logback.core.rolling;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class PackageTest extends TestCase {
+
+	public static Test suite() {
+    TestSuite suite = new TestSuite();
+    suite.addTestSuite(RenamingTest.class);
+    suite.addTestSuite(SizeBasedRollingTest.class);
+    suite.addTestSuite(TimeBasedRollingTest.class);
+    return suite;
+  }
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedRollingTest.java	Thu Sep  7 18:58:28 2006
@@ -21,6 +21,8 @@
 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;
 import ch.qos.logback.core.layout.DummyLayout;
 import ch.qos.logback.core.util.Compare;
@@ -29,9 +31,10 @@
 
 /**
  * 
- * Do not forget to call activateOptions when configuring programatically.
+ * Do not forget to call start() when configuring programatically.
  * 
  * @author Ceki Gülcü
+ * @author Sébastien Pennec
  * 
  */
 public class SizeBasedRollingTest extends TestCase {
@@ -42,14 +45,14 @@
   
   public void setUp() {
 	  {
-		File target = new File(Constants.TEST_DIR_PREFIX + "output/sizeBased-test2.log");
-		target.mkdirs();
-		target.delete();
+			File target = new File(Constants.TEST_DIR_PREFIX + "output/sizeBased-test2.log");
+			target.mkdirs();
+			target.delete();
 	  }
 	  {
-		File target = new File(Constants.TEST_DIR_PREFIX + "output/sbr-test3.log");
-		target.mkdirs();
-		target.delete();
+			File target = new File(Constants.TEST_DIR_PREFIX + "output/sbr-test3.log");
+			target.mkdirs();
+			target.delete();
 	  }
   }
 
@@ -63,13 +66,18 @@
   public void test1() throws Exception {
     // We purposefully use the \n as the line separator.
     // This makes the regression test system independent.
+  	Context context = new ContextBase();
     Layout layout = new DummyLayout();
     RollingFileAppender rfa = new RollingFileAppender();
     rfa.setLayout(layout);
+    rfa.setContext(new ContextBase());
 
     FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy();
+    fwrp.setContext(context);
     SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
-    sbtp.setMaxFileSize(100);
+    sbtp.setContext(context);
+    
+    sbtp.setMaxFileSize("100");
     sbtp.start();
     fwrp.setFileNamePattern(Constants.TEST_DIR_PREFIX + "output/sizeBased-test1.%i");
     try {
@@ -78,22 +86,28 @@
     } catch (IllegalStateException e) {
       return;
     }
+    
+    //StatusPrinter.print(context.getStatusManager()); 
   }
 
   /**
    * Test basic rolling functionality.
    */
   public void test2() throws Exception {
-
+  	Context context = new ContextBase();
+  	
     DummyLayout layout = new DummyLayout();
     RollingFileAppender rfa = new RollingFileAppender();
     rfa.setName("ROLLING");
     rfa.setLayout(layout);
-
+    rfa.setContext(context);
+    
     FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy();
+    swrp.setContext(context);
     SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
+    sbtp.setContext(context);
 
-    sbtp.setMaxFileSize(100);
+    sbtp.setMaxFileSize("100");
     swrp.setMinIndex(0);
     swrp.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/sizeBased-test2.log");
 
@@ -136,20 +150,26 @@
       assertTrue(Compare.compare(Constants.TEST_DIR_PREFIX + "output/sizeBased-test2.1",
     		  Constants.TEST_DIR_PREFIX + "witness/rolling/sbr-test2.1"));
     }
+    
+    //StatusPrinter.print(context.getStatusManager());
   }
 
   /**
    * Same as testBasic but also with GZ compression.
    */
   public void test3() throws Exception {
+  	Context context = new ContextBase();
     DummyLayout layout = new DummyLayout();
     RollingFileAppender rfa = new RollingFileAppender();
     rfa.setLayout(layout);
-
+    rfa.setContext(context);
+    
     FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy();
+    fwrp.setContext(context);
     SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
-
-    sbtp.setMaxFileSize(100);
+    sbtp.setContext(context);
+    
+    sbtp.setMaxFileSize("100");
     fwrp.setMinIndex(0);
     fwrp.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/sbr-test3.log");
     fwrp.setFileNamePattern(Constants.TEST_DIR_PREFIX + "output/sbr-test3.%i.gz");
@@ -189,6 +209,7 @@
     		  Constants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.1.gz"));
     }
 
+    //StatusPrinter.print(context.getStatusManager());
   }
 
   boolean isWindows() {

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicyTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeBasedTriggeringPolicyTest.java	Thu Sep  7 18:58:28 2006
@@ -0,0 +1,44 @@
+package ch.qos.logback.core.rolling;
+
+import junit.framework.TestCase;
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+
+public class SizeBasedTriggeringPolicyTest extends TestCase {
+
+	public void testStringToLong() {
+		Context context = new ContextBase();
+		SizeBasedTriggeringPolicy policy = new SizeBasedTriggeringPolicy();
+		policy.setContext(context);
+
+		Long result;
+
+		{
+			result = policy.toFileSize("123");
+			assertEquals(new Long("123"), result);
+		}
+		{
+			result = policy.toFileSize("123KB");
+			// = 123 * 1024
+			assertEquals(new Long("125952"), result);
+		}
+		{
+			result = policy.toFileSize("123MB");
+			// = 123 * 1024 * 1024
+			assertEquals(new Long("128974848"), result);
+		}
+		{
+			result = policy.toFileSize("123GB");
+			// = 123 * 1024 * 1024 * 1024
+			assertEquals(new Long("132070244352"), result);
+		}
+
+		{
+			result = policy.toFileSize("123xxxx");
+			// = 123 * 1024 * 1024 * 1024
+			assertEquals(new Long(SizeBasedTriggeringPolicy.DEFAULT_MAX_FILE_SIZE), result);
+			assertEquals(2, context.getStatusManager().getCount());
+		}
+
+	}
+}



More information about the logback-dev mailing list