001package ch.qos.logback.core.rolling;
002
003import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP;
004import static ch.qos.logback.core.testUtil.CoreTestConstants.OUTPUT_DIR_PREFIX;
005
006import java.util.Map;
007
008import org.junit.After;
009import org.junit.Assert;
010import org.junit.Before;
011import org.junit.Test;
012
013import ch.qos.logback.core.Context;
014import ch.qos.logback.core.ContextBase;
015import ch.qos.logback.core.FileAppender;
016import ch.qos.logback.core.encoder.NopEncoder;
017import ch.qos.logback.core.status.Status;
018import ch.qos.logback.core.testUtil.RandomUtil;
019import ch.qos.logback.core.testUtil.StatusChecker;
020import ch.qos.logback.core.util.StatusPrinter;
021
022public class CollisionDetectionTest {
023
024    Context context = new ContextBase();
025    StatusChecker statusChecker = new StatusChecker(context);
026    int diff = RandomUtil.getPositiveInt();
027    protected String randomOutputDir = OUTPUT_DIR_PREFIX + diff + "/";
028    
029    @Before
030    public void setUp() throws Exception {
031    }
032
033    @After
034    public void tearDown() throws Exception {
035    }
036
037    
038    FileAppender<String> buildFileAppender(String name, String filenameSuffix) {
039        FileAppender<String> fileAppender = new FileAppender<String>();
040        fileAppender.setName(name);
041        fileAppender.setContext(context);
042        fileAppender.setFile(randomOutputDir+filenameSuffix);
043        fileAppender.setEncoder(new NopEncoder<String>());
044        return fileAppender;
045    }
046    
047    RollingFileAppender<String> buildRollingFileAppender(String name, String filenameSuffix, String patternSuffix) {
048        RollingFileAppender<String> rollingFileAppender = new RollingFileAppender<String>();
049        rollingFileAppender.setName(name);
050        rollingFileAppender.setContext(context);
051        rollingFileAppender.setFile(randomOutputDir+filenameSuffix);
052        rollingFileAppender.setEncoder(new NopEncoder<String>());
053        
054        TimeBasedRollingPolicy<String> tbrp = new TimeBasedRollingPolicy<String>();
055        tbrp.setContext(context);
056        tbrp.setFileNamePattern(randomOutputDir+patternSuffix);
057        tbrp.setParent(rollingFileAppender);
058        //tbrp.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
059        //tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime);
060        rollingFileAppender.setRollingPolicy(tbrp);
061        tbrp.start();
062
063        
064        return rollingFileAppender;
065    }
066    
067    
068    @Test
069    public void collisionImpossibleForSingleAppender() {
070        FileAppender<String> fileAppender = buildFileAppender("FA", "collisionImpossibleForSingleAppender");
071        fileAppender.start();
072        statusChecker.assertIsErrorFree();
073        
074    }
075
076    @Test
077    public void appenderStopShouldClearEntryInCollisionMap() {
078        String key = "FA";
079        FileAppender<String> fileAppender = buildFileAppender(key, "collisionImpossibleForSingleAppender");
080        fileAppender.start();
081        assertCollisionMapHasEntry(FA_FILENAME_COLLISION_MAP, key);
082        fileAppender.stop();
083        assertCollisionMapHasNoEntry(FA_FILENAME_COLLISION_MAP, key);
084        statusChecker.assertIsErrorFree();
085        
086        
087    }
088    
089    private void assertCollisionMapHasEntry(String mapName, String key) {
090        @SuppressWarnings("unchecked")
091        Map<String, ?> map = (Map<String, ?>) context.getObject(mapName);
092        Assert.assertNotNull(map);
093        Assert.assertNotNull(map.get(key));
094    }
095    private void assertCollisionMapHasNoEntry(String mapName, String key) {
096        @SuppressWarnings("unchecked")
097        Map<String, ?> map = (Map<String, ?>) context.getObject(mapName);
098        Assert.assertNotNull(map);
099        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}