View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertNull;
19  
20  import java.util.Calendar;
21  import java.util.TimeZone;
22  
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.Context;
26  import ch.qos.logback.core.ContextBase;
27  
28  /**
29   * @author Ceki
30   * 
31   */
32  public class FileNamePatternTest {
33  
34      Context context = new ContextBase();
35  
36      @Test
37      public void testSmoke() {
38          FileNamePattern pp = new FileNamePattern("t", context);
39          assertEquals("t", pp.convertInt(3));
40  
41          pp = new FileNamePattern("foo", context);
42          assertEquals("foo", pp.convertInt(3));
43  
44          pp = new FileNamePattern("%i foo", context);
45  
46          assertEquals("3 foo", pp.convertInt(3));
47  
48          pp = new FileNamePattern("foo%i.xixo", context);
49          assertEquals("foo3.xixo", pp.convertInt(3));
50  
51          pp = new FileNamePattern("foo%i.log", context);
52          assertEquals("foo3.log", pp.convertInt(3));
53  
54          pp = new FileNamePattern("foo.%i.log", context);
55          assertEquals("foo.3.log", pp.convertInt(3));
56  
57          pp = new FileNamePattern("foo.%3i.log", context);
58          assertEquals("foo.003.log", pp.convertInt(3));
59  
60          pp = new FileNamePattern("foo.%1i.log", context);
61          assertEquals("foo.43.log", pp.convertInt(43));
62  
63          // pp = new FileNamePattern("%i.foo\\%", context);
64          // assertEquals("3.foo%", pp.convertInt(3));
65  
66          // pp = new FileNamePattern("\\%foo", context);
67          // assertEquals("%foo", pp.convertInt(3));
68      }
69  
70      @Test
71      // test ways for dealing with flowing i converter, as in "foo%ix"
72      public void flowingI() {
73          {
74              FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
75              assertEquals("foo3bar3", pp.convertInt(3));
76          }
77          {
78              FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
79              assertEquals("foo3bar3", pp.convertInt(3));
80          }
81      }
82  
83      @Test
84      public void date() {
85          Calendar cal = Calendar.getInstance();
86          cal.set(2003, 4, 20, 17, 55);
87  
88          FileNamePattern pp = new FileNamePattern("foo%d{yyyy.MM.dd}", context);
89  
90          assertEquals("foo2003.05.20", pp.convert(cal.getTime()));
91  
92          pp = new FileNamePattern("foo%d{yyyy.MM.dd HH:mm}", context);
93          assertEquals("foo2003.05.20 17:55", pp.convert(cal.getTime()));
94  
95          pp = new FileNamePattern("%d{yyyy.MM.dd HH:mm} foo", context);
96          assertEquals("2003.05.20 17:55 foo", pp.convert(cal.getTime()));
97  
98      }
99  
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 }