View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j;
    19
    20  import junit.framework.TestCase;
    21
    22  import java.io.File;
    23  import java.io.FileWriter;
    24  import java.io.IOException;
    25
    26  /***
    27   *  Test of RollingFileAppender.
    28   *
    29   * @author Curt Arnold
    30   */
    31  public class RFATestCase extends TestCase {
    32
    33    public RFATestCase(String name) {
    34      super(name);
    35    }
    36
    37    public void tearDown() {
    38        LogManager.resetConfiguration();
    39    }
    40
    41      /***
    42       * Test basic rolling functionality using property file configuration.
    43       */
    44      public void test1() throws Exception {
    45       Logger logger = Logger.getLogger(RFATestCase.class);
    46        PropertyConfigurator.configure("input/RFA1.properties");
    47
    48        // Write exactly 10 bytes with each log
    49        for (int i = 0; i < 25; i++) {
    50          if (i < 10) {
    51            logger.debug("Hello---" + i);
    52          } else if (i < 100) {
    53            logger.debug("Hello--" + i);
    54          }
    55        }
    56
    57        assertTrue(new File("output/RFA-test1.log").exists());
    58        assertTrue(new File("output/RFA-test1.log.1").exists());
    59      }
    60
    61      /***
    62       * Test basic rolling functionality using API configuration.
    63       */
    64      public void test2() throws Exception {
    65        Logger logger = Logger.getLogger(RFATestCase.class);
    66        Logger root = Logger.getRootLogger();
    67        PatternLayout layout = new PatternLayout("%m\n");
    68        org.apache.log4j.RollingFileAppender rfa =
    69          new org.apache.log4j.RollingFileAppender();
    70        rfa.setName("ROLLING");
    71        rfa.setLayout(layout);
    72        rfa.setAppend(false);
    73        rfa.setMaxBackupIndex(3);
    74        rfa.setMaximumFileSize(100);
    75        rfa.setFile("output/RFA-test2.log");
    76        rfa.activateOptions();
    77        root.addAppender(rfa);
    78
    79        // Write exactly 10 bytes with each log
    80        for (int i = 0; i < 55; i++) {
    81          if (i < 10) {
    82            logger.debug("Hello---" + i);
    83          } else if (i < 100) {
    84            logger.debug("Hello--" + i);
    85          }
    86        }
    87
    88        assertTrue(new File("output/RFA-test2.log").exists());
    89        assertTrue(new File("output/RFA-test2.log.1").exists());
    90        assertTrue(new File("output/RFA-test2.log.2").exists());
    91        assertTrue(new File("output/RFA-test2.log.3").exists());
    92        assertFalse(new File("output/RFA-test2.log.4").exists());
    93      }
    94
    95      /***
    96       * Tests 2 parameter constructor.
    97       * @throws IOException if IOException during test.
    98       */
    99      public void test2ParamConstructor() throws IOException {
    100         SimpleLayout layout = new SimpleLayout();
    101         RollingFileAppender appender =
    102                 new RollingFileAppender(layout,"output/rfa_2param.log");
    103         assertEquals(1, appender.getMaxBackupIndex());
    104         assertEquals(10*1024*1024, appender.getMaximumFileSize());
    105     }
    106     /***
    107      * Tests 3 parameter constructor.
    108      * @throws IOException if IOException during test.
    109      */
    110     public void test3ParamConstructor() throws IOException {
    111         SimpleLayout layout = new SimpleLayout();
    112         RollingFileAppender appender =
    113                 new RollingFileAppender(layout,"output/rfa_3param.log", false);
    114         assertEquals(1, appender.getMaxBackupIndex());
    115     }
    116
    117     /***
    118      * Test locking of .1 file.
    119      */
    120     public void testLockDotOne() throws Exception {
    121       Logger logger = Logger.getLogger(RFATestCase.class);
    122       Logger root = Logger.getRootLogger();
    123       PatternLayout layout = new PatternLayout("%m\n");
    124       org.apache.log4j.RollingFileAppender rfa =
    125         new org.apache.log4j.RollingFileAppender();
    126       rfa.setName("ROLLING");
    127       rfa.setLayout(layout);
    128       rfa.setAppend(false);
    129       rfa.setMaxBackupIndex(10);
    130       rfa.setMaximumFileSize(100);
    131       rfa.setFile("output/RFA-dot1.log");
    132       rfa.activateOptions();
    133       root.addAppender(rfa);
    134
    135       new File("output/RFA-dot1.log.2").delete();
    136
    137       FileWriter dot1 = new FileWriter("output/RFA-dot1.log.1");
    138       dot1.write("Locked file");
    139       FileWriter dot5 = new FileWriter("output/RFA-dot1.log.5");
    140       dot5.write("Unlocked file");
    141       dot5.close();
    142
    143       // Write exactly 10 bytes with each log
    144       for (int i = 0; i < 15; i++) {
    145         if (i < 10) {
    146           logger.debug("Hello---" + i);
    147         } else if (i < 100) {
    148           logger.debug("Hello--" + i);
    149         }
    150       }
    151       dot1.close();
    152
    153       for (int i = 15; i < 25; i++) {
    154             logger.debug("Hello--" + i);
    155       }
    156       rfa.close();
    157
    158
    159       assertTrue(new File("output/RFA-dot1.log.7").exists());
    160       //
    161       //     if .2 is the locked file then
    162       //       renaming wasn't successful until the file was closed
    163       if (new File("output/RFA-dot1.log.2").length() < 15) {
    164           assertEquals(50, new File("output/RFA-dot1.log").length());
    165           assertEquals(200, new File("output/RFA-dot1.log.1").length());
    166       } else {
    167           assertTrue(new File("output/RFA-dot1.log").exists());
    168           assertTrue(new File("output/RFA-dot1.log.1").exists());
    169           assertTrue(new File("output/RFA-dot1.log.2").exists());
    170           assertTrue(new File("output/RFA-dot1.log.3").exists());
    171           assertFalse(new File("output/RFA-dot1.log.4").exists());
    172       }
    173     }
    174
    175
    176     /***
    177      * Test locking of .3 file.
    178      */
    179     public void testLockDotThree() throws Exception {
    180       Logger logger = Logger.getLogger(RFATestCase.class);
    181       Logger root = Logger.getRootLogger();
    182       PatternLayout layout = new PatternLayout("%m\n");
    183       org.apache.log4j.RollingFileAppender rfa =
    184         new org.apache.log4j.RollingFileAppender();
    185       rfa.setName("ROLLING");
    186       rfa.setLayout(layout);
    187       rfa.setAppend(false);
    188       rfa.setMaxBackupIndex(10);
    189       rfa.setMaximumFileSize(100);
    190       rfa.setFile("output/RFA-dot3.log");
    191       rfa.activateOptions();
    192       root.addAppender(rfa);
    193
    194       new File("output/RFA-dot3.log.1").delete();
    195       new File("output/RFA-dot3.log.2").delete();
    196       new File("output/RFA-dot3.log.4").delete();
    197
    198       FileWriter dot3 = new FileWriter("output/RFA-dot3.log.3");
    199       dot3.write("Locked file");
    200       FileWriter dot5 = new FileWriter("output/RFA-dot3.log.5");
    201       dot5.write("Unlocked file");
    202       dot5.close();
    203
    204       // Write exactly 10 bytes with each log
    205       for (int i = 0; i < 15; i++) {
    206         if (i < 10) {
    207           logger.debug("Hello---" + i);
    208         } else if (i < 100) {
    209           logger.debug("Hello--" + i);
    210         }
    211       }
    212       dot3.close();
    213
    214       for (int i = 15; i < 35; i++) {
    215           logger.debug("Hello--" + i);
    216       }
    217       rfa.close();
    218
    219       assertTrue(new File("output/RFA-dot3.log.8").exists());
    220       //
    221       //     if .3 is the locked file then
    222       //       renaming wasn't successful until file was closed
    223       if (new File("output/RFA-dot3.log.5").exists()) {
    224           assertEquals(50, new File("output/RFA-dot3.log").length());
    225           assertEquals(100, new File("output/RFA-dot3.log.1").length());
    226           assertEquals(200, new File("output/RFA-dot3.log.2").length());
    227       } else {
    228           assertTrue(new File("output/RFA-dot3.log").exists());
    229           assertTrue(new File("output/RFA-dot3.log.1").exists());
    230           assertTrue(new File("output/RFA-dot3.log.2").exists());
    231           assertTrue(new File("output/RFA-dot3.log.3").exists());
    232           assertFalse(new File("output/RFA-dot3.log.4").exists());
    233       }
    234     }
    235
    236
    237 }