logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r398081 - in /logging/log4j/branches/v1_2-branch/tests: build.xml input/RFA1.properties src/java/org/apache/log4j/DRFATestCase.java src/java/org/apache/log4j/RFATestCase.java src/java/org/apache/log4j/varia/ERFATestCase.java
Date Sat, 29 Apr 2006 04:13:00 GMT
Author: carnold
Date: Fri Apr 28 21:12:54 2006
New Revision: 398081

URL: http://svn.apache.org/viewcvs?rev=398081&view=rev
Log:
Bug 39023: o.a.l.DRFA, RFA and .varia.ERFA not compatibile

Added:
    logging/log4j/branches/v1_2-branch/tests/input/RFA1.properties
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/RFATestCase.java
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/varia/ERFATestCase.java
Modified:
    logging/log4j/branches/v1_2-branch/tests/build.xml
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/DRFATestCase.java

Modified: logging/log4j/branches/v1_2-branch/tests/build.xml
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/build.xml?rev=398081&r1=398080&r2=398081&view=diff
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/build.xml (original)
+++ logging/log4j/branches/v1_2-branch/tests/build.xml Fri Apr 28 21:12:54 2006
@@ -137,7 +137,7 @@
                                      OptionConverter, BoundedFIFO, 
                                      CyclicBuffer, OR, 
                                      LevelMatchFilter, PatternParser, 
-                                     PatternLayout, DRFA, NTEventLogAppender, Syslog"/>
+                                     PatternLayout, RFA, ERFA, DRFA, NTEventLogAppender,
Syslog"/>
 
   <!-- ================================================================= -->
   <!-- Longer unit tests                                                 -->
@@ -360,6 +360,22 @@
       <classpath refid="tests.classpath"/>
       <formatter type="plain" usefile="false"/>
       <test name="org.apache.log4j.DRFATestCase" />
+    </junit>
+  </target>
+
+  <target name="RFA" depends="build">
+    <junit printsummary="yes" fork="yes" haltonfailure="${haltonfailure}">
+      <classpath refid="tests.classpath"/>
+      <formatter type="plain" usefile="false"/>
+      <test name="org.apache.log4j.RFATestCase" />
+    </junit>
+  </target>
+
+  <target name="ERFA" depends="build">
+    <junit printsummary="yes" fork="yes" haltonfailure="${haltonfailure}">
+      <classpath refid="tests.classpath"/>
+      <formatter type="plain" usefile="false"/>
+      <test name="org.apache.log4j.varia.ERFATestCase" />
     </junit>
   </target>
 

Added: logging/log4j/branches/v1_2-branch/tests/input/RFA1.properties
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/input/RFA1.properties?rev=398081&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/input/RFA1.properties (added)
+++ logging/log4j/branches/v1_2-branch/tests/input/RFA1.properties Fri Apr 28 21:12:54 2006
@@ -0,0 +1,12 @@
+log4j.rootLogger=DEBUG, testAppender
+log4j.appender.testAppender=org.apache.log4j.RollingFileAppender
+log4j.appender.testAppender.file=output/RFA-test1.log
+log4j.appender.testAppender.Append=false
+log4j.appender.testAppender.layout=org.apache.log4j.PatternLayout
+log4j.appender.testAppender.layout.ConversionPattern=%m\n
+log4j.appender.testAppender.maxFileSize=100
+
+#  Prevent internal log4j DEBUG messages from polluting the output.
+log4j.logger.org.apache.log4j.PropertyConfigurator=INFO
+log4j.logger.org.apache.log4j.config.PropertySetter=INFO
+log4j.logger.org.apache.log4j.FileAppender=INFO

Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/DRFATestCase.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/DRFATestCase.java?rev=398081&r1=398080&r2=398081&view=diff
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/DRFATestCase.java (original)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/DRFATestCase.java Fri
Apr 28 21:12:54 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,103 +16,122 @@
 
 package org.apache.log4j;
 
-import java.util.Calendar;
-import java.util.Date;
-import java.util.TimeZone;
-
 import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.framework.TestFailure;
