View Javadoc
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  //import org.apache.commons.io.FileUtils;
13  //import org.apache.commons.lang3.RandomStringUtils;
14  import org.junit.After;
15  import org.junit.Test;
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  import org.slf4j.LoggerFactoryFriend;
19  
20  import ch.qos.logback.classic.LoggerContext;
21  import ch.qos.logback.classic.joran.JoranConfigurator;
22  import ch.qos.logback.core.joran.spi.JoranException;
23  
24  public class LogbackListenerTest {
25      private File logFile = new File("target/test.log");
26  
27      private void doConfigure() throws JoranException {
28          LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
29          JoranConfigurator configurator = new JoranConfigurator();
30          configurator.setContext(context);
31          configurator.doConfigure(new File("src/test/input/issue/logback-1159.xml"));
32      }
33  
34      @After
35      public void after() {
36          logFile.delete();
37          LoggerFactoryFriend.reset();
38      }
39  
40      private void disableLogFileAccess() throws IOException {
41          logFile.createNewFile();
42          logFile.deleteOnExit();
43          Path path = Paths.get(logFile.toURI());
44          Set<PosixFilePermission> permissions = Collections.emptySet();
45          try {
46              Files.setPosixFilePermissions(path, permissions);
47          } catch (UnsupportedOperationException e) {
48              path.toFile().setReadOnly();
49          }
50      }
51  
52      @Test(expected = LoggingError.class)
53      public void testThatErrorIsDetectedAtLogInit() throws Exception {
54          disableLogFileAccess();
55          doConfigure();
56      }
57  
58      @Test
59      public void assertThatNonFailSafeAppendersNotAffected() throws JoranException {
60          doConfigure();
61          Logger logger = LoggerFactory.getLogger("NOTJOURNAL");
62          logger.error("This should not fail");
63      }
64  
65  }