1 package ch.qos.logback.classic.issue.logback1159;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8 import java.nio.file.attribute.PosixFilePermission;
9 import java.util.Collections;
10 import java.util.Set;
11
12
13
14 import org.junit.jupiter.api.AfterEach;
15 import org.junit.jupiter.api.Assertions;
16 import org.junit.jupiter.api.Test;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.slf4j.LoggerFactoryFriend;
20
21 import ch.qos.logback.classic.LoggerContext;
22 import ch.qos.logback.classic.joran.JoranConfigurator;
23 import ch.qos.logback.core.joran.spi.JoranException;
24
25 public class LogbackListenerTest {
26 private File logFile = new File("target/test.log");
27
28 private void doConfigure() throws JoranException {
29 LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
30 JoranConfigurator configurator = new JoranConfigurator();
31 configurator.setContext(context);
32 configurator.doConfigure(new File("src/test/input/issue/logback-1159.xml"));
33 }
34
35 @AfterEach
36 public void after() {
37 logFile.delete();
38 LoggerFactoryFriend.reset();
39 }
40
41 private void disableLogFileAccess() throws IOException {
42 logFile.createNewFile();
43 logFile.deleteOnExit();
44 Path path = Paths.get(logFile.toURI());
45 Set<PosixFilePermission> permissions = Collections.emptySet();
46 try {
47 Files.setPosixFilePermissions(path, permissions);
48 } catch (UnsupportedOperationException e) {
49 path.toFile().setReadOnly();
50 }
51 }
52
53 @Test
54 public void testThatErrorIsDetectedAtLogInit() throws Exception {
55 Assertions.assertThrows(LoggingError.class, () -> {
56 disableLogFileAccess();
57 doConfigure();
58 });
59 }
60
61 @Test
62 public void assertThatNonFailSafeAppendersNotAffected() throws JoranException {
63 doConfigure();
64 Logger logger = LoggerFactory.getLogger("NOTJOURNAL");
65 logger.error("This should not fail");
66 }
67
68 }