-import junit.framework.Test;
 
-import org.apache.log4j.*;
+import java.io.File;
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.TimeZone;
+import java.util.Date;
+import java.text.SimpleDateFormat;
 
 /**
    Exhaustive test of the DailyRollingFileAppender compute algorithm.
-   
+
    @author Ceki G&uuml;lc&uuml;
+   @author Curt Arnold
  */
 public class DRFATestCase extends TestCase {
 
-  public DRFATestCase(String name) {
+    /**
+     * Create new test.
+     * @param name test name.
+     */
+  public DRFATestCase(final String name) {
     super(name);
   }
 
+    /**
+     * Reset configuration after every test.
+     */
+  public void tearDown() {
+      LogManager.resetConfiguration();
+  }
+
+    /**
+     * Test prediction of check period.
+     */
   public
   void testComputeCheckPeriod() {
     DailyRollingFileAppender drfa = new DailyRollingFileAppender();
     drfa.setName("testComputeCheckPeriod");
     drfa.setDatePattern("yyyy-MM-dd.'log'");
     drfa.activateOptions();
-    
+
     int x = drfa.computeCheckPeriod();
     int y = DailyRollingFileAppender.TOP_OF_DAY;
-    assertEquals(drfa.computeCheckPeriod(), 
-		 DailyRollingFileAppender.TOP_OF_DAY);
+    assertEquals(drfa.computeCheckPeriod(),
+         DailyRollingFileAppender.TOP_OF_DAY);
 
     drfa.setDatePattern("yyyy-MM-dd mm.'log'");
-    assertEquals(drfa.computeCheckPeriod(), 
-		 DailyRollingFileAppender.TOP_OF_MINUTE);
+    assertEquals(drfa.computeCheckPeriod(),
+         DailyRollingFileAppender.TOP_OF_MINUTE);
 
     drfa.setDatePattern("yyyy-MM-dd a.'log'");
-    assertEquals(drfa.computeCheckPeriod(), 
-		 DailyRollingFileAppender.HALF_DAY);
+    assertEquals(drfa.computeCheckPeriod(),
+         DailyRollingFileAppender.HALF_DAY);
 
     drfa.setDatePattern("yyyy-MM-dd HH.'log'");
-    assertEquals(drfa.computeCheckPeriod(), 
-		 DailyRollingFileAppender.TOP_OF_HOUR);
+    assertEquals(drfa.computeCheckPeriod(),
+         DailyRollingFileAppender.TOP_OF_HOUR);
 
     drfa.setDatePattern("yyyy-MM.'log'");
-    assertEquals(drfa.computeCheckPeriod(), 
-		 DailyRollingFileAppender.TOP_OF_MONTH);
+    assertEquals(drfa.computeCheckPeriod(),
+         DailyRollingFileAppender.TOP_OF_MONTH);
 
     drfa.setDatePattern("'log'HH'log'");
-    assertEquals(drfa.computeCheckPeriod(), 
-		 DailyRollingFileAppender.TOP_OF_HOUR);
+    assertEquals(drfa.computeCheckPeriod(),
+         DailyRollingFileAppender.TOP_OF_HOUR);
   }
 
 
+    /**
+     *   Test of RollingCalendar.
+     */
   public
-  void testRC1() {  
+  void testRC1() {
     RollingCalendar rc = new RollingCalendar();
     rc.setType(DailyRollingFileAppender.TOP_OF_DAY);
 
     Calendar c = Calendar.getInstance();
 
     // jan, mar, may, july, aug, oct, dec have 31 days
-    int [] M31 = {0,2,4,6,7,9,11}; 
+    int [] M31 = {0,2,4,6,7,9,11};
 
     for(int i = 0; i < M31.length; i ++) {
       for(int d = 1; d <=31; d++) {
-	for(int h = 0; h < 23; h++) {
-	  c.clear();
-	  c.set(Calendar.YEAR, 20);
-	  c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
-	  c.set(Calendar.DAY_OF_MONTH, d); 
-	  c.set(Calendar.HOUR_OF_DAY, h);
-	  c.set(Calendar.MINUTE, 10); 
-	  c.set(Calendar.SECOND, 10);
-	  c.set(Calendar.MILLISECOND, 88);
-    
-	  c.setTime(rc.getNextCheckDate(c.getTime()));
-	  if(d == 31) {
-	    assertEquals(c.get(Calendar.MONTH),(Calendar.JANUARY+M31[i]+1)%12);
-	    assertEquals(c.get(Calendar.DAY_OF_MONTH), 1);
-	  } else {
-	    assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
-	    assertEquals(c.get(Calendar.DAY_OF_MONTH), d+1);
-	  }
-	  assertEquals(c.get(Calendar.HOUR_OF_DAY), 0);
-	  assertEquals(c.get(Calendar.MINUTE), 0);
-	  assertEquals(c.get(Calendar.SECOND), 0);
-	  assertEquals(c.get(Calendar.MILLISECOND), 0);
-	}
+    for(int h = 0; h < 23; h++) {
+      c.clear();
+      c.set(Calendar.YEAR, 20);
+      c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
+      c.set(Calendar.DAY_OF_MONTH, d);
+      c.set(Calendar.HOUR_OF_DAY, h);
+      c.set(Calendar.MINUTE, 10);
+      c.set(Calendar.SECOND, 10);
+      c.set(Calendar.MILLISECOND, 88);
+
+      c.setTime(rc.getNextCheckDate(c.getTime()));
+      if(d == 31) {
+        assertEquals(c.get(Calendar.MONTH),(Calendar.JANUARY+M31[i]+1)%12);
+        assertEquals(c.get(Calendar.DAY_OF_MONTH), 1);
+      } else {
+        assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
+        assertEquals(c.get(Calendar.DAY_OF_MONTH), d+1);
+      }
+      assertEquals(c.get(Calendar.HOUR_OF_DAY), 0);
+      assertEquals(c.get(Calendar.MINUTE), 0);
+      assertEquals(c.get(Calendar.SECOND), 0);
+      assertEquals(c.get(Calendar.MILLISECOND), 0);
+    }
       }
     }
   }
 
+    /**
+     * RollingCalendar test.
+     */
   public
-  void testRC2() {  
+  void testRC2() {
     RollingCalendar rc = new RollingCalendar();
 
     rc.setType(DailyRollingFileAppender.TOP_OF_HOUR);
@@ -121,67 +140,69 @@
     TimeZone tz = c.getTimeZone();
 
     // jan, mar, may, july, aug, oct, dec have 31 days
-    int [] M31 = {0,2,4,6,7,9,11}; 
+    int [] M31 = {0,2,4,6,7,9,11};
 
     for(int i = 0; i < M31.length; i ++) {
       System.out.println("Month = "+(M31[i]+1));
       for(int d = 1; d <= 31; d++) {
-	for(int h = 0; h < 23; h++) {
-	  for(int m = 0; m <= 59; m++) {
-	    c.clear();
-	    c.set(Calendar.YEAR, 20);
-	    c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
-	    c.set(Calendar.DAY_OF_MONTH, d); 
-	    c.set(Calendar.HOUR_OF_DAY, h);
-	    c.set(Calendar.MINUTE, m); 
-	    c.set(Calendar.SECOND, 12);
-	    c.set(Calendar.MILLISECOND, 88);
-	    
-	    boolean dltState0 = c.getTimeZone().inDaylightTime(c.getTime());
-	    c.setTime(rc.getNextCheckDate(c.getTime()));	    
-	    boolean dltState1 = c.getTimeZone().inDaylightTime(c.getTime());
-
-	    assertEquals(c.get(Calendar.MILLISECOND), 0);
-	    assertEquals(c.get(Calendar.SECOND), 0);
-	    assertEquals(c.get(Calendar.MINUTE), 0);
-	    
-	    if(dltState0 == dltState1) {
-	      assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+1)%24);
-	    } else {
-	      // returning to standard time
-	      if(dltState0) {
-		assertEquals(c.get(Calendar.HOUR_OF_DAY), h);
-	      } else { // switching to day light saving time
-		//System.err.println("m="+m+", h="+h+", d="+d+", i="+i);
-		//if(h==2) {
-		// System.err.println(c);
-		//}
-		//assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+2)%24);
-	      }
-	    }
-
-	    if(h == 23) {
-	      assertEquals(c.get(Calendar.DAY_OF_MONTH), (d+1)%32);
-	      if(d == 31) {
-		assertEquals(c.get(Calendar.MONTH), 
-			     (Calendar.JANUARY+M31[i]+1)%12);
-	      } else {
-		assertEquals(c.get(Calendar.MONTH), 
-			     Calendar.JANUARY+M31[i]);
-	      }
-	    } else {
-	      assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
-	      assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
-	    }
-	  }
-	}
+    for(int h = 0; h < 23; h++) {
+      for(int m = 0; m <= 59; m++) {
+        c.clear();
+        c.set(Calendar.YEAR, 20);
+        c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
+        c.set(Calendar.DAY_OF_MONTH, d);
+        c.set(Calendar.HOUR_OF_DAY, h);
+        c.set(Calendar.MINUTE, m);
+        c.set(Calendar.SECOND, 12);
+        c.set(Calendar.MILLISECOND, 88);
+
+        boolean dltState0 = c.getTimeZone().inDaylightTime(c.getTime());
+        c.setTime(rc.getNextCheckDate(c.getTime()));
+        boolean dltState1 = c.getTimeZone().inDaylightTime(c.getTime());
+
+        assertEquals(c.get(Calendar.MILLISECOND), 0);
+        assertEquals(c.get(Calendar.SECOND), 0);
+        assertEquals(c.get(Calendar.MINUTE), 0);
+
+        if(dltState0 == dltState1) {
+          assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+1)%24);
+        } else {
+          // returning to standard time
+          if(dltState0) {
+        assertEquals(c.get(Calendar.HOUR_OF_DAY), h);
+          } else { // switching to day light saving time
+        //System.err.println("m="+m+", h="+h+", d="+d+", i="+i);
+        //if(h==2) {
+        // System.err.println(c);
+        //}
+        //assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+2)%24);
+          }
+        }
+
+        if(h == 23) {
+          assertEquals(c.get(Calendar.DAY_OF_MONTH), (d+1)%32);
+          if(d == 31) {
+        assertEquals(c.get(Calendar.MONTH),
+                 (Calendar.JANUARY+M31[i]+1)%12);
+          } else {
+        assertEquals(c.get(Calendar.MONTH),
+                 Calendar.JANUARY+M31[i]);
+          }
+        } else {
+          assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
+          assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
+        }
+      }
+    }
       }
     }
   }
 
