Debian Codegraph @

src/test/java/ezvcard/property/DateOrTimePropertyTest.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.LocalDate;
12 import java.time.temporal.Temporal;
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Test;
17
18 import ezvcard.VCardVersion;
19 import ezvcard.util.PartialDate;
20
21 /*
22 Copyright (c) 2012-2023, Michael Angstadt
23 All rights reserved.
24
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions are met:
27
28 1. Redistributions of source code must retain the above copyright notice, this
29 list of conditions and the following disclaimer.
30 2. Redistributions in binary form must reproduce the above copyright notice,
31 this list of conditions and the following disclaimer in the documentation
32 and/or other materials provided with the distribution.
33
34 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
35 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
38 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45 The views and conclusions contained in the software and documentation are those
46 of the authors and should not be interpreted as representing official policies,
47 either expressed or implied, of the FreeBSD Project.
48 */
49
50 /**
51 * @author Michael Angstadt
52 */
53 public class DateOrTimePropertyTest {
54 @Test
55 public void constructors() throws Exception {
56 DateOrTimePropertyImpl property = new DateOrTimePropertyImpl();
57 assertNull(property.getDate());
58 assertNull(property.getPartialDate());
59 assertNull(property.getText());
60
61 LocalDate date = LocalDate.of(2016, 1, 14);
62 property = new DateOrTimePropertyImpl(date);
63 assertEquals(date, property.getDate());
64 assertNull(property.getPartialDate());
65 assertNull(property.getText());
66
67 PartialDate partialDate = new PartialDate.Builder().month(1).date(14).build();
68 property = new DateOrTimePropertyImpl(partialDate);
69 assertNull(property.getDate());
70 assertEquals(partialDate, property.getPartialDate());
71 assertNull(property.getText());
72
73 String text = "text";
74 property = new DateOrTimePropertyImpl(text);
75 assertNull(property.getDate());
76 assertNull(property.getPartialDate());
77 assertEquals(text, property.getText());
78 }
79
80 @Test
81 public void set_value() {
82 DateOrTimePropertyImpl property = new DateOrTimePropertyImpl();
83
84 LocalDate date = LocalDate.of(2016, 1, 14);
85 property.setDate(date);
86 assertEquals(date, property.getDate());
87 assertNull(property.getPartialDate());
88 assertNull(property.getText());
89
90 property.setDate(null);
91 assertNull(property.getDate());
92 assertNull(property.getPartialDate());
93 assertNull(property.getText());
94
95 PartialDate partialDate = new PartialDate.Builder().month(1).date(14).build();
96 property.setPartialDate(partialDate);
97 assertNull(property.getDate());
98 assertEquals(partialDate, property.getPartialDate());
99 assertNull(property.getText());
100
101 String text = "text";
102 property.setText(text);
103 assertNull(property.getDate());
104 assertNull(property.getPartialDate());
105 assertEquals(text, property.getText());
106
107 property.setDate(date);
108 assertEquals(date, property.getDate());
109 assertNull(property.getPartialDate());
110 assertNull(property.getText());
111
112 property.setPartialDate(null);
113 assertNull(property.getDate());
114 assertNull(property.getPartialDate());
115 assertNull(property.getText());
116 }
117
118 @Test
119 public void validate() {
120 DateOrTimePropertyImpl empty = new DateOrTimePropertyImpl();
121 assertValidate(empty).run(8);
122
123 DateOrTimePropertyImpl withDate = new DateOrTimePropertyImpl();
124 LocalDate date = LocalDate.of(1980, 6, 5);
125 withDate.setDate(date);
126 assertValidate(withDate).run();
127
128 DateOrTimePropertyImpl withPartialDate = new DateOrTimePropertyImpl();
129 withPartialDate.setPartialDate(PartialDate.builder().month(6).date(5).build());
130 assertValidate(withPartialDate).versions(VCardVersion.V2_1, VCardVersion.V3_0).run(12);
131 assertValidate(withPartialDate).versions(VCardVersion.V4_0).run();
132
133 DateOrTimePropertyImpl withText = new DateOrTimePropertyImpl();
134 withText.setText("text");
135 assertValidate(withText).versions(VCardVersion.V2_1, VCardVersion.V3_0).run(11);
136 assertValidate(withText).versions(VCardVersion.V4_0).run();
137 }
138
139 @Test
140 public void toStringValues() {
141 DateOrTimePropertyImpl property = new DateOrTimePropertyImpl();
142 assertFalse(property.toStringValues().isEmpty());
143 }
144
145 @Test
146 public void copy() {
147 DateOrTimePropertyImpl original = new DateOrTimePropertyImpl();
148 assertCopy(original);
149
150 original = new DateOrTimePropertyImpl();
151 original.setDate(LocalDate.now());
152 assertCopy(original);
153
154 original = new DateOrTimePropertyImpl();
155 original.setPartialDate(new PartialDate.Builder().month(1).date(14).build());
156 assertCopy(original);
157 }
158
159 @Test
160 public void equals() {
161 LocalDate date1 = LocalDate.of(2016, 1, 14);
162 LocalDate date2 = LocalDate.of(2016, 1, 15);
163 List<VCardProperty> properties = new ArrayList<>();
164
165 DateOrTimePropertyImpl property = new DateOrTimePropertyImpl();
166 properties.add(property);
167
168 property = new DateOrTimePropertyImpl();
169 property.setDate(date1);
170 properties.add(property);
171
172 property = new DateOrTimePropertyImpl();
173 property.setDate(date2);
174 properties.add(property);
175
176 property = new DateOrTimePropertyImpl();
177 property.setPartialDate(new PartialDate.Builder().month(1).date(14).build());
178 properties.add(property);
179
180 property = new DateOrTimePropertyImpl();
181 property.setPartialDate(new PartialDate.Builder().month(1).date(15).build());
182 properties.add(property);
183
184 property = new DateOrTimePropertyImpl();
185 property.setText("text");
186 properties.add(property);
187
188 property = new DateOrTimePropertyImpl();
189 property.setText("text2");
190 properties.add(property);
191
192 assertNothingIsEqual(properties);
193
194 //@formatter:off
195 assertEqualsMethod(DateOrTimePropertyImpl.class)
196 .constructor().test()
197 .constructor(date1).test()
198 .constructor(new PartialDate.Builder().year(2016).month(1).date(14).build()).test()
199 .constructor("text").test();
200 //@formatter:on
201 }
202
203 public static class DateOrTimePropertyImpl extends DateOrTimeProperty {
204 public DateOrTimePropertyImpl() {
205 super((Temporal) null);
206 }
207
208 public DateOrTimePropertyImpl(LocalDate date) {
209 super(date);
210 }
211
212 public DateOrTimePropertyImpl(PartialDate partialDate) {
213 super(partialDate);
214 }
215
216 public DateOrTimePropertyImpl(String text) {
217 super(text);
218 }
219
220 public DateOrTimePropertyImpl(DateOrTimePropertyImpl original) {
221 super(original);
222 }
223 }
224 }