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.helper;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertNotNull;
018import static org.junit.Assert.assertNull;
019
020import java.util.Calendar;
021import java.util.TimeZone;
022
023import org.junit.Test;
024
025import ch.qos.logback.core.Context;
026import ch.qos.logback.core.ContextBase;
027
028/**
029 * @author Ceki
030 * 
031 */
032public class FileNamePatternTest {
033
034    Context context = new ContextBase();
035
036    @Test
037    public void testSmoke() {
038        FileNamePattern pp = new FileNamePattern("t", context);
039        assertEquals("t", pp.convertInt(3));
040
041        pp = new FileNamePattern("foo", context);
042        assertEquals("foo", pp.convertInt(3));
043
044        pp = new FileNamePattern("%i foo", context);
045
046        assertEquals("3 foo", pp.convertInt(3));
047
048        pp = new FileNamePattern("foo%i.xixo", context);
049        assertEquals("foo3.xixo", pp.convertInt(3));
050
051        pp = new FileNamePattern("foo%i.log", context);
052        assertEquals("foo3.log", pp.convertInt(3));
053
054        pp = new FileNamePattern("foo.%i.log", context);
055        assertEquals("foo.3.log", pp.convertInt(3));
056
057        pp = new FileNamePattern("foo.%3i.log", context);
058        assertEquals("foo.003.log", pp.convertInt(3));
059
060        pp = new FileNamePattern("foo.%1i.log", context);
061        assertEquals("foo.43.log", pp.convertInt(43));
062
063        // pp = new FileNamePattern("%i.foo\\%", context);
064        // assertEquals("3.foo%", pp.convertInt(3));
065
066        // pp = new FileNamePattern("\\%foo", context);
067        // assertEquals("%foo", pp.convertInt(3));
068    }
069
070    @Test
071    // test ways for dealing with flowing i converter, as in "foo%ix"
072    public void flowingI() {
073        {
074            FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
075            assertEquals("foo3bar3", pp.convertInt(3));
076        }
077        {
078            FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
079            assertEquals("foo3bar3", pp.convertInt(3));
080        }
081    }
082
083    @Test
084    public void date() {
085        Calendar cal = Calendar.getInstance();
086        cal.set(2003, 4, 20, 17, 55);
087
088        FileNamePattern pp = new FileNamePattern("foo%d{yyyy.MM.dd}", context);
089
090        assertEquals("foo2003.05.20", pp.convert(cal.getTime()));
091
092        pp = new FileNamePattern("foo%d{yyyy.MM.dd HH:mm}", context);
093        assertEquals("foo2003.05.20 17:55", pp.convert(cal.getTime()));
094
095        pp = new FileNamePattern("%d{yyyy.MM.dd HH:mm} foo", context);
096        assertEquals("2003.05.20 17:55 foo", pp.convert(cal.getTime()));
097
098    }
099
100    @Test
101    public void dateWithTimeZone() {
102        TimeZone utc = TimeZone.getTimeZone("UTC");
103        Calendar cal = Calendar.getInstance(utc);
104        cal.set(2003, 4, 20, 10, 55);
105
106        FileNamePattern fnp = new FileNamePattern("foo%d{yyyy-MM-dd'T'HH:mm, Australia/Perth}", context);
107        // Perth is 8 hours ahead of UTC
108        assertEquals("foo2003-05-20T18:55", fnp.convert(cal.getTime()));
109    }
110
111    @Test
112    public void auxAndTimeZoneShouldNotConflict() {
113        TimeZone utc = TimeZone.getTimeZone("UTC");
114        Calendar cal = Calendar.getInstance(utc);
115        cal.set(2003, 4, 20, 10, 55);
116
117        {
118            FileNamePattern fnp = new FileNamePattern("foo%d{yyyy-MM-dd'T'HH:mm, aux, Australia/Perth}", context);
119            // Perth is 8 hours ahead of UTC
120            assertEquals("foo2003-05-20T18:55", fnp.convert(cal.getTime()));
121            assertNull(fnp.getPrimaryDateTokenConverter());
122        }
123
124        {
125            FileNamePattern fnp = new FileNamePattern("folder/%d{yyyy/MM, aux, Australia/Perth}/test.%d{yyyy-MM-dd'T'HHmm, Australia/Perth}.log", context);
126            assertEquals("folder/2003/05/test.2003-05-20T1855.log", fnp.convert(cal.getTime()));
127            assertNotNull(fnp.getPrimaryDateTokenConverter());
128        }
129    }
130
131    @Test
132    public void withBackslash() {
133        FileNamePattern pp = new FileNamePattern("c:\\foo\\bar.%i", context);
134        assertEquals("c:/foo/bar.3", pp.convertInt(3));
135    }
136
137    @Test
138    public void objectListConverter() {
139        Calendar cal = Calendar.getInstance();
140        cal.set(2003, 4, 20, 17, 55);
141        FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
142        assertEquals("foo-2003.05.20-79.txt", fnp.convertMultipleArguments(cal.getTime(), 79));
143    }
144
145    @Test
146    public void asRegexByDate() {
147
148        Calendar cal = Calendar.getInstance();
149        cal.set(2003, 4, 20, 17, 55);
150
151        {
152            FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
153            String regex = fnp.toRegexForFixedDate(cal.getTime());
154            assertEquals("foo-2003.05.20-(\\d+).txt", regex);
155        }
156        {
157            FileNamePattern fnp = new FileNamePattern("\\toto\\foo-%d{yyyy\\MM\\dd}-%i.txt", context);
158            String regex = fnp.toRegexForFixedDate(cal.getTime());
159            assertEquals("/toto/foo-2003/05/20-(\\d+).txt", regex);
160        }
161    }
162
163    @Test
164    public void asRegex() {
165        {
166            FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
167            String regex = fnp.toRegex();
168            assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}-\\d+.txt", regex);
169        }
170        {
171            FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd'T'}-%i.txt", context);
172            String regex = fnp.toRegex();
173            assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}T-\\d+.txt", regex);
174        }
175    }
176
177    @Test
178    public void convertMultipleDates() {
179        Calendar cal = Calendar.getInstance();
180        cal.set(2003, 4, 20, 17, 55);
181        FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM, aux}/%d{yyyy.MM.dd}.txt", context);
182        assertEquals("foo-2003.05/2003.05.20.txt", fnp.convert(cal.getTime()));
183    }
184
185    @Test
186    public void nullTimeZoneByDefault() {
187        FileNamePattern fnp = new FileNamePattern("%d{hh}", context);
188        assertNull(fnp.getPrimaryDateTokenConverter().getTimeZone());
189    }
190
191    @Test
192    public void settingTimeZoneOptionHasAnEffect() {
193        TimeZone tz = TimeZone.getTimeZone("Australia/Perth");
194
195        FileNamePattern fnp = new FileNamePattern("%d{hh, " + tz.getID() + "}", context);
196        assertEquals(tz, fnp.getPrimaryDateTokenConverter().getTimeZone());
197    }
198}