You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2003/05/19 14:19:37 UTC

cvs commit: jakarta-log4j/tests/input compress1.txt

ceki        2003/05/19 05:19:37

  Modified:    src/java/org/apache/log4j/rolling RollingFileAppender.java
                        SlidingWindowRollingPolicy.java
               tests/src/java/org/apache/log4j/rolling
                        SizeBasedRollingTestCase.java
  Added:       tests/src/java/org/apache/log4j/rolling/helpers
                        CompressTestCase.java
               src/java/org/apache/log4j/rolling/helpers Compress.java
               tests/input compress1.txt
  Log:
  
  Adding compression code and more tests
  
  Revision  Changes    Path
  1.1                  jakarta-log4j/tests/src/java/org/apache/log4j/rolling/helpers/CompressTestCase.java
  
  Index: CompressTestCase.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  package org.apache.log4j.rolling.helpers;
  
  import org.apache.log4j.BasicConfigurator;
  import org.apache.log4j.util.Compare;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  /**
   * @author Ceki
  
   */
  public class CompressTestCase extends TestCase {
  
    /**
     * Constructor for CompressTestCase.
     * @param arg0
     */
    public CompressTestCase(String arg0) {
      super(arg0);
    }
  
    public void test1() throws Exception {
      
      Compress.GZCompress("input/compress1.txt", "output/compress1.txt.gz");
      
      assertTrue(Compare.compare("output/compress1.txt.gz",
             "witness/compress1.txt.gz"));
      
    }
    
    public static Test suite() {
        TestSuite suite = new TestSuite();
  
        suite.addTest(new CompressTestCase("test1"));
  
  
        return suite;
      }
  }
  
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/rolling/helpers/Compress.java
  
  Index: Compress.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.rolling.helpers;
  
  import org.apache.log4j.Logger;
  
  import java.io.*;
  import java.util.zip.GZIPOutputStream;
  
  
  /**
   * @author Ceki
   *
   */
  public class Compress {
    static final Logger logger = Logger.getLogger(Compress.class);
  
    public static void ZIPcompress(String file2zip, String zippedFile) {
    }
  
    public static void GZCompress(String nameOfFile2gz, String nameOfgzedFile) {
      File file2gz = new File(nameOfFile2gz);
      File gzedFile = new File(nameOfgzedFile);
  
      if (!file2gz.exists()) {
        logger.warn(
          "The file to compress named [" + nameOfFile2gz + "] does not exist.");
  
        return;
      }
  
      if (gzedFile.exists()) {
        logger.warn(
          "The target compressed file named [" + nameOfFile2gz
          + "] exist already.");
  
        return;
      }
  
      try {
        FileOutputStream fos = new FileOutputStream(nameOfgzedFile);
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        FileInputStream fis = new FileInputStream(nameOfFile2gz);
        byte[] inbuf = new byte[8102];
        int n;
  
        while ((n = fis.read(inbuf)) != -1) {
          gzos.write(inbuf, 0, n);
        }
        fis.close();
        gzos.close();
      } catch (Exception e) {
        logger.error(
          "Error occured while compressing [" + nameOfFile2gz + "] into ["
          + nameOfgzedFile + "].", e);
      }
    }
  }
  
  
  
  1.9       +1 -1      jakarta-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java
  
  Index: RollingFileAppender.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/RollingFileAppender.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- RollingFileAppender.java	17 May 2003 17:33:14 -0000	1.8
  +++ RollingFileAppender.java	19 May 2003 12:19:37 -0000	1.9
  @@ -90,7 +90,7 @@
   
       if (rollingPolicy != null) {
         rollingPolicy.activateOptions();
  -
  +      System.out.println("Actibe log file name"+rollingPolicy.getActiveLogFileName());
         setFile(rollingPolicy.getActiveLogFileName());
         activeFile = new File(rollingPolicy.getActiveLogFileName());
         super.activateOptions();
  
  
  
  1.6       +15 -4     jakarta-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java
  
  Index: SlidingWindowRollingPolicy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/rolling/SlidingWindowRollingPolicy.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SlidingWindowRollingPolicy.java	17 May 2003 14:30:03 -0000	1.5
  +++ SlidingWindowRollingPolicy.java	19 May 2003 12:19:37 -0000	1.6
  @@ -65,6 +65,7 @@
    * */
   public class SlidingWindowRollingPolicy implements RollingPolicy,
     OptionHandler {
  +    
     static Logger logger = Logger.getLogger(SlidingWindowRollingPolicy.class);
     int maxIndex;
     int minIndex;
  @@ -89,7 +90,7 @@
         }
   
         // Map {(maxIndex - 1), ..., minIndex} to {maxIndex, ..., minIndex+1}
  -      for (int i = maxIndex - 1; i > minIndex; i--) {
  +      for (int i = maxIndex - 1; i >= minIndex; i--) {
           Util.rename(
             fileNamePattern.convert(i), fileNamePattern.convert(i + 1));
         }
  @@ -100,9 +101,9 @@
           Util.rename(activeFileName, fileNamePattern.convert(minIndex));
         } else {
           // TODO: compress the currently active file (minIndex) into minIndex+1
  -        Util.rename(
  -          fileNamePattern.convert(minIndex),
  -          fileNamePattern.convert(minIndex + 1));
  +        //Util.rename(
  +          //fileNamePattern.convert(minIndex),
  +          //fileNamePattern.convert(minIndex + 1));
         }
       }
     }
  @@ -157,4 +158,14 @@
     public void setMinIndex(int minIndex) {
       this.minIndex = minIndex;
     }
  +
  +
  +  public String getActiveFileName() {
  +    return activeFileName;
  +  }
  +
  +  public void setActiveFileName(String afn) {
  +    activeFileName = afn;
  +  }
  +
   }
  
  
  
  1.6       +68 -20    jakarta-log4j/tests/src/java/org/apache/log4j/rolling/SizeBasedRollingTestCase.java
  
  Index: SizeBasedRollingTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/tests/src/java/org/apache/log4j/rolling/SizeBasedRollingTestCase.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SizeBasedRollingTestCase.java	17 May 2003 18:57:56 -0000	1.5
  +++ SizeBasedRollingTestCase.java	19 May 2003 12:19:37 -0000	1.6
  @@ -54,10 +54,12 @@
   import junit.framework.TestSuite;
   
   import org.apache.log4j.ConsoleAppender;
  +import org.apache.log4j.LogManager;
   import org.apache.log4j.Logger;
   import org.apache.log4j.PatternLayout;
   import org.apache.log4j.util.Compare;
   
  +
   /**
    *
    * @author Ceki G&uuml;lc&uuml;
  @@ -74,17 +76,19 @@
     }
   
     public void tearDown() {
  +    LogManager.shutdown();
     }
   
     public void test1() throws Exception {
       Logger root = Logger.getRootLogger();
       root.addAppender(new ConsoleAppender(new PatternLayout()));
  -    
  +
       // We purposefully use the \n as the line separator. 
       // This makes the regression test system indepent.
       PatternLayout layout = new PatternLayout("%m\n");
       RollingFileAppender rfa = new RollingFileAppender();
       rfa.setLayout(layout);
  +
       SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
       SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
       sbtp.setMaxFileSize(100);
  @@ -93,9 +97,10 @@
       rfa.setTriggeringPolicy(sbtp);
       rfa.activateOptions();
       root.addAppender(rfa);
  -    
  +
       // Write exactly 10 bytes with each log
       for (int i = 0; i < 22; i++) {
  +      Thread.sleep(1000);
         if (i < 10) {
           logger.debug("Hello---" + i);
         } else if (i < 100) {
  @@ -106,32 +111,75 @@
       }
   
       // The File.length() method is not accurate under Windows    
  -    if(!isWindows()) {
  -	
  -      assertTrue(Compare.compare("output/sizeBased-test1.1", 
  -				 "witness/sizeBased-test1.1"));
  -      assertTrue(Compare.compare("output/sizeBased-test1.2", 
  -				 "witness/sizeBased-test1.2"));
  -      assertTrue(Compare.compare("output/sizeBased-test1.3", 
  -				 "witness/sizeBased-test1.3"));}
  +    if (!isWindows()) {
  +      assertTrue(
  +        Compare.compare(
  +          "output/sizeBased-test1.1", "witness/sizeBased-test1.1"));
  +      assertTrue(
  +        Compare.compare(
  +          "output/sizeBased-test1.2", "witness/sizeBased-test1.2"));
  +      assertTrue(
  +        Compare.compare(
  +          "output/sizeBased-test1.3", "witness/sizeBased-test1.3"));
  +    }
  +  }
  +
  +  public void test2() throws Exception {
  +    Logger root = Logger.getRootLogger();
  +    root.addAppender(new ConsoleAppender(new PatternLayout()));
  +
  +    PatternLayout layout = new PatternLayout("%m\n");
  +    RollingFileAppender rfa = new RollingFileAppender();
  +    rfa.setLayout(layout);
  +
  +    SlidingWindowRollingPolicy swrp = new SlidingWindowRollingPolicy();
  +    SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
  +
  +    sbtp.setMaxFileSize(100);
  +    swrp.setActiveFileName("output/sizeBased-test2");
  +    swrp.setFileNamePattern("output/sizeBased-test2.%i");
  +    rfa.setRollingPolicy(swrp);
  +    rfa.setTriggeringPolicy(sbtp);
  +    rfa.activateOptions();
  +    root.addAppender(rfa);
  +
  +    // Write exactly 10 bytes with each log
  +    for (int i = 0; i < 25; i++) {
  +      Thread.sleep(1000);
  +      if (i < 10) {
  +        logger.debug("Hello   " + i);
  +      } else if (i < 100) {
  +        logger.debug("Hello  " + i);
  +      }
  +    }
  +
  +    // The File.length() method is not accurate under Windows    
  +
  +    /**
  +     *
  +     if(!isWindows()) {
  +
  +      assertTrue(Compare.compare("output/sizeBased-test2.1",
  +         "witness/sizeBased-test1.1"));
  +      assertTrue(Compare.compare("output/sizeBased-test2.2",
  +         "witness/sizeBased-test1.2"));
  +      assertTrue(Compare.compare("output/sizeBased-test2.3",
  +         "witness/sizeBased-test1.3"));
  +         }
  +
  +         */
     }
   
     boolean isWindows() {
       return System.getProperty("os.name").indexOf("Windows") != -1;
     }
   
  -
     public static Test suite() {
       TestSuite suite = new TestSuite();
  -    
  -    if(System.getProperty("os.name").indexOf("Windows") != -1) {
  -      // The File.length() method is not accurate under Windows 
  -      System.out.println("This test cannot be run under Windows.");
  -      return suite;
  -    } else {
  -      suite.addTest(new SizeBasedRollingTestCase("test1"));
  -    }
  +
  +    suite.addTest(new SizeBasedRollingTestCase("test1"));
  +    suite.addTest(new SizeBasedRollingTestCase("test2"));
  +
       return suite;
     }
  -
   }
  
  
  
  1.1                  jakarta-log4j/tests/input/compress1.txt
  
  Index: compress1.txt
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    apache@apache.org.
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  

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