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.util;
15  
16  import static org.junit.Assert.assertTrue;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.ArrayList;
20  import java.util.Calendar;
21  import java.util.List;
22  import java.util.Locale;
23  
24  import ch.qos.logback.core.rolling.helper.DateTokenConverter;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  
28  import ch.qos.logback.core.CoreConstants;
29  
30  public class DatePatternToRegexTest {
31      static Calendar CAL_2009_08_3_NIGHT = Calendar.getInstance();
32      static Calendar CAL_2009_08_3_MORNING = Calendar.getInstance();
33      static Locale CZ_LOCALE = new Locale("cs", "CZ");
34      static Locale KO_LOCALE = new Locale("ko", "KR");
35  
36      @BeforeClass
37      public static void setUpCalendars() {
38          CAL_2009_08_3_NIGHT.set(2009, 8, 3, 21, 57, 16);
39          CAL_2009_08_3_NIGHT.set(Calendar.MILLISECOND, 333);
40  
41          CAL_2009_08_3_MORNING.set(2009, 8, 3, 10, 24, 37);
42          CAL_2009_08_3_MORNING.set(Calendar.MILLISECOND, 333);
43      }
44  
45      @Test
46      public void ISO8601() {
47          doTest(CoreConstants.ISO8601_PATTERN, CAL_2009_08_3_NIGHT);
48      }
49  
50      @Test
51      public void withQuotes() {
52          doTest("yyyy-MM-dd'T'HH:mm:ss,SSS", CAL_2009_08_3_NIGHT);
53  
54      }
55  
56      @Test
57      public void month() {
58          doTest("yyyy-MMM-dd", CAL_2009_08_3_NIGHT);
59          doTest("yyyy-MMM-dd", CAL_2009_08_3_NIGHT, CZ_LOCALE);
60          doTest("yyyy-MMM-dd", CAL_2009_08_3_NIGHT, KO_LOCALE);
61  
62          doTest("yyyy-MMMM-dd", CAL_2009_08_3_NIGHT);
63          doTest("yyyy-MMMM-dd", CAL_2009_08_3_NIGHT, CZ_LOCALE);
64          doTest("yyyy-MMMM-dd", CAL_2009_08_3_NIGHT, KO_LOCALE);
65  
66      }
67  
68      public void monthWithLocal() {
69  
70      }
71  
72      @Test
73      public void dot() {
74          doTest("yyyy.MMM.dd", CAL_2009_08_3_NIGHT);
75          ;
76      }
77  
78      @Test
79      public void timeZone() {
80          doTest("yyyy-MMM-dd HH:mm:ss z", CAL_2009_08_3_NIGHT);
81          doTest("yyyy-MMM-dd HH:mm:ss Z", CAL_2009_08_3_NIGHT);
82      }
83  
84      @Test
85      public void dayInWeek() {
86          doTest("EE", CAL_2009_08_3_NIGHT);
87          doTest("EE", CAL_2009_08_3_NIGHT, CZ_LOCALE);
88          doTest("EE", CAL_2009_08_3_NIGHT, KO_LOCALE);
89  
90          doTest("EEEE", CAL_2009_08_3_NIGHT);
91          doTest("EEEE", CAL_2009_08_3_NIGHT, CZ_LOCALE);
92          doTest("EEEE", CAL_2009_08_3_NIGHT, KO_LOCALE);
93      }
94  
95      @Test
96      public void amPm() {
97          doTest("yyyy-MM-dd a", CAL_2009_08_3_NIGHT);
98          doTest("yyyy-MM-dd a", CAL_2009_08_3_NIGHT, CZ_LOCALE);
99          doTest("yyyy-MM-dd a", CAL_2009_08_3_NIGHT, KO_LOCALE);
100 
101         doTest("yyyy-MM-dd a", CAL_2009_08_3_MORNING);
102         doTest("yyyy-MM-dd a", CAL_2009_08_3_MORNING, CZ_LOCALE);
103         doTest("yyyy-MM-dd a", CAL_2009_08_3_MORNING, KO_LOCALE);
104 
105     }
106 
107     void doTest(String datePattern, Calendar calendar) {
108         doTest(datePattern, calendar, null);
109     }
110 
111     void doTest(String datePattern, Calendar calendar, Locale locale) {
112         Locale oldDefaultLocale = Locale.getDefault();
113         if (locale != null) {
114             Locale.setDefault(locale);
115         }
116 
117         try {
118             SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
119             DateTokenConverter<?> dtc = makeDTC(datePattern);
120             verify(sdf, calendar, dtc);
121         } finally {
122             if (locale != null)
123                 Locale.setDefault(oldDefaultLocale);
124 
125         }
126     }
127 
128     Locale locale;
129 
130     // void doTest(String datePattern, Calendar calendar) {
131     // doTest(datePattern, calendar, false);
132     // }
133 
134     void verify(SimpleDateFormat sdf, Calendar calendar, DateTokenConverter<?> dtc) {
135         String expected = sdf.format(calendar.getTime());
136         // if (slashified) {
137         // expected = expected.replace('\\', '/');
138         // }
139         String regex = dtc.toRegex();
140         // System.out.println("expected="+expected);
141         // System.out.println(regex);
142         assertTrue("[" + expected + "] does not match regex [" + regex + "]", expected.matches(regex));
143     }
144 
145     private DateTokenConverter<?> makeDTC(String datePattern) {
146         DateTokenConverter<?> dtc = new DateTokenConverter<Object>();
147         List<String> optionList = new ArrayList<String>();
148         optionList.add(datePattern);
149         dtc.setOptionList(optionList);
150         dtc.start();
151         return dtc;
152     }
153 }