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