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.util;
015
016import static org.junit.Assert.assertTrue;
017
018import java.text.SimpleDateFormat;
019import java.util.ArrayList;
020import java.util.Calendar;
021import java.util.List;
022import java.util.Locale;
023
024import ch.qos.logback.core.rolling.helper.DateTokenConverter;
025import org.junit.BeforeClass;
026import org.junit.Test;
027
028import ch.qos.logback.core.CoreConstants;
029
030public class DatePatternToRegexTest {
031    static Calendar CAL_2009_08_3_NIGHT = Calendar.getInstance();
032    static Calendar CAL_2009_08_3_MORNING = Calendar.getInstance();
033    static Locale CZ_LOCALE = new Locale("cs", "CZ");
034    static Locale KO_LOCALE = new Locale("ko", "KR");
035
036    @BeforeClass
037    public static void setUpCalendars() {
038        CAL_2009_08_3_NIGHT.set(2009, 8, 3, 21, 57, 16);
039        CAL_2009_08_3_NIGHT.set(Calendar.MILLISECOND, 333);
040
041        CAL_2009_08_3_MORNING.set(2009, 8, 3, 10, 24, 37);
042        CAL_2009_08_3_MORNING.set(Calendar.MILLISECOND, 333);
043    }
044
045    @Test
046    public void ISO8601() {
047        doTest(CoreConstants.ISO8601_PATTERN, CAL_2009_08_3_NIGHT);
048    }
049
050    @Test
051    public void withQuotes() {
052        doTest("yyyy-MM-dd'T'HH:mm:ss,SSS", CAL_2009_08_3_NIGHT);
053
054    }
055
056    @Test
057    public void month() {
058        doTest("yyyy-MMM-dd", CAL_2009_08_3_NIGHT);
059        doTest("yyyy-MMM-dd", CAL_2009_08_3_NIGHT, CZ_LOCALE);
060        doTest("yyyy-MMM-dd", CAL_2009_08_3_NIGHT, KO_LOCALE);
061
062        doTest("yyyy-MMMM-dd", CAL_2009_08_3_NIGHT);
063        doTest("yyyy-MMMM-dd", CAL_2009_08_3_NIGHT, CZ_LOCALE);
064        doTest("yyyy-MMMM-dd", CAL_2009_08_3_NIGHT, KO_LOCALE);
065
066    }
067
068    public void monthWithLocal() {
069
070    }
071
072    @Test
073    public void dot() {
074        doTest("yyyy.MMM.dd", CAL_2009_08_3_NIGHT);
075        ;
076    }
077
078    @Test
079    public void timeZone() {
080        doTest("yyyy-MMM-dd HH:mm:ss z", CAL_2009_08_3_NIGHT);
081        doTest("yyyy-MMM-dd HH:mm:ss Z", CAL_2009_08_3_NIGHT);
082    }
083
084    @Test
085    public void dayInWeek() {
086        doTest("EE", CAL_2009_08_3_NIGHT);
087        doTest("EE", CAL_2009_08_3_NIGHT, CZ_LOCALE);
088        doTest("EE", CAL_2009_08_3_NIGHT, KO_LOCALE);
089
090        doTest("EEEE", CAL_2009_08_3_NIGHT);
091        doTest("EEEE", CAL_2009_08_3_NIGHT, CZ_LOCALE);
092        doTest("EEEE", CAL_2009_08_3_NIGHT, KO_LOCALE);
093    }
094
095    @Test
096    public void amPm() {
097        doTest("yyyy-MM-dd a", CAL_2009_08_3_NIGHT);
098        doTest("yyyy-MM-dd a", CAL_2009_08_3_NIGHT, CZ_LOCALE);
099        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}