1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.File;
19  import java.text.SimpleDateFormat;
20  import java.util.Calendar;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.Context;
26  import ch.qos.logback.core.ContextBase;
27  import ch.qos.logback.core.encoder.EchoEncoder;
28  import ch.qos.logback.core.encoder.Encoder;
29  import ch.qos.logback.core.util.Compare;
30  import ch.qos.logback.core.util.CoreTestConstants;
31  
32  /**
33   * 
34   * This test case aims to unit test/reproduce problems encountered while
35   * renaming the log file under windows.
36   * 
37   * @author Ceki
38   * 
39   */
40  public class RenamingTest {
41  
42    Encoder<Object> encoder;
43    Context context = new ContextBase();
44  
45    @Before
46    public void setUp() throws Exception {
47      encoder = new EchoEncoder<Object>();
48  
49      File target = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "test.log");
50      target.mkdirs();
51      target.delete();
52    }
53  
54    @Test
55    public void testRename() throws Exception {
56  
57      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
58      rfa.setEncoder(encoder);
59      rfa.setContext(context);
60  
61      // rollover by the second
62      String datePattern = "yyyy-MM-dd_HH_mm_ss";
63      SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
64      String[] filenames = new String[2];
65  
66      TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
67      tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "test-%d{"
68          + datePattern + "}");
69      tbrp.setContext(context);
70      tbrp.setParent(rfa);
71      tbrp.start();
72  
73      rfa.setRollingPolicy(tbrp);
74      rfa.start();
75  
76  
77      Calendar cal0 = Calendar.getInstance();
78      rfa.doAppend("Hello 0");
79      
80      DelayerUtil.delayUntilNextSecond(50);
81      
82      Calendar cal1 = Calendar.getInstance();
83      rfa.doAppend("Hello 1");
84  
85      filenames[0] = CoreTestConstants.OUTPUT_DIR_PREFIX + "test-"
86          + sdf.format(cal0.getTime());
87      filenames[1] = CoreTestConstants.OUTPUT_DIR_PREFIX + "test-"
88          + sdf.format(cal1.getTime());
89      
90    
91      for (int i = 0; i < filenames.length; i++) {
92        assertTrue(Compare.compare(filenames[i], CoreTestConstants.TEST_DIR_PREFIX
93            + "witness/rolling/renaming." + i));
94      }
95    }
96  }