-
+    /**
+     * RollingCalendar test.
+     */
   public
-  void testRC3() {  
+  void testRC3() {
     RollingCalendar rc = new RollingCalendar();
 
     rc.setType(DailyRollingFileAppender.TOP_OF_MINUTE);
@@ -191,80 +212,213 @@
     Calendar c = Calendar.getInstance();
 
     // jan, mar, may, july, aug, oct, dec have 31 days
-    int [] M31 = {2,9,0,4,6,7,11}; 
+    int [] M31 = {2,9,0,4,6,7,11};
 
     for(int i = 0; i < M31.length; i ++) {
       System.out.println("Month = "+(M31[i]+1));
       for(int d = 1; d <= 31; d++) {
-	for(int h = 0; h < 23; h++) {
-	  for(int m = 0; m < M.length; m++) {
-	    for(int s = 0; s < S.length; s++) {
-	      c.clear();
-	      c.set(Calendar.YEAR, 20);
-	      c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
-	      c.set(Calendar.DAY_OF_MONTH, d); 
-	      c.set(Calendar.HOUR_OF_DAY, h);
-	      c.set(Calendar.MINUTE, M[m]); 
-	      c.set(Calendar.SECOND, S[s]);
-	      c.set(Calendar.MILLISECOND, 88);
-	      c.add(Calendar.MILLISECOND, 1);
-	    
-	      boolean dltState0 = c.getTimeZone().inDaylightTime(c.getTime());
-
-	      c.setTime(rc.getNextCheckDate(c.getTime()));
-	      c.add(Calendar.MILLISECOND, 0);
-	      boolean dltState1 = c.getTimeZone().inDaylightTime(c.getTime());
-	       
-	      assertEquals(c.get(Calendar.MILLISECOND), 0);
-	      assertEquals(c.get(Calendar.SECOND), 0);
-	      assertEquals(c.get(Calendar.MINUTE), (M[m]+1)%60);
-
-	      if(M[m] == 59) {
-		if(dltState0 == dltState1) {
-		  assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+1)%24); 
-		}
-		if(h == 23) {
-		  assertEquals(c.get(Calendar.DAY_OF_MONTH), (d+1)%32);
-		  if(d == 31) {
-		      assertEquals(c.get(Calendar.MONTH), 
-		    	 (Calendar.JANUARY+M31[i]+1)%12);
-		  } else {
-		    assertEquals(c.get(Calendar.MONTH), 
-		    	 Calendar.JANUARY+M31[i]);
-		  }
-		} else {
-		  assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
-		}
-	      } else {
-		// allow discrepancies only if we are switching from std to dls time
-		if(c.get(Calendar.HOUR_OF_DAY) != h) {
-		  c.add(Calendar.HOUR_OF_DAY, +1);
-		  boolean dltState2 = c.getTimeZone().inDaylightTime(c.getTime());
-		  if(dltState1 == dltState2) {
-		    fail("No switch"); 
-		  } 
-		}		
-		assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
-		assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
-	      }
-	    }
-	  }
-	}
+    for(int h = 0; h < 23; h++) {
+      for(int m = 0; m < M.length; m++) {
+        for(int s = 0; s < S.length; s++) {
+          c.clear();
+          c.set(Calendar.YEAR, 20);
+          c.set(Calendar.MONTH, Calendar.JANUARY + M31[i]);
+          c.set(Calendar.DAY_OF_MONTH, d);
+          c.set(Calendar.HOUR_OF_DAY, h);
+          c.set(Calendar.MINUTE, M[m]);
+          c.set(Calendar.SECOND, S[s]);
+          c.set(Calendar.MILLISECOND, 88);
+          c.add(Calendar.MILLISECOND, 1);
+
+          boolean dltState0 = c.getTimeZone().inDaylightTime(c.getTime());
+
+          c.setTime(rc.getNextCheckDate(c.getTime()));
+          c.add(Calendar.MILLISECOND, 0);
+          boolean dltState1 = c.getTimeZone().inDaylightTime(c.getTime());
+
+          assertEquals(c.get(Calendar.MILLISECOND), 0);
+          assertEquals(c.get(Calendar.SECOND), 0);
+          assertEquals(c.get(Calendar.MINUTE), (M[m]+1)%60);
+
+          if(M[m] == 59) {
+        if(dltState0 == dltState1) {
+          assertEquals(c.get(Calendar.HOUR_OF_DAY), (h+1)%24);
+        }
+        if(h == 23) {
+          assertEquals(c.get(Calendar.DAY_OF_MONTH), (d+1)%32);
+          if(d == 31) {
+              assertEquals(c.get(Calendar.MONTH),
+                 (Calendar.JANUARY+M31[i]+1)%12);
+          } else {
+            assertEquals(c.get(Calendar.MONTH),
+                 Calendar.JANUARY+M31[i]);
+          }
+        } else {
+          assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
+        }
+          } else {
+        // allow discrepancies only if we are switching from std to dls time
+        if(c.get(Calendar.HOUR_OF_DAY) != h) {
+          c.add(Calendar.HOUR_OF_DAY, +1);
+          boolean dltState2 = c.getTimeZone().inDaylightTime(c.getTime());
+          if(dltState1 == dltState2) {
+            fail("No switch");
+          }
+        }
+        assertEquals(c.get(Calendar.DAY_OF_MONTH), d);
+        assertEquals(c.get(Calendar.MONTH), Calendar.JANUARY+M31[i]);
+          }
+        }
+      }
+    }
       }
     }
   }
 
 
