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 TemporalDateSwap_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 Date T_Date = Date.from(Instant.from(DateTimeFormatter.ISO_INSTANT.parse("2012-12-21T12:34:56Z")));
47  
48  	private static <T> StringSwap_Tester<T> tester(int index, String label, T object, StringSwap<T> swap, String expected, BeanSession bs) {
49  		return StringSwap_Tester.create(index, label, object, swap, expected, bs).build();
50  	}
51  
52  	private static final StringSwap_Tester<?>[] TESTERS = {
53  		tester(1, "BasicIsoDate, Default timezone", T_Date, new TemporalDateSwap.BasicIsoDate(), "20121221-0500", BS_DEFAULT),
54  		tester(2, "BasicIsoDate, Pacific timezone", T_Date, new TemporalDateSwap.BasicIsoDate(), "20121221-0800", BS_PST),
55  		tester(3, "IsoDate, Default timezone", T_Date, new TemporalDateSwap.IsoDate(), "2012-12-21-05:00", BS_DEFAULT),
56  		tester(4, "IsoDate, Pacific timezone", T_Date, new TemporalDateSwap.IsoDate(), "2012-12-21-08:00", BS_PST),
57  		tester(5, "IsoDateTime, Default timezone", T_Date, new TemporalDateSwap.IsoDateTime(), "2012-12-21T07:34:56-05:00[GMT-05:00]", BS_DEFAULT),
58  		tester(6, "IsoDateTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoDateTime(), "2012-12-21T04:34:56-08:00[America/Los_Angeles]", BS_PST),
59  		tester(7, "IsoInstant, Default timezone", T_Date, new TemporalDateSwap.IsoInstant(), "2012-12-21T12:34:56Z", BS_DEFAULT),
60  		tester(8, "IsoInstant, Pacific timezone", T_Date, new TemporalDateSwap.IsoInstant(), "2012-12-21T12:34:56Z", BS_PST),
61  		tester(9, "IsoLocalDate, Default timezone", T_Date, new TemporalDateSwap.IsoLocalDate(), "2012-12-21", BS_DEFAULT),
62  		tester(10, "IsoLocalDate, Pacific timezone", T_Date, new TemporalDateSwap.IsoLocalDate(), "2012-12-21", BS_PST),
63  		tester(11, "IsoLocalDateTime, Default timezone", T_Date, new TemporalDateSwap.IsoLocalDateTime(), "2012-12-21T07:34:56", BS_DEFAULT),
64  		tester(12, "IsoLocalDateTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoLocalDateTime(), "2012-12-21T04:34:56", BS_PST),
65  		tester(13, "IsoLocalTime, Default timezone", T_Date, new TemporalDateSwap.IsoLocalTime(), "07:34:56", BS_DEFAULT),
66  		tester(14, "IsoLocalTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoLocalTime(), "04:34:56", BS_PST),
67  		tester(15, "IsoOffsetDate, Default timezone", T_Date, new TemporalDateSwap.IsoOffsetDate(), "2012-12-21-05:00", BS_DEFAULT),
68  		tester(16, "IsoOffsetDate, Pacific timezone", T_Date, new TemporalDateSwap.IsoOffsetDate(), "2012-12-21-08:00", BS_PST),
69  		tester(17, "IsoOffsetDateTime, Default timezone", T_Date, new TemporalDateSwap.IsoOffsetDateTime(), "2012-12-21T07:34:56-05:00", BS_DEFAULT),
70  		tester(18, "IsoOffsetDateTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoOffsetDateTime(), "2012-12-21T04:34:56-08:00", BS_PST),
71  		tester(19, "IsoOffsetTime, Default timezone", T_Date, new TemporalDateSwap.IsoOffsetTime(), "07:34:56-05:00", BS_DEFAULT),
72  		tester(20, "IsoOffsetTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoOffsetTime(), "04:34:56-08:00", BS_PST),
73  		tester(21, "IsoOrdinalDate, Default timezone", T_Date, new TemporalDateSwap.IsoOrdinalDate(), "2012-356-05:00", BS_DEFAULT),
74  		tester(22, "IsoOrdinalDate, Pacific timezone", T_Date, new TemporalDateSwap.IsoOrdinalDate(), "2012-356-08:00", BS_PST),
75  		tester(23, "IsoTime, Default timezone", T_Date, new TemporalDateSwap.IsoTime(), "07:34:56-05:00", BS_DEFAULT),
76  		tester(24, "IsoTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoTime(), "04:34:56-08:00", BS_PST),
77  		tester(25, "IsoWeekDate, Default timezone", T_Date, new TemporalDateSwap.IsoWeekDate(), "2012-W51-5-05:00", BS_DEFAULT),
78  		tester(26, "IsoWeekDate, Pacific timezone", T_Date, new TemporalDateSwap.IsoWeekDate(), "2012-W51-5-08:00", BS_PST),
79  		tester(27, "IsoZonedDateTime, Default timezone", T_Date, new TemporalDateSwap.IsoZonedDateTime(), "2012-12-21T07:34:56-05:00[GMT-05:00]", BS_DEFAULT),
80  		tester(28, "IsoZonedDateTime, Pacific timezone", T_Date, new TemporalDateSwap.IsoZonedDateTime(), "2012-12-21T04:34:56-08:00[America/Los_Angeles]", BS_PST),
81  		tester(29, "Rfc1123DateTime, Default timezone", T_Date, new TemporalDateSwap.Rfc1123DateTime(), "Fri, 21 Dec 2012 07:34:56 -0500", BS_DEFAULT),
82  		tester(30, "Rfc1123DateTime, Pacific timezone", T_Date, new TemporalDateSwap.Rfc1123DateTime(), "Fri, 21 Dec 2012 04:34:56 -0800", BS_PST),
83  		tester(31, "YEAR, Default timezone", T_Date, new TemporalDateSwap("uuuu"), "2012", BS_DEFAULT),
84  		tester(32, "YEAR, Pacific timezone", T_Date, new TemporalDateSwap("uuuu"), "2012", BS_DEFAULT),
85  		tester(33, "BasicIsoDate, null", null, new TemporalDateSwap.BasicIsoDate(), null, BS_DEFAULT)
86  	};
87  
88  	static StringSwap_Tester<?>[] testers() {
89  		return TESTERS;
90  	}
91  }