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.classic.issue.logback1159;
16  
17  import java.io.File;
18  import java.io.IOException;
19  import java.nio.file.Files;
20  import java.nio.file.Path;
21  import java.nio.file.Paths;
22  import java.nio.file.attribute.PosixFilePermission;
23  import java.util.Collections;
24  import java.util.Set;
25  
26  //import org.apache.commons.io.FileUtils;
27  //import org.apache.commons.lang3.RandomStringUtils;
28  import ch.qos.logback.classic.util.LogbackMDCAdapter;
29  import org.junit.jupiter.api.AfterEach;
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.slf4j.LoggerFactoryFriend;
36  
37  import ch.qos.logback.classic.LoggerContext;
38  import ch.qos.logback.classic.joran.JoranConfigurator;
39  import ch.qos.logback.core.joran.spi.JoranException;
40  import org.slf4j.spi.MDCAdapter;
41  
42  public class LogbackListenerTest {
43      private File logFile = new File("target/test.log");
44  
45      LoggerContext loggerContext = new LoggerContext();
46      LogbackMDCAdapter mdcAdapter = new LogbackMDCAdapter();
47  
48      @BeforeEach
49      void setUp() {
50          loggerContext.setMDCAdapter(mdcAdapter);
51      }
52  
53      private void doConfigure() throws JoranException {
54          JoranConfigurator configurator = new JoranConfigurator();
55          configurator.setContext(loggerContext);
56          configurator.doConfigure(new File("src/test/input/issue/logback-1159.xml"));
57      }
58  
59      @AfterEach
60      public void after() {
61          logFile.delete();
62      }
63  
64      private void disableLogFileAccess() throws IOException {
65          logFile.createNewFile();
66          logFile.deleteOnExit();
67          Path path = Paths.get(logFile.toURI());
68          Set<PosixFilePermission> permissions = Collections.emptySet();
69          try {
70              Files.setPosixFilePermissions(path, permissions);
71          } catch (UnsupportedOperationException e) {
72              path.toFile().setReadOnly();
73          }
74      }
75  
76      @Test
77      public void testThatErrorIsDetectedAtLogInit() throws Exception {
78          Assertions.assertThrows(LoggingError.class, () -> {
79              disableLogFileAccess();
80              doConfigure();
81          });
82      }
83  
84      @Test
85      public void assertThatNonFailSafeAppendersNotAffected() throws JoranException {
86          doConfigure();
87          Logger logger = LoggerFactory.getLogger("NOTJOURNAL");
88          logger.error("This should not fail");
89      }
90  
91  }