View Javadoc
1   package ch.qos.logback.core.rolling;
2   
3   import java.util.Date;
4   
5   import org.junit.After;
6   import org.junit.Before;
7   import org.junit.Ignore;
8   import org.junit.Test;
9   
10  import ch.qos.logback.core.CoreConstants;
11  import ch.qos.logback.core.encoder.EchoEncoder;
12  import ch.qos.logback.core.hook.DefaultShutdownHook;
13  import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
14  import ch.qos.logback.core.status.OnConsoleStatusListener;
15  import ch.qos.logback.core.testUtil.RandomUtil;
16  import ch.qos.logback.core.util.StatusListenerConfigHelper;
17  import ch.qos.logback.core.util.StatusPrinter;
18  @Ignore
19  public class JVMExitBeforeCompressionISDoneTest extends ScaffoldingForRollingTests {
20  
21      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
22      TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
23      DefaultShutdownHook delayingShutdownHook = new DefaultShutdownHook();
24      
25      static final long FRI_2016_05_13_T_170415_GMT = 1463159055630L;
26                                                      
27      EchoEncoder<Object> encoder = new EchoEncoder<Object>();
28  
29      @Before
30      @Override
31      public void setUp() {
32          super.setUp();
33          StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
34          delayingShutdownHook.setContext(context);
35          initRFA(rfa);
36      }
37  
38      void initRFA(RollingFileAppender<Object> rfa) {
39          rfa.setContext(context);
40          rfa.setEncoder(encoder);
41      }
42  
43      void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp, String filenamePattern, long givenTime) {
44          tbrp.setContext(context);
45          tbrp.setFileNamePattern(filenamePattern);
46          tbrp.setParent(rfa);
47          tbrp.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
48          tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime);
49          rfa.setRollingPolicy(tbrp);
50          tbrp.start();
51          rfa.start();
52      }
53  
54      @After
55      public void tearDown() throws Exception {
56          StatusPrinter.print(context);
57      }
58  
59      @Ignore
60      @Test
61      public void test1() {
62          Thread shutdownThread = new Thread(delayingShutdownHook);
63          Runtime.getRuntime().addShutdownHook(shutdownThread);
64          
65          String patternPrefix = "test1";
66          String compressionSuffix = ".zip";
67  
68          this.currentTime = FRI_2016_05_13_T_170415_GMT;
69          
70          Date d = new Date(FRI_2016_05_13_T_170415_GMT); //WED_2016_03_23_T_230705_CET);
71          System.out.println(d);
72          System.out.print(d.getTime());
73          
74          int ticksPerHour = 100;
75          int hours = 7;
76          int totalTicks = ticksPerHour*hours;
77          long singleTickDuration = CoreConstants.MILLIS_IN_ONE_HOUR/ticksPerHour;
78          
79          String fileNamePatternStr = randomOutputDir + patternPrefix + "-%d{" + DATE_PATTERN_BY_DAY + ", GMT}" + compressionSuffix;
80          initTRBP(rfa, tbrp, fileNamePatternStr, currentTime);
81  
82          incCurrentTime(singleTickDuration);
83          tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
84  
85          for (int i = 0; i < totalTicks; i++) {
86              StringBuilder sb = new StringBuilder(1000);
87              sb.append("Hello");
88              for(int j = 0; j < 100; j++) {
89                  sb.append(RandomUtil.getPositiveInt());
90              }
91              sb.append(i);
92              
93              rfa.doAppend(sb.toString());
94              addExpectedFileNamedIfItsTime_ByDate(fileNamePatternStr);
95              incCurrentTime(singleTickDuration);
96              tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
97          }
98          
99  
100             
101         
102         // String nameOfExpectedZipFile = randomOutputDir + patternPrefix+"-2016-05-13.zip";;
103         
104         // File expectedZipFile = new File(nameOfExpectedZipFile);
105         // assertTrue("expecting file ["+nameOfExpectedZipFile+"] to exist", expectedZipFile.exists());
106         // File[] files = getFilesInDirectory(randomOutputDir);
107         // assertEquals(2, files.length);
108     }
109 
110 }