1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.rolling;
15
16 import org.junit.jupiter.api.AfterEach;
17 import org.junit.jupiter.api.BeforeEach;
18 import org.junit.jupiter.api.Test;
19
20 import ch.qos.logback.classic.ClassicTestConstants;
21 import ch.qos.logback.classic.Logger;
22 import ch.qos.logback.classic.LoggerContext;
23 import ch.qos.logback.classic.joran.JoranConfigurator;
24 import ch.qos.logback.core.joran.spi.JoranException;
25 import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
26 import ch.qos.logback.core.testUtil.CoreTestConstants;
27 import ch.qos.logback.core.testUtil.RandomUtil;
28 import ch.qos.logback.core.status.testUtil.StatusChecker;
29 import ch.qos.logback.core.util.CachingDateFormatter;
30
31
32
33
34
35
36
37
38 public class UniqueFileTest {
39 static String UNIK_DIFF = "UNIK_DIFF";
40
41 LoggerContext lc = new LoggerContext();
42 StatusChecker sc = new StatusChecker(lc);
43 Logger logger = lc.getLogger(this.getClass());
44 int diff = RandomUtil.getPositiveInt() % 1000;
45 String diffAsStr = Integer.toString(diff);
46
47 @BeforeEach
48 public void setUp() {
49 System.setProperty(UNIK_DIFF, diffAsStr);
50 }
51
52 @AfterEach
53 public void tearDown() {
54 System.clearProperty(UNIK_DIFF);
55 }
56
57 void loadConfig(String confifFile) throws JoranException {
58 JoranConfigurator jc = new JoranConfigurator();
59 jc.setContext(lc);
60 jc.doConfigure(confifFile);
61 }
62
63 @Test
64 public void basic() throws Exception {
65 loadConfig(ClassicTestConstants.JORAN_INPUT_PREFIX + "unique.xml");
66 CachingDateFormatter sdf = new CachingDateFormatter("yyyyMMdd'T'HHmm");
67 String timestamp = sdf.format(System.currentTimeMillis());
68
69 sc.assertIsErrorFree();
70
71 Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME);
72 root.info("hello");
73
74 ScaffoldingForRollingTests
75 .existenceCheck(CoreTestConstants.OUTPUT_DIR_PREFIX + "UNIK_" + timestamp + diffAsStr + "log.txt");
76 }
77 }