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    *      http://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.juneau.transforms;
18  
19  import static org.apache.juneau.TestUtils.*;
20  
21  import java.time.*;
22  import java.time.format.*;
23  import java.util.*;
24  
25  import org.apache.juneau.*;
26  import org.apache.juneau.swap.*;
27  import org.apache.juneau.swaps.*;
28  import org.junit.jupiter.api.*;
29  
30  class TemporalCalendarSwap_StringSwapTest extends StringSwapTest_Base {
31  
32  	@BeforeAll
33  	static void beforeClass() {
34  		setTimeZone("GMT-5");
35  	}
36  
37  	@AfterAll
38  	static void afterClass() {
39  		unsetTimeZone();
40  	}
41  
42  	private static BeanSession
43  		BS_DEFAULT = BeanContext.DEFAULT_SESSION,
44  		BS_PST = BeanContext.DEFAULT.createSession().timeZone(TimeZone.getTimeZone("PST")).build();
45  
46  	private static GregorianCalendar T_Calendar = GregorianCalendar.from(ZonedDateTime.from(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse("2012-12-21T12:34:56Z")));
47  	static {
48  		T_Calendar.setTimeZone(TimeZone.getTimeZone("GMT-5"));
49  	}
50  
51  	private static <T> StringSwap_Tester<T> tester(int index, String label, T object, StringSwap<T> swap, String expected, BeanSession bs) {
52  		return StringSwap_Tester.create(index, label, object, swap, expected, bs).build();
53  	}
54  
55  	private static final StringSwap_Tester<?>[] TESTERS = {
56  		tester(1, "BasicIsoDate, Default timezone", T_Calendar, new TemporalCalendarSwap.BasicIsoDate(), "20121221-0500", BS_DEFAULT),
57  		tester(2, "BasicIsoDate, Pacific timezone", T_Calendar, new TemporalCalendarSwap.BasicIsoDate(), "20121221-0500", BS_PST),
58  		tester(3, "IsoDate, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoDate(), "2012-12-21-05:00", BS_DEFAULT),
59  		tester(4, "IsoDate, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoDate(), "2012-12-21-05:00", BS_PST),
60  		tester(5, "IsoDateTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoDateTime(), "2012-12-21T07:34:56-05:00[GMT-05:00]", BS_DEFAULT),
61  		tester(6, "IsoDateTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoDateTime(), "2012-12-21T07:34:56-05:00[GMT-05:00]", BS_PST),
62  		tester(7, "IsoInstant, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoInstant(), "2012-12-21T12:34:56Z", BS_DEFAULT),
63  		tester(8, "IsoInstant, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoInstant(), "2012-12-21T12:34:56Z", BS_PST),
64  		tester(9, "IsoLocalDate, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoLocalDate(), "2012-12-21", BS_DEFAULT),
65  		tester(10, "IsoLocalDate, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoLocalDate(), "2012-12-21", BS_PST),
66  		tester(11, "IsoLocalDateTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoLocalDateTime(), "2012-12-21T07:34:56", BS_DEFAULT),
67  		tester(12, "IsoLocalDateTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoLocalDateTime(), "2012-12-21T07:34:56", BS_PST),
68  		tester(13, "IsoLocalTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoLocalTime(), "07:34:56", BS_DEFAULT),
69  		tester(14, "IsoLocalTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoLocalTime(), "07:34:56", BS_PST),
70  		tester(15, "IsoOffsetDate, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoOffsetDate(), "2012-12-21-05:00", BS_DEFAULT),
71  		tester(16, "IsoOffsetDate, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoOffsetDate(), "2012-12-21-05:00", BS_PST),
72  		tester(17, "IsoOffsetDateTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoOffsetDateTime(), "2012-12-21T07:34:56-05:00", BS_DEFAULT),
73  		tester(18, "IsoOffsetDateTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoOffsetDateTime(), "2012-12-21T07:34:56-05:00", BS_PST),
74  		tester(19, "IsoOffsetTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoOffsetTime(), "07:34:56-05:00", BS_DEFAULT),
75  		tester(20, "IsoOffsetTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoOffsetTime(), "07:34:56-05:00", BS_PST),
76  		tester(21, "IsoOrdinalDate, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoOrdinalDate(), "2012-356-05:00", BS_DEFAULT),
77  		tester(22, "IsoOrdinalDate, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoOrdinalDate(), "2012-356-05:00", BS_PST),
78  		tester(23, "IsoTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoTime(), "07:34:56-05:00", BS_DEFAULT),
79  		tester(24, "IsoTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoTime(), "07:34:56-05:00", BS_PST),
80  		tester(25, "IsoWeekDate, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoWeekDate(), "2012-W51-5-05:00", BS_DEFAULT),
81  		tester(26, "IsoWeekDate, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoWeekDate(), "2012-W51-5-05:00", BS_PST),
82  		tester(27, "IsoZonedDateTime, Default timezone", T_Calendar, new TemporalCalendarSwap.IsoZonedDateTime(), "2012-12-21T07:34:56-05:00[GMT-05:00]", BS_DEFAULT),
83  		tester(28, "IsoZonedDateTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.IsoZonedDateTime(), "2012-12-21T07:34:56-05:00[GMT-05:00]", BS_PST),
84  		tester(29, "Rfc1123DateTime, Default timezone", T_Calendar, new TemporalCalendarSwap.Rfc1123DateTime(), "Fri, 21 Dec 2012 07:34:56 -0500", BS_DEFAULT),
85  		tester(30, "Rfc1123DateTime, Pacific timezone", T_Calendar, new TemporalCalendarSwap.Rfc1123DateTime(), "Fri, 21 Dec 2012 07:34:56 -0500", BS_PST),
86  		tester(31, "YEAR, Default timezone", T_Calendar, new TemporalCalendarSwap("uuuu"), "2012", BS_DEFAULT),
87  		tester(32, "YEAR, Pacific timezone", T_Calendar, new TemporalCalendarSwap("uuuu"), "2012", BS_DEFAULT),
88  		tester(33, "BasicIsoDate, null", null, new TemporalCalendarSwap.BasicIsoDate(), null, BS_DEFAULT)
89  	};
90  
91  	static StringSwap_Tester<?>[] testers() {
92  		return TESTERS;
93  	}
94  }