1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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 v1.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.util;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.ContextBase;
19  import ch.qos.logback.core.hook.ShutdownHookBase;
20  import org.junit.jupiter.api.Disabled;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Given the code executed in a shutdown hook is the last bit of code that is executed
25   * before the JVM exits, there is no way to test that a shutdown hook has been removed or
26   * is active.
27   *
28   * As such, this test cannot be automated.
29   */
30  @Disabled
31  class ContextUtilAddOrReplaceShutdownHookTest {
32  
33      Context context = new ContextBase();
34      ContextUtil contextUtil = new ContextUtil(context);
35  
36      @Test
37      public void smoke() {
38  
39          contextUtil.addOrReplaceShutdownHook(new HelloShutdownHookHook(2));
40          contextUtil.addOrReplaceShutdownHook(new HelloShutdownHookHook(3));
41          contextUtil.addOrReplaceShutdownHook(new HelloShutdownHookHook(5));
42          // expect to see
43          // HelloShutdownHookHook{number=5}
44      }
45  
46      static class HelloShutdownHookHook extends ShutdownHookBase {
47  
48          int number;
49  
50  
51          public HelloShutdownHookHook(int number) {
52              this.number = number;
53  
54          }
55  
56          @Override
57          public void run() {
58              System.out.println(this);
59          }
60  
61          @Override
62          public String toString() {
63              return "HelloShutdownHookHook{" + "number=" + number + '}';
64          }
65      }
66  
67  }