001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.rolling;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018import static org.junit.Assert.assertTrue;
019
020import org.junit.Before;
021import org.junit.Test;
022
023import ch.qos.logback.core.Context;
024import ch.qos.logback.core.ContextBase;
025import ch.qos.logback.core.status.Status;
026import ch.qos.logback.core.testUtil.StatusChecker;
027
028/**
029 * @author Ceki Gülcü
030 */
031public class TimeBasedFileNamingAndTriggeringPolicyBaseTest {
032
033    static long MILLIS_IN_MINUTE = 60 * 1000;
034    static long MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE;
035
036    Context context = new ContextBase();
037    RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
038    TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy<Object>();
039    DefaultTimeBasedFileNamingAndTriggeringPolicy<Object> timeBasedFNATP = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>();
040
041    @Before
042    public void setUp() {
043        rfa.setContext(context);
044        tbrp.setContext(context);
045        timeBasedFNATP.setContext(context);
046
047        rfa.setRollingPolicy(tbrp);
048        tbrp.setParent(rfa);
049        tbrp.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedFNATP);
050        timeBasedFNATP.setTimeBasedRollingPolicy(tbrp);
051    }
052
053    @Test
054    public void singleDate() {
055        // Tuesday December 20th 17:59:01 CET 2011
056        long startTime = 1324400341553L;
057        tbrp.setFileNamePattern("foo-%d{yyyy-MM'T'mm}.log");
058        tbrp.start();
059
060        timeBasedFNATP.setCurrentTime(startTime);
061        timeBasedFNATP.start();
062
063        timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
064        timeBasedFNATP.isTriggeringEvent(null, null);
065        String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
066        assertEquals("foo-2011-12T59.log", elapsedPeriodsFileName);
067    }
068
069    // see "log rollover should be configurable using %d multiple times in file name pattern"
070    // http://jira.qos.ch/browse/LBCORE-242
071
072    @Test
073    public void multiDate() {
074        // Tuesday December 20th 17:59:01 CET 2011
075        long startTime = 1324400341553L;
076        tbrp.setFileNamePattern("foo-%d{yyyy-MM, AUX}/%d{mm}.log");
077        tbrp.start();
078
079        timeBasedFNATP.setCurrentTime(startTime);
080        timeBasedFNATP.start();
081
082        timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE);
083        boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
084        assertTrue(triggerred);
085        String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
086        assertEquals("foo-2011-12/59.log", elapsedPeriodsFileName);
087    }
088
089    @Test
090    public void withTimeZone() {
091        // Tuesday December 20th 17:59:01 CET 2011
092        long startTime = 1324400341553L;
093        tbrp.setFileNamePattern("foo-%d{yyyy-MM-dd, GMT+5}.log");
094        tbrp.start();
095
096        timeBasedFNATP.setCurrentTime(startTime);
097        timeBasedFNATP.start();
098
099        timeBasedFNATP.setCurrentTime(startTime + MILLIS_IN_MINUTE + 2 * MILLIS_IN_HOUR);
100        boolean triggerred = timeBasedFNATP.isTriggeringEvent(null, null);
101        assertTrue(triggerred);
102        String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
103        assertEquals("foo-2011-12-20.log", elapsedPeriodsFileName);
104    }
105
106    @Test
107    public void extraIntegerTokenInFileNamePatternShouldBeDetected() {
108        String pattern = "test-%d{yyyy-MM-dd'T'HH}-%i.log.zip";
109        tbrp.setFileNamePattern(pattern);
110        tbrp.start();
111        
112        assertFalse(tbrp.isStarted());
113        StatusChecker statusChecker = new StatusChecker(context);
114        statusChecker.assertContainsMatch(Status.ERROR, "Filename pattern .{37} contains an integer token converter");
115    }
116}