-  
-  public 
-  static
-  Test suite() {
-    TestSuite suite = new TestSuite();
-    suite.addTest(new DRFATestCase("testComputeCheckPeriod"));
-    suite.addTest(new DRFATestCase("testRC1"));
-    suite.addTest(new DRFATestCase("testRC2"));
-    suite.addTest(new DRFATestCase("testRC3"));
-    return suite;
-  }
-  
+    /**
+     * Common test code for 3 parameter constructor.
+     *
+     * @throws IOException if IOException during test.
+     */
+   public void test3Param(final String datePattern,
+                          final String filename) throws IOException {
+       Layout layout = new SimpleLayout();
+       DailyRollingFileAppender appender =
+               new DailyRollingFileAppender(layout, filename, datePattern);
+       assertEquals(datePattern, appender.getDatePattern());
+       Logger root = Logger.getRootLogger();
+       root.addAppender(appender);
+       root.info("Hello, World");
+       assertTrue(new File(filename).exists());
+    }
+
+    /**
+     * Creates an appender with an unrecognized top-of-year pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testTopOfYear() throws IOException {
+        try {
+            test3Param("'.'yyyy", "output/drfa_topOfYear.log");
+            fail("Expected illegal state exception.");
+        } catch(IllegalStateException ex) {
+            assertNotNull(ex);
+        }
+    }
+
+    /**
+     * Creates an appender with a top-of-month pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testTopOfMonth() throws IOException {
+        test3Param("'.'yyyy-MM", "output/drfa_topOfMonth.log");
+    }
+
+
+    /**
+     * Creates an appender with a top-of-week pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testTopOfWeek() throws IOException {
+        test3Param("'.'yyyy-w", "output/drfa_topOfWeek.log");
+    }
+
+    /**
+     * Creates an appender with a top-of-day pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testTopOfDay() throws IOException {
+        test3Param("'.'yyyy-MM-dd", "output/drfa_topOfDay.log");
+    }
+
+
+    /**
+     * Creates an appender with a half day pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testHalfDay() throws IOException {
+        test3Param("'.'yyyy-MM-dd-a", "output/drfa_halfDay.log");
+    }
+
+    /**
+     * Creates an appender with a top-of-hour pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testTopOfHour() throws IOException {
+        test3Param("'.'yyyy-MM-dd-HH", "output/drfa_topOfHour.log");
+    }
+
+    /**
+     * Creates an appender with a top-of-day pattern.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testTopOfMinute() throws IOException {
+        test3Param("'.'yyyy-MM-dd-HH-mm", "output/drfa_topOfMinute.log");
+    }
+
+    /**
+     * Attempts to rollOver with no date pattern set.
+     *
+     * @throws IOException if IOException during test.
+     */
+    public void testRolloverNoPattern() throws IOException {
+        Layout layout = new SimpleLayout();
+        DailyRollingFileAppender appender =
+                new DailyRollingFileAppender(layout, "output/drfa_nopattern.log", null);
+
+        VectorErrorHandler errorHandler = new VectorErrorHandler();
+        appender.setErrorHandler(errorHandler);
+        appender.rollOver();
+        assertEquals(1, errorHandler.size());
+        assertEquals("Missing DatePattern option in rollOver().",
+                errorHandler.getMessage(0));
+    }
+
+    /**
+     * Tests rollOver with a minute periodicity.
+     *
+     * @throws IOException
+     * @throws InterruptedException
+     */
+    public void testMinuteRollover() throws IOException, InterruptedException {
+        Layout layout = new SimpleLayout();
+        String filename = "output/drfa_minuteRollover.log";
+        String pattern = "'.'yyyy-MM-dd-HH-mm";
+
+        DailyRollingFileAppender appender =
+                new DailyRollingFileAppender(layout,
+                        filename,
+                        pattern);
+        Logger root = Logger.getRootLogger();
+        root.addAppender(appender);
+        File firstFile =
+                new File(filename + new SimpleDateFormat(pattern).format(new Date()));
+        root.info("Hello, World");
+        //
+        //   create a file by that name so it has to be deleted
+        //       on rollover
+        firstFile.createNewFile();
+        assertTrue(firstFile.exists());
+        assertEquals(0, firstFile.length());
+
+        Calendar cal = Calendar.getInstance();
+        long now = cal.getTimeInMillis();
+        cal.set(Calendar.SECOND, 0);
+        cal.set(Calendar.MILLISECOND, 1);
+        cal.add(Calendar.MINUTE, 1);
+        long until = cal.getTimeInMillis();
+        Thread.sleep(until - now);
+        root.info("Hello, World");
+        assertTrue(firstFile.exists());
+        assertTrue(firstFile.length() > 0);
+
+    }
+
 }

Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/RFATestCase.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/RFATestCase.java?rev=398081&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/RFATestCase.java (added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/RFATestCase.java Fri
Apr 28 21:12:54 2006
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ *  Test of RollingFileAppender.
+ *
+ * @author Curt Arnold
+ */
+public class RFATestCase extends TestCase {
+
+  public RFATestCase(String name) {
+    super(name);
+  }
+
+  public void tearDown() {
+      LogManager.resetConfiguration();
+  }
+
+    /**
+     * Test basic rolling functionality using property file configuration.
+     */
+    public void test1() throws Exception {
+     Logger logger = Logger.getLogger(RFATestCase.class);
+      PropertyConfigurator.configure("input/RFA1.properties");
+
+      // Write exactly 10 bytes with each log
+      for (int i = 0; i < 25; i++) {
+        if (i < 10) {
+          logger.debug("Hello---" + i);
+        } else if (i < 100) {
+          logger.debug("Hello--" + i);
+        }
+      }
+
+      assertTrue(new File("output/RFA-test1.log").exists());
+      assertTrue(new File("output/RFA-test1.log.1").exists());
+    }
+
+    /**
+     * Test basic rolling functionality using API configuration.
+     */
+    public void test2() throws Exception {
+      Logger logger = Logger.getLogger(RFATestCase.class);
+      Logger root = Logger.getRootLogger();
+      PatternLayout layout = new PatternLayout("%m\n");
+      org.apache.log4j.RollingFileAppender rfa =
+        new org.apache.log4j.RollingFileAppender();
+      rfa.setName("ROLLING");
+      rfa.setLayout(layout);
+      rfa.setAppend(false);
+      rfa.setMaxBackupIndex(3);
+      rfa.setMaximumFileSize(100);
+      rfa.setFile("output/RFA-test2.log");
+      rfa.activateOptions();
+      root.addAppender(rfa);
+
+      // Write exactly 10 bytes with each log
+      for (int i = 0; i < 55; i++) {
+        if (i < 10) {
+          logger.debug("Hello---" + i);
+        } else if (i < 100) {
+          logger.debug("Hello--" + i);
+        }
+      }
+
+      assertTrue(new File("output/RFA-test2.log").exists());
+      assertTrue(new File("output/RFA-test2.log.1").exists());
+      assertTrue(new File("output/RFA-test2.log.2").exists());
+      assertTrue(new File("output/RFA-test2.log.3").exists());
+      assertFalse(new File("output/RFA-test2.log.4").exists());
+    }
+
+    /**
+     * Tests 2 parameter constructor.
+     * @throws IOException if IOException during test.
+     */
+    public void test2ParamConstructor() throws IOException {
+        SimpleLayout layout = new SimpleLayout();
+        RollingFileAppender appender =
+                new RollingFileAppender(layout,"output/rfa_2param.log");
+        assertEquals(1, appender.getMaxBackupIndex());
+        assertEquals(10*1024*1024, appender.getMaximumFileSize());
+    }
+    /**
+     * Tests 3 parameter constructor.
+     * @throws IOException if IOException during test.
+     */
+    public void test3ParamConstructor() throws IOException {
+        SimpleLayout layout = new SimpleLayout();
+        RollingFileAppender appender =
+                new RollingFileAppender(layout,"output/rfa_3param.log", false);
+        assertEquals(1, appender.getMaxBackupIndex());
+    }
+
+}

Added: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/varia/ERFATestCase.java
URL: http://svn.apache.org/viewcvs/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/varia/ERFATestCase.java?rev=398081&view=auto
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/varia/ERFATestCase.java
(added)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/varia/ERFATestCase.java
Fri Apr 28 21:12:54 2006
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.varia;
+import junit.framework.TestCase;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.RFATestCase;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.net.Socket;
+
+/**
+ *  Test of ExternallyRolledFileAppender.
+ *
+ * @author Curt Arnold
+ */
+public class ERFATestCase extends TestCase {
+
+    /**
+     * Create new instance of test.
+     * @param name test name.
+     */
+  public ERFATestCase(final String name) {
+    super(name);
+  }
+
+    /**
+     * Reset configuration after test.
+     */
+  public void tearDown() {
+      LogManager.resetConfiguration();
+  }
+
+    /**
+     * Test ExternallyRolledFileAppender constructor.
+     */
+  public void testConstructor() {
+      ExternallyRolledFileAppender appender =
+              new ExternallyRolledFileAppender();
+      assertEquals(0, appender.getPort());
+  }
+
+    /**
+     * Send a message to the ERFA.
+     * @param port port number.
+     * @param msg message, may not be null.
+     * @param expectedResponse expected response, may not be null.
+     * @throws IOException thrown on IO error.
+     */
+  void sendMessage(int port, final String msg, final String expectedResponse) throws IOException
{
+      Socket socket = new Socket((String) null, port);
+      DataInputStream reader = new DataInputStream(socket.getInputStream());
+      DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
+      writer.writeUTF(msg);
+      String response = reader.readUTF();
+      assertEquals(expectedResponse, response);
+      reader.close();
+      writer.close();
+      socket.close();
+  }
+
+    /**
+     * Test externally triggered rollover.
+     * @throws IOException thrown on IO error.
+     */
+  public void testRollover() throws IOException {
+      ExternallyRolledFileAppender erfa =
+              new ExternallyRolledFileAppender();
+
+      int port = 5500;
+
+      Logger logger = Logger.getLogger(RFATestCase.class);
+      Logger root = Logger.getRootLogger();
+      PatternLayout layout = new PatternLayout("%m\n");
+      erfa.setLayout(layout);
+      erfa.setAppend(false);
+      erfa.setMaxBackupIndex(2);
+      erfa.setPort(port);
+      erfa.setFile("output/ERFA-test2.log");
+      try {
+        erfa.activateOptions();
+      } catch(SecurityException ex) {
+          return;
+      }
+      root.addAppender(erfa);
+
+
+      // Write exactly 10 bytes with each log
+      for (int i = 0; i < 55; i++) {
+        if (i < 10) {
+          logger.debug("Hello---" + i);
+        } else if (i < 100) {
+          logger.debug("Hello--" + i);
+        }
+        if ((i % 10) == 9) {
+            try {
+                sendMessage(port, "RollOver", "OK");
+            } catch(SecurityException ex) {
+                return;
+            }
+        }
+      }
+
+      try {
+        sendMessage(port,
+              "That's all folks.",
+              "Expecting [RollOver] string.");
+      } catch(SecurityException ex) {
+          return;
+      }
+
+
+      assertTrue(new File("output/ERFA-test2.log").exists());
+      assertTrue(new File("output/ERFA-test2.log.1").exists());
+      assertTrue(new File("output/ERFA-test2.log.2").exists());
+      assertFalse(new File("output/ERFA-test2.log.3").exists());
+  }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message