View Javadoc
1   package ch.qos.logback.core.rolling;
2   
3   import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP;
4   import static ch.qos.logback.core.testUtil.CoreTestConstants.OUTPUT_DIR_PREFIX;
5   import static org.junit.jupiter.api.Assertions.assertNotNull;
6   import static org.junit.jupiter.api.Assertions.assertNull;
7   
8   import java.util.Map;
9   
10  import org.junit.jupiter.api.AfterEach;
11  import org.junit.jupiter.api.BeforeEach;
12  import org.junit.jupiter.api.Test;
13  
14  import ch.qos.logback.core.Context;
15  import ch.qos.logback.core.ContextBase;
16  import ch.qos.logback.core.FileAppender;
17  import ch.qos.logback.core.encoder.NopEncoder;
18  import ch.qos.logback.core.status.Status;
19  import ch.qos.logback.core.testUtil.RandomUtil;
20  import ch.qos.logback.core.status.testUtil.StatusChecker;
21  import ch.qos.logback.core.util.StatusPrinter;
22  
23  public class CollisionDetectionTest {
24  
25      Context context = new ContextBase();
26      StatusChecker statusChecker = new StatusChecker(context);
27      int diff = RandomUtil.getPositiveInt();
28      protected String randomOutputDir = OUTPUT_DIR_PREFIX + diff + "/";
29  
30      @BeforeEach
31      public void setUp() throws Exception {
32      }
33  
34      @AfterEach
35      public void tearDown() throws Exception {
36      }
37  
38      FileAppender<String> buildFileAppender(String name, String filenameSuffix) {
39          FileAppender<String> fileAppender = new FileAppender<String>();
40          fileAppender.setName(name);
41          fileAppender.setContext(context);
42          fileAppender.setFile(randomOutputDir + filenameSuffix);
43          fileAppender.setEncoder(new NopEncoder<String>());
44          return fileAppender;
45      }
46  
47      RollingFileAppender<String> buildRollingFileAppender(String name, String filenameSuffix, String patternSuffix) {
48          RollingFileAppender<String> rollingFileAppender = new RollingFileAppender<String>();
49          rollingFileAppender.setName(name);
50          rollingFileAppender.setContext(context);
51          rollingFileAppender.setFile(randomOutputDir + filenameSuffix);
52          rollingFileAppender.setEncoder(new NopEncoder<String>());
53  
54          TimeBasedRollingPolicy<String> tbrp = new TimeBasedRollingPolicy<String>();
55          tbrp.setContext(context);
56          tbrp.setFileNamePattern(randomOutputDir + patternSuffix);
57          tbrp.setParent(rollingFileAppender);
58          // tbrp.timeBasedFileNamingAndTriggeringPolicy = new
59          // DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
60          // tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime);
61          rollingFileAppender.setRollingPolicy(tbrp);
62          tbrp.start();
63  
64          return rollingFileAppender;
65      }
66  
67      @Test
68      public void collisionImpossibleForSingleAppender() {
69          FileAppender<String> fileAppender = buildFileAppender("FA", "collisionImpossibleForSingleAppender");
70          fileAppender.start();
71          statusChecker.assertIsErrorFree();
72  
73      }
74  
75      @Test
76      public void appenderStopShouldClearEntryInCollisionMap() {
77          String key = "FA";
78          FileAppender<String> fileAppender = buildFileAppender(key, "collisionImpossibleForSingleAppender");
79          fileAppender.start();
80          assertCollisionMapHasEntry(FA_FILENAME_COLLISION_MAP, key);
81          fileAppender.stop();
82          assertCollisionMapHasNoEntry(FA_FILENAME_COLLISION_MAP, key);
83          statusChecker.assertIsErrorFree();
84  
85      }
86  
87      private void assertCollisionMapHasEntry(String mapName, String key) {
88          @SuppressWarnings("unchecked")
89          Map<String, ?> map = (Map<String, ?>) context.getObject(mapName);
90          assertNotNull(map);
91          assertNotNull(map.get(key));
92      }
93  
94      private void assertCollisionMapHasNoEntry(String mapName, String key) {
95          @SuppressWarnings("unchecked")
96          Map<String, ?> map = (Map<String, ?>) context.getObject(mapName);
97          assertNotNull(map);
98          assertNull(map.get(key));
99      }
100 
101     @Test
102     public void collisionWithTwoFileAppenders() {
103         String suffix = "collisionWithToFileAppenders";
104 
105         FileAppender<String> fileAppender1 = buildFileAppender("FA1", suffix);
106         fileAppender1.start();
107 
108         FileAppender<String> fileAppender2 = buildFileAppender("FA2", suffix);
109         fileAppender2.start();
110         statusChecker.assertContainsMatch(Status.ERROR, "'File' option has the same value");
111         // StatusPrinter.print(context);
112     }
113 
114     @Test
115     public void collisionWith_FA_RFA() {
116         String suffix = "collisionWith_FA_RFA";
117 
118         FileAppender<String> fileAppender1 = buildFileAppender("FA", suffix);
119         fileAppender1.start();
120 
121         RollingFileAppender<String> rollingfileAppender = buildRollingFileAppender("RFA", suffix, "bla-%d.log");
122         rollingfileAppender.start();
123         StatusPrinter.print(context);
124         statusChecker.assertContainsMatch(Status.ERROR, "'File' option has the same value");
125     }
126 
127     @Test
128     public void collisionWith_2RFA() {
129         String suffix = "collisionWith_2RFA";
130 
131         RollingFileAppender<String> rollingfileAppender1 = buildRollingFileAppender("RFA1", suffix, "bla-%d.log");
132         rollingfileAppender1.start();
133         RollingFileAppender<String> rollingfileAppender2 = buildRollingFileAppender("RFA1", suffix, "bla-%d.log");
134         rollingfileAppender2.start();
135 
136         StatusPrinter.print(context);
137         statusChecker.assertContainsMatch(Status.ERROR, "'FileNamePattern' option has the same value");
138     }
139 
140 }