1   package ch.qos.logback.core.rolling;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.io.File;
6   import java.io.FilenameFilter;
7   import java.util.concurrent.TimeUnit;
8   
9   import org.junit.After;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  import ch.qos.logback.core.Context;
14  import ch.qos.logback.core.ContextBase;
15  import ch.qos.logback.core.layout.EchoLayout;
16  import ch.qos.logback.core.util.Constants;
17  
18  public class TimeBasedRollingWithCleanTest {
19  
20    Context context = new ContextBase();
21    EchoLayout<Object> layout = new EchoLayout<Object>();
22  
23    static final String MONTHLY_DATE_PATTERN = "yyyy-MM";
24    static final String DAILY_DATE_PATTERN = "yyyy-MM-dd";
25  
26    static final long MILLIS_IN_MINUTE = 60 * 1000;
27    static final long MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
28    static final long MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR;
29    static final long MILLIS_IN_MONTH = 30 * MILLIS_IN_DAY;
30  
31    @Before
32    public void setUp() throws Exception {
33      context.setName("test");
34  
35      // remove all files containing the string 'clean'
36      File dir = new File(Constants.OUTPUT_DIR_PREFIX);
37      if (dir.isDirectory()) {
38        File[] toDelete = dir.listFiles(new FilenameFilter() {
39          public boolean accept(File dir, String name) {
40            return name.contains("clean");
41          }
42        });
43        for (File f : toDelete) {
44          System.out.println(f);
45          f.delete();
46        }
47      }
48    }
49  
50    @After
51    public void tearDown() throws Exception {
52    }
53  
54    @Test
55    public void montlyRollover() throws Exception {
56      doRollover(Constants.OUTPUT_DIR_PREFIX + "clean-%d{" + MONTHLY_DATE_PATTERN
57          + "}.txt", MILLIS_IN_MONTH, 20);
58  
59    }
60  
61    @Test
62    public void dailyRollover() throws Exception {
63      doRollover(Constants.OUTPUT_DIR_PREFIX + "clean-%d{" + DAILY_DATE_PATTERN
64          + "}.txt.zip", MILLIS_IN_DAY, 5);
65    }
66  
67    void doRollover(String fileNamePattern, long delay, int maxHistory)
68        throws Exception {
69      long currentTime = System.currentTimeMillis();
70  
71      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
72      rfa.setContext(context);
73      rfa.setLayout(layout);
74      // rfa.setFile(Constants.OUTPUT_DIR_PREFIX + "clean.txt");
75      TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy();
76      tbrp.setContext(context);
77      tbrp.setFileNamePattern(fileNamePattern);
78  
79      tbrp.setMaxHistory(maxHistory);
80      tbrp.setParent(rfa);
81      tbrp.setCurrentTime(currentTime);
82      tbrp.start();
83      rfa.setRollingPolicy(tbrp);
84      rfa.start();
85  
86      for (int i = 0; i < maxHistory * 3; i++) {
87        rfa.doAppend("Hello---" + i);
88        tbrp.setCurrentTime(addTime(tbrp.getCurrentTime(), delay / 2));
89        if (tbrp.future != null) {
90          tbrp.future.get(200, TimeUnit.MILLISECONDS);
91        }
92      }
93      rfa.stop();
94      check(maxHistory + 1);
95    }
96  
97    void check(int expectedCount) {
98      // remove all files containing the string 'clean'
99      File dir = new File(Constants.OUTPUT_DIR_PREFIX);
100     if (dir.isDirectory()) {
101       File[] match = dir.listFiles(new FilenameFilter() {
102         public boolean accept(File dir, String name) {
103           return name.contains("clean");
104         }
105       });
106       //System.out.println(Arrays.toString(match));
107       assertEquals(expectedCount, match.length);
108     }
109 
110   }
111 
112   static long addTime(long currentTime, long timeToWait) {
113     return currentTime + timeToWait;
114   }
115 
116 }