1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.rolling.helper;
15
16 import java.time.ZoneId;
17 import java.util.Calendar;
18 import java.util.TimeZone;
19
20 import org.junit.jupiter.api.Test;
21
22 import ch.qos.logback.core.Context;
23 import ch.qos.logback.core.ContextBase;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28
29
30
31
32
33 public class FileNamePatternTest {
34
35 Context context = new ContextBase();
36
37 @Test
38 public void testSmoke() {
39 FileNamePattern pp = new FileNamePattern("t", context);
40 assertEquals("t", pp.convertInt(3));
41
42 pp = new FileNamePattern("foo", context);
43 assertEquals("foo", pp.convertInt(3));
44
45 pp = new FileNamePattern("%i foo", context);
46
47 assertEquals("3 foo", pp.convertInt(3));
48
49 pp = new FileNamePattern("foo%i.xixo", context);
50 assertEquals("foo3.xixo", pp.convertInt(3));
51
52 pp = new FileNamePattern("foo%i.log", context);
53 assertEquals("foo3.log", pp.convertInt(3));
54
55 pp = new FileNamePattern("foo.%i.log", context);
56 assertEquals("foo.3.log", pp.convertInt(3));
57
58 pp = new FileNamePattern("foo.%3i.log", context);
59 assertEquals("foo.003.log", pp.convertInt(3));
60
61 pp = new FileNamePattern("foo.%1i.log", context);
62 assertEquals("foo.43.log", pp.convertInt(43));
63
64
65
66
67
68
69 }
70
71 @Test
72
73 public void flowingI() {
74 {
75 FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
76 assertEquals("foo3bar3", pp.convertInt(3));
77 }
78 {
79 FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
80 assertEquals("foo3bar3", pp.convertInt(3));
81 }
82 }
83
84 @Test
85 public void date() {
86 Calendar cal = Calendar.getInstance();
87 cal.set(2003, 4, 20, 17, 55);
88
89 FileNamePattern pp = new FileNamePattern("foo%d{yyyy.MM.dd}", context);
90
91 assertEquals("foo2003.05.20", pp.convert(cal.getTime()));
92
93 pp = new FileNamePattern("foo%d{yyyy.MM.dd HH:mm}", context);
94 assertEquals("foo2003.05.20 17:55", pp.convert(cal.getTime()));
95
96 pp = new FileNamePattern("%d{yyyy.MM.dd HH:mm} foo", context);
97 assertEquals("2003.05.20 17:55 foo", pp.convert(cal.getTime()));
98
99 }
100
101 @Test
102 public void dateWithTimeZone() {
103 TimeZone utc = TimeZone.getTimeZone("UTC");
104 Calendar cal = Calendar.getInstance(utc);
105 cal.set(2003, 4, 20, 10, 55);
106
107 FileNamePattern fnp = new FileNamePattern("foo%d{yyyy-MM-dd'T'HH:mm, Australia/Perth}", context);
108
109 assertEquals("foo2003-05-20T18:55", fnp.convert(cal.getTime()));
110 }
111
112 @Test
113 public void auxAndTimeZoneShouldNotConflict() {
114 TimeZone utc = TimeZone.getTimeZone("UTC");
115 Calendar cal = Calendar.getInstance(utc);
116 cal.set(2003, 4, 20, 10, 55);
117
118 {
119 FileNamePattern fnp = new FileNamePattern("foo%d{yyyy-MM-dd'T'HH:mm, aux, Australia/Perth}", context);
120
121 assertEquals("foo2003-05-20T18:55", fnp.convert(cal.getTime()));
122 assertNull(fnp.getPrimaryDateTokenConverter());
123 }
124
125 {
126 FileNamePattern fnp = new FileNamePattern(
127 "folder/%d{yyyy/MM, aux, Australia/Perth}/test.%d{yyyy-MM-dd'T'HHmm, Australia/Perth}.log",
128 context);
129 assertEquals("folder/2003/05/test.2003-05-20T1855.log", fnp.convert(cal.getTime()));
130 assertNotNull(fnp.getPrimaryDateTokenConverter());
131 }
132 }
133
134 @Test
135 public void withBackslash() {
136 FileNamePattern pp = new FileNamePattern("c:\\foo\\bar.%i", context);
137 assertEquals("c:/foo/bar.3", pp.convertInt(3));
138 }
139
140 @Test
141 public void objectListConverter() {
142 Calendar cal = Calendar.getInstance();
143 cal.set(2003, 4, 20, 17, 55);
144 FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
145 assertEquals("foo-2003.05.20-79.txt", fnp.convertMultipleArguments(cal.getTime(), 79));
146 }
147
148 @Test
149 public void asRegexByDate() {
150
151 Calendar cal = Calendar.getInstance();
152 cal.set(2003, 4, 20, 17, 55);
153
154 {
155 FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
156 String regex = fnp.toRegexForFixedDate(cal.getTime());
157 assertEquals("foo-2003.05.20-(\\d+).txt", regex);
158 }
159 {
160 FileNamePattern fnp = new FileNamePattern("\\toto\\foo-%d{yyyy\\MM\\dd}-%i.txt", context);
161 String regex = fnp.toRegexForFixedDate(cal.getTime());
162 assertEquals("/toto/foo-2003/05/20-(\\d+).txt", regex);
163 }
164 }
165
166 @Test
167 public void asRegex() {
168 {
169 FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd}-%i.txt", context);
170 String regex = fnp.toRegex();
171 assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}-\\d+.txt", regex);
172 }
173 {
174 FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM.dd'T'}-%i.txt", context);
175 String regex = fnp.toRegex();
176 assertEquals("foo-\\d{4}\\.\\d{2}\\.\\d{2}T-\\d+.txt", regex);
177 }
178 }
179
180 @Test
181 public void convertMultipleDates() {
182 Calendar cal = Calendar.getInstance();
183 cal.set(2003, 4, 20, 17, 55);
184 FileNamePattern fnp = new FileNamePattern("foo-%d{yyyy.MM, aux}/%d{yyyy.MM.dd}.txt", context);
185 assertEquals("foo-2003.05/2003.05.20.txt", fnp.convert(cal.getTime()));
186 }
187
188 @Test
189 public void nullTimeZoneByDefault() {
190 FileNamePattern fnp = new FileNamePattern("%d{hh}", context);
191 assertNull(fnp.getPrimaryDateTokenConverter().getZoneId());
192 }
193
194 @Test
195 public void settingTimeZoneOptionHasAnEffect() {
196 ZoneId tz = ZoneId.of("Australia/Perth");
197
198 FileNamePattern fnp = new FileNamePattern("%d{hh, " + tz.getId() + "}", context);
199 assertEquals(tz, fnp.getPrimaryDateTokenConverter().getZoneId());
200 }
201 }