Debian Codegraph @

src/test/java/ezvcard/property/TimezoneTest.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import static ezvcard.property.PropertySensei.assertCopy;
4 import static ezvcard.property.PropertySensei.assertEqualsMethod;
5 import static ezvcard.property.PropertySensei.assertNothingIsEqual;
6 import static ezvcard.property.PropertySensei.assertValidate;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNull;
10
11 import java.time.Instant;
12 import java.time.ZoneId;
13 import java.time.ZoneOffset;
14
15 import org.junit.Test;
16
17 import ezvcard.VCardVersion;
18
19 /*
20 Copyright (c) 2012-2023, Michael Angstadt
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions are met:
25
26 1. Redistributions of source code must retain the above copyright notice, this
27 list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright notice,
29 this list of conditions and the following disclaimer in the documentation
30 and/or other materials provided with the distribution.
31
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
36 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 The views and conclusions contained in the software and documentation are those
44 of the authors and should not be interpreted as representing official policies,
45 either expressed or implied, of the FreeBSD Project.
46 */
47
48 /**
49 * @author Michael Angstadt
50 */
51 public class TimezoneTest {
52 @Test
53 public void constructors() throws Exception {
54 Timezone property = new Timezone((String) null);
55 assertNull(property.getOffset());
56 assertNull(property.getText());
57
58 property = new Timezone(ZoneOffset.ofHours(1));
59 assertEquals(ZoneOffset.ofHours(1), property.getOffset());
60 assertNull(property.getText());
61
62 property = new Timezone(ZoneId.ofOffset("GMT", ZoneOffset.ofHours(1)));
63 assertEquals(ZoneOffset.ofHours(1), property.getOffset());
64 assertEquals("GMT+01:00", property.getText());
65
66 property = new Timezone("text");
67 assertNull(property.getOffset());
68 assertEquals("text", property.getText());
69
70 property = new Timezone(ZoneOffset.ofHours(1), "text");
71 assertEquals(ZoneOffset.ofHours(1), property.getOffset());
72 assertEquals("text", property.getText());
73 }
74
75 @Test
76 public void set_value() {
77 Timezone property = new Timezone((String) null);
78
79 property.setOffset(ZoneOffset.ofHours(1));
80 assertEquals(ZoneOffset.ofHours(1), property.getOffset());
81 assertNull(property.getText());
82
83 property.setText("text");
84 assertEquals(ZoneOffset.ofHours(1), property.getOffset());
85 assertEquals("text", property.getText());
86 }
87
88 @Test
89 public void validate() {
90 Timezone empty = new Timezone((ZoneOffset) null);
91 assertValidate(empty).versions(VCardVersion.V2_1).run(8, 20);
92 assertValidate(empty).versions(VCardVersion.V3_0).run(8);
93 assertValidate(empty).versions(VCardVersion.V4_0).run(8);
94
95 Timezone withOffset = new Timezone(ZoneOffset.ofHoursMinutes(-5, -30));
96 assertValidate(withOffset).run();
97
98 Timezone withText = new Timezone("America/New_York");
99 assertValidate(withText).versions(VCardVersion.V2_1).run(20);
100 assertValidate(withText).versions(VCardVersion.V3_0).run();
101 assertValidate(withText).versions(VCardVersion.V4_0).run();
102 }
103
104 @Test
105 public void toTimeZone_offset() {
106 Timezone t = new Timezone(ZoneOffset.ofHoursMinutes(-5, -30));
107 ZoneId actual = t.toTimeZone();
108 assertEquals(ZoneOffset.ofHoursMinutes(-5, -30), actual);
109 }
110
111 @Test
112 public void toTimeZone_timezone_id() {
113 Timezone t = new Timezone("America/New_York");
114 ZoneId actual = t.toTimeZone();
115 assertEquals(ZoneId.of("America/New_York"), actual);
116 }
117
118 @Test
119 public void toTimeZone_text() {
120 Timezone t = new Timezone("text");
121 ZoneId actual = t.toTimeZone();
122 assertNull(actual);
123 }
124
125 @Test
126 public void toTimeZone_text_and_offset() {
127 Timezone t = new Timezone(ZoneOffset.ofHoursMinutes(-5, -30), "text");
128 ZoneId actual = t.toTimeZone();
129
130 assertEquals(ZoneOffset.ofHoursMinutes(-5, -30), actual.getRules().getOffset(Instant.now()));
131 assertEquals("-05:30", actual.getId());
132 }
133
134 @Test
135 public void toStringValues() {
136 Timezone property = new Timezone("text");
137 assertFalse(property.toStringValues().isEmpty());
138 }
139
140 @Test
141 public void copy() {
142 Timezone original = new Timezone((String) null);
143 assertCopy(original);
144
145 original = new Timezone("text");
146 assertCopy(original);
147
148 original = new Timezone(ZoneOffset.ofHours(1));
149 assertCopy(original);
150 }
151
152 @Test
153 public void equals() {
154 //@formatter:off
155 assertNothingIsEqual(
156 new Timezone((String) null),
157 new Timezone("text"),
158 new Timezone("text2"),
159 new Timezone(ZoneOffset.ofHours(1)),
160 new Timezone(ZoneOffset.ofHours(2)),
161 new Timezone(ZoneOffset.ofHours(1), "text")
162 );
163
164 assertEqualsMethod(Timezone.class, "text")
165 .constructor(new Class<?>[]{String.class}, (String)null).test()
166 .constructor("text").test()
167 .constructor(ZoneOffset.ofHours(1)).test()
168 .constructor(ZoneOffset.ofHours(1), "text").test();
169 //@formatter:on
170 }
171 }