001package ch.qos.logback.core.rolling;
002
003import java.util.Date;
004
005import org.junit.After;
006import org.junit.Before;
007import org.junit.Ignore;
008import org.junit.Test;
009
010import ch.qos.logback.core.CoreConstants;
011import ch.qos.logback.core.encoder.EchoEncoder;
012import ch.qos.logback.core.hook.DefaultShutdownHook;
013import ch.qos.logback.core.rolling.testUtil.ScaffoldingForRollingTests;
014import ch.qos.logback.core.status.OnConsoleStatusListener;
015import ch.qos.logback.core.testUtil.RandomUtil;
016import ch.qos.logback.core.util.StatusListenerConfigHelper;
017import ch.qos.logback.core.util.StatusPrinter;
018@Ignore
019public class JVMExitBeforeCompressionISDoneTest extends ScaffoldingForRollingTests {
020
021    RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
022    TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
023    DefaultShutdownHook delayingShutdownHook = new DefaultShutdownHook();
024    
025    static final long FRI_2016_05_13_T_170415_GMT = 1463159055630L;
026                                                    
027    EchoEncoder<Object> encoder = new EchoEncoder<Object>();
028
029    @Before
030    @Override
031    public void setUp() {
032        super.setUp();
033        StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
034        delayingShutdownHook.setContext(context);
035        initRFA(rfa);
036    }
037
038    void initRFA(RollingFileAppender<Object> rfa) {
039        rfa.setContext(context);
040        rfa.setEncoder(encoder);
041    }
042
043    void initTRBP(RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp, String filenamePattern, long givenTime) {
044        tbrp.setContext(context);
045        tbrp.setFileNamePattern(filenamePattern);
046        tbrp.setParent(rfa);
047        tbrp.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
048        tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime);
049        rfa.setRollingPolicy(tbrp);
050        tbrp.start();
051        rfa.start();
052    }
053
054    @After
055    public void tearDown() throws Exception {
056        StatusPrinter.print(context);
057    }
058
059    @Ignore
060    @Test
061    public void test1() {
062        Thread shutdownThread = new Thread(delayingShutdownHook);
063        Runtime.getRuntime().addShutdownHook(shutdownThread);
064        
065        String patternPrefix = "test1";
066        String compressionSuffix = ".zip";
067
068        this.currentTime = FRI_2016_05_13_T_170415_GMT;
069        
070        Date d = new Date(FRI_2016_05_13_T_170415_GMT); //WED_2016_03_23_T_230705_CET);
071        System.out.println(d);
072        System.out.print(d.getTime());
073        
074        int ticksPerHour = 100;
075        int hours = 7;
076        int totalTicks = ticksPerHour*hours;
077        long singleTickDuration = CoreConstants.MILLIS_IN_ONE_HOUR/ticksPerHour;
078        
079        String fileNamePatternStr = randomOutputDir + patternPrefix + "-%d{" + DATE_PATTERN_BY_DAY + ", GMT}" + compressionSuffix;
080        initTRBP(rfa, tbrp, fileNamePatternStr, currentTime);
081
082        incCurrentTime(singleTickDuration);
083        tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
084
085        for (int i = 0; i < totalTicks; i++) {
086            StringBuilder sb = new StringBuilder(1000);
087            sb.append("Hello");
088            for(int j = 0; j < 100; j++) {
089                sb.append(RandomUtil.getPositiveInt());
090            }
091            sb.append(i);
092            
093            rfa.doAppend(sb.toString());
094            addExpectedFileNamedIfItsTime_ByDate(fileNamePatternStr);
095            incCurrentTime(singleTickDuration);
096            tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime);
097        }
098        
099
100            
101        
102        // String nameOfExpectedZipFile = randomOutputDir + patternPrefix+"-2016-05-13.zip";;
103        
104        // File expectedZipFile = new File(nameOfExpectedZipFile);
105        // assertTrue("expecting file ["+nameOfExpectedZipFile+"] to exist", expectedZipFile.exists());
106        // File[] files = getFilesInDirectory(randomOutputDir);
107        // assertEquals(2, files.length);
108    }
109
110}