View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.time;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.text.ParsePosition;
22  import java.util.Calendar;
23  import java.util.GregorianCalendar;
24  import java.util.Locale;
25  import java.util.TimeZone;
26  import java.util.stream.Stream;
27  
28  import org.apache.commons.lang3.AbstractLangTest;
29  import org.junit.jupiter.params.ParameterizedTest;
30  import org.junit.jupiter.params.provider.Arguments;
31  import org.junit.jupiter.params.provider.MethodSource;
32  import org.junitpioneer.jupiter.ReadsDefaultLocale;
33  import org.junitpioneer.jupiter.ReadsDefaultTimeZone;
34  
35  class WeekYearTest extends AbstractLangTest {
36  
37      public static Stream<Arguments> data() {
38          // @formatter:off
39          return Stream.of(
40              Arguments.of(new GregorianCalendar(2005, Calendar.JANUARY, 1), "2004-W53-6"),
41              Arguments.of(new GregorianCalendar(2005, Calendar.JANUARY, 2), "2004-W53-7"),
42              Arguments.of(new GregorianCalendar(2005, Calendar.DECEMBER, 31), "2005-W52-6"),
43              Arguments.of(new GregorianCalendar(2007, Calendar.JANUARY, 1), "2007-W01-1"),
44              Arguments.of(new GregorianCalendar(2007, Calendar.DECEMBER, 30), "2007-W52-7"),
45              Arguments.of(new GregorianCalendar(2007, Calendar.DECEMBER, 31), "2008-W01-1"),
46              Arguments.of(new GregorianCalendar(2008, Calendar.JANUARY, 1), "2008-W01-2"),
47              Arguments.of(new GregorianCalendar(2008, Calendar.DECEMBER, 28), "2008-W52-7"),
48              Arguments.of(new GregorianCalendar(2008, Calendar.DECEMBER, 29), "2009-W01-1"),
49              Arguments.of(new GregorianCalendar(2008, Calendar.DECEMBER, 30), "2009-W01-2"),
50              Arguments.of(new GregorianCalendar(2008, Calendar.DECEMBER, 31), "2009-W01-3"),
51              Arguments.of(new GregorianCalendar(2009, Calendar.JANUARY, 1), "2009-W01-4"),
52              Arguments.of(new GregorianCalendar(2009, Calendar.DECEMBER, 31), "2009-W53-4"),
53              Arguments.of(new GregorianCalendar(2010, Calendar.JANUARY, 1), "2009-W53-5"),
54              Arguments.of(new GregorianCalendar(2010, Calendar.JANUARY, 2), "2009-W53-6"),
55              Arguments.of(new GregorianCalendar(2010, Calendar.JANUARY, 3), "2009-W53-7")
56          );
57          // @formatter:on
58      }
59  
60      @ParameterizedTest
61      @MethodSource("data")
62      @ReadsDefaultLocale
63      @ReadsDefaultTimeZone
64      void testParser(final Calendar vulgar, final String isoForm) {
65          final DateParser parser = new FastDateParser("YYYY-'W'ww-u", TimeZone.getDefault(), Locale.getDefault());
66  
67          final Calendar cal = Calendar.getInstance();
68          cal.setMinimalDaysInFirstWeek(4);
69          cal.setFirstDayOfWeek(Calendar.MONDAY);
70          cal.clear();
71  
72          parser.parse(isoForm, new ParsePosition(0), cal);
73          assertEquals(vulgar.getTime(), cal.getTime());
74      }
75  
76      @ParameterizedTest
77      @MethodSource("data")
78      @ReadsDefaultLocale
79      @ReadsDefaultTimeZone
80      void testPrinter(final Calendar vulgar, final String isoForm) {
81          final FastDatePrinter printer = new FastDatePrinter("YYYY-'W'ww-u", TimeZone.getDefault(), Locale.getDefault());
82  
83          vulgar.setMinimalDaysInFirstWeek(4);
84          vulgar.setFirstDayOfWeek(Calendar.MONDAY);
85  
86          assertEquals(isoForm, printer.format(vulgar));
87      }
88  }