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