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.rolling;
19  
20  import junit.framework.TestCase;
21  import org.apache.log4j.ConsoleAppender;
22  import org.apache.log4j.Logger;
23  import org.apache.log4j.PatternLayout;
24  import org.apache.log4j.util.Compare;
25  
26  import java.io.File;
27  import java.text.SimpleDateFormat;
28  import java.util.Calendar;
29  
30  
31  /***
32   * 
33   * This test case aims to unit test/reproduce problems encountered while
34   * renaming the log file under windows.
35   * 
36   * @author Ceki
37   *
38   */
39  public class RenamingTest extends TestCase {
40    Logger logger = Logger.getLogger(RenamingTest.class);
41    
42    Logger root;
43    PatternLayout layout;
44    
45    public RenamingTest(String arg0) {
46      super(arg0);
47    }
48  
49    protected void setUp() throws Exception {
50      super.setUp();
51      root = Logger.getRootLogger();
52      layout = new PatternLayout("%c{1} - %m%n");
53      root.addAppender(new ConsoleAppender(new PatternLayout()));
54      
55    }
56  
57    protected void tearDown() throws Exception {
58      super.tearDown();
59    }
60  
61    public void testRename() throws Exception {
62    
63      RollingFileAppender rfa = new RollingFileAppender();
64      rfa.setLayout(layout);
65      rfa.setAppend(false);
66  
67      // rollover by the second
68      String datePattern = "yyyy-MM-dd_HH_mm_ss";
69      SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
70  
71      TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
72      tbrp.setFileNamePattern("test-%d{" + datePattern + "}");
73      rfa.setFile("test.log");
74      tbrp.activateOptions();
75      rfa.setRollingPolicy(tbrp);
76      rfa.activateOptions();
77  
78      Calendar cal = Calendar.getInstance();
79  
80      root.addAppender(rfa);
81      logger.debug("Hello   " + 0);
82      Thread.sleep(5000);
83      logger.debug("Hello   " + 1);
84      
85      String rolledFile = "test-" + sdf.format(cal.getTime());
86  
87      //
88      //   if the rolled file exists
89      //       either the test wasn't run from the Ant script
90      //            which opens test.log in another process or
91      //              the test is running on a platform that allows open files to be renamed
92      if (new File(rolledFile).exists()) {
93          assertTrue(Compare.compare(RenamingTest.class,
94                  rolledFile, "witness/rolling/renaming.0"));
95          assertTrue(Compare.compare(RenamingTest.class,
96                  "test.log", "witness/rolling/renaming.1"));
97      } else {
98          //
99          //   otherwise the rollover should have been blocked
100         //
101         assertTrue(Compare.compare(RenamingTest.class,
102                 "test.log", "witness/rolling/renaming.2"));
103     }
104   }
105 }