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   
6   import java.util.Map;
7   
8   import org.junit.After;
9   import org.junit.Assert;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  import ch.qos.logback.core.Context;
14  import ch.qos.logback.core.ContextBase;
15  import ch.qos.logback.core.FileAppender;
16  import ch.qos.logback.core.encoder.NopEncoder;
17  import ch.qos.logback.core.status.Status;
18  import ch.qos.logback.core.testUtil.RandomUtil;
19  import ch.qos.logback.core.testUtil.StatusChecker;
20  import ch.qos.logback.core.util.StatusPrinter;
21  
22  public class CollisionDetectionTest {
23  
24      Context context = new ContextBase();
25      StatusChecker statusChecker = new StatusChecker(context);
26      int diff = RandomUtil.getPositiveInt();
27      protected String randomOutputDir = OUTPUT_DIR_PREFIX + diff + "/";
28      
29      @Before
30      public void setUp() throws Exception {
31      }
32  
33      @After
34      public void tearDown() throws Exception {
35      }
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 DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
59          //tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime);
60          rollingFileAppender.setRollingPolicy(tbrp);
61          tbrp.start();
62  
63          
64          return rollingFileAppender;
65      }
66      
67      
68      @Test
69      public void collisionImpossibleForSingleAppender() {
70          FileAppender<String> fileAppender = buildFileAppender("FA", "collisionImpossibleForSingleAppender");
71          fileAppender.start();
72          statusChecker.assertIsErrorFree();
73          
74      }
75  
76      @Test
77      public void appenderStopShouldClearEntryInCollisionMap() {
78          String key = "FA";
79          FileAppender<String> fileAppender = buildFileAppender(key, "collisionImpossibleForSingleAppender");
80          fileAppender.start();
81          assertCollisionMapHasEntry(FA_FILENAME_COLLISION_MAP, key);
82          fileAppender.stop();
83          assertCollisionMapHasNoEntry(FA_FILENAME_COLLISION_MAP, key);
84          statusChecker.assertIsErrorFree();
85          
86          
87      }
88      
89      private void assertCollisionMapHasEntry(String mapName, String key) {
90          @SuppressWarnings("unchecked")
91          Map<String, ?> map = (Map<String, ?>) context.getObject(mapName);
92          Assert.assertNotNull(map);
93          Assert.assertNotNull(map.get(key));
94      }
95      private void assertCollisionMapHasNoEntry(String mapName, String key) {
96          @SuppressWarnings("unchecked")
97          Map<String, ?> map = (Map<String, ?>) context.getObject(mapName);
98          Assert.assertNotNull(map);
99          Assert.assertNull(map.get(key));
100     }
101 
102     @Test
103     public void collisionWithTwoFileAppenders() {
104         String suffix = "collisionWithToFileAppenders";
105         
106         FileAppender<String> fileAppender1 = buildFileAppender("FA1", suffix);
107         fileAppender1.start();
108         
109         FileAppender<String> fileAppender2 = buildFileAppender("FA2", suffix);
110         fileAppender2.start();
111         statusChecker.assertContainsMatch(Status.ERROR, "'File' option has the same value");
112         //StatusPrinter.print(context);
113     }
114 
115     @Test
116     public void collisionWith_FA_RFA() {
117         String suffix = "collisionWith_FA_RFA";
118         
119         FileAppender<String> fileAppender1 = buildFileAppender("FA", suffix);
120         fileAppender1.start();
121         
122         RollingFileAppender<String> rollingfileAppender = buildRollingFileAppender("RFA", suffix, "bla-%d.log");
123         rollingfileAppender.start();
124         StatusPrinter.print(context);
125         statusChecker.assertContainsMatch(Status.ERROR, "'File' option has the same value");        
126     }
127 
128     @Test
129     public void collisionWith_2RFA() {
130         String suffix = "collisionWith_2RFA";
131         
132         RollingFileAppender<String> rollingfileAppender1 = buildRollingFileAppender("RFA1", suffix, "bla-%d.log");
133         rollingfileAppender1.start();
134         RollingFileAppender<String> rollingfileAppender2 = buildRollingFileAppender("RFA1", suffix, "bla-%d.log");
135         rollingfileAppender2.start();
136         
137         StatusPrinter.print(context);
138         statusChecker.assertContainsMatch(Status.ERROR, "'FileNamePattern' option has the same value");        
139     }
140 
141 }