1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.appender;
15
16 import java.io.File;
17 import java.util.List;
18
19 import org.junit.jupiter.api.Assertions;
20 import org.junit.jupiter.api.Test;
21
22 import ch.qos.logback.core.Appender;
23 import ch.qos.logback.core.FileAppender;
24 import ch.qos.logback.core.testUtil.DummyEncoder;
25 import ch.qos.logback.core.encoder.NopEncoder;
26 import ch.qos.logback.core.status.Status;
27 import ch.qos.logback.core.status.StatusManager;
28 import ch.qos.logback.core.testUtil.CoreTestConstants;
29 import ch.qos.logback.core.testUtil.RandomUtil;
30 import ch.qos.logback.core.status.testUtil.StatusChecker;
31 import ch.qos.logback.core.util.StatusPrinter;
32
33 public class FileAppenderTest extends AbstractAppenderTest<Object> {
34
35 int diff = RandomUtil.getPositiveInt();
36
37 protected Appender<Object> getAppender() {
38 return new FileAppender<Object>();
39 }
40
41 protected Appender<Object> getConfiguredAppender() {
42 FileAppender<Object> appender = new FileAppender<Object>();
43 appender.setEncoder(new NopEncoder<Object>());
44 appender.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "temp.log");
45 appender.setName("test");
46 appender.setContext(context);
47 appender.start();
48 return appender;
49 }
50
51 @Test
52 public void smoke() {
53 String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-smoke.log";
54
55 FileAppender<Object> appender = new FileAppender<Object>();
56 appender.setEncoder(new DummyEncoder<Object>());
57 appender.setAppend(false);
58 appender.setFile(filename);
59 appender.setName("smoke");
60 appender.setContext(context);
61 appender.start();
62 appender.doAppend(new Object());
63 appender.stop();
64
65 File file = new File(filename);
66 Assertions.assertTrue(file.exists());
67 Assertions.assertTrue(file.delete(), "failed to delete " + file.getAbsolutePath());
68 }
69
70 @Test
71 public void testCreateParentFolders() {
72 String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fat-testCreateParentFolders-" + diff
73 + "/testCreateParentFolders.txt";
74 File file = new File(filename);
75 Assertions.assertFalse(file.getParentFile().exists());
76 Assertions.assertFalse(file.exists());
77
78 FileAppender<Object> appender = new FileAppender<Object>();
79 appender.setEncoder(new DummyEncoder<Object>());
80 appender.setAppend(false);
81 appender.setFile(filename);
82 appender.setName("testCreateParentFolders");
83 appender.setContext(context);
84 appender.start();
85 appender.doAppend(new Object());
86 appender.stop();
87 Assertions.assertTrue(file.getParentFile().exists());
88 Assertions.assertTrue(file.exists());
89
90
91 Assertions.assertTrue(file.delete(), "failed to delete " + file.getAbsolutePath());
92 File parent = file.getParentFile();
93 Assertions.assertTrue(parent.delete(), "failed to delete " + parent.getAbsolutePath());
94 }
95
96 @Test
97 public void testPrudentModeLogicalImplications() {
98 String filename = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "fat-testPrudentModeLogicalImplications.txt";
99 File file = new File(filename);
100 FileAppender<Object> appender = new FileAppender<Object>();
101 appender.setEncoder(new DummyEncoder<Object>());
102 appender.setFile(filename);
103 appender.setName("testPrudentModeLogicalImplications");
104 appender.setContext(context);
105
106 appender.setAppend(false);
107 appender.setPrudent(true);
108 appender.start();
109
110 Assertions.assertTrue(appender.isAppend());
111
112 StatusManager sm = context.getStatusManager();
113
114 StatusChecker statusChecker = new StatusChecker(context);
115 Assertions.assertEquals(Status.WARN, statusChecker.getHighestLevel(0));
116 List<Status> statusList = sm.getCopyOfStatusList();
117 Assertions.assertTrue(
118 statusList.size() >= 2, "Expecting status list size to be 2 or larger, but was " + statusList.size());
119 String msg1 = statusList.get(1).getMessage();
120
121 Assertions.assertTrue(msg1.startsWith("Setting \"Append\" property"), "Got message [" + msg1 + "]");
122
123 appender.doAppend(new Object());
124 appender.stop();
125 Assertions.assertTrue(file.exists());
126 Assertions.assertTrue(file.delete(), "failed to delete " + file.getAbsolutePath());
127 }
128
129 @Test
130 public void fileNameCollision() {
131 String fileName = CoreTestConstants.OUTPUT_DIR_PREFIX + diff + "fileNameCollision";
132
133 FileAppender<Object> appender0 = new FileAppender<Object>();
134 appender0.setName("FA0");
135 appender0.setFile(fileName);
136 appender0.setContext(context);
137 appender0.setEncoder(new DummyEncoder<Object>());
138 appender0.start();
139 Assertions.assertTrue(appender0.isStarted());
140
141 FileAppender<Object> appender1 = new FileAppender<Object>();
142 appender1.setName("FA1");
143 appender1.setFile(fileName);
144 appender1.setContext(context);
145 appender1.setEncoder(new DummyEncoder<Object>());
146 appender1.start();
147
148 Assertions.assertFalse(appender1.isStarted());
149
150 StatusPrinter.print(context);
151 StatusChecker checker = new StatusChecker(context);
152 checker.assertContainsMatch(Status.ERROR, "'File' option has the same value");
153
154 }
155 }