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