Debian Codegraph @

src/test/java/ezvcard/property/VCardPropertyTest.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import static ezvcard.VCardVersion.V2_1;
4 import static ezvcard.VCardVersion.V3_0;
5 import static ezvcard.VCardVersion.V4_0;
6 import static ezvcard.property.PropertySensei.assertEqualsMethod;
7 import static ezvcard.property.PropertySensei.assertNothingIsEqual;
8 import static ezvcard.property.PropertySensei.assertValidate;
9 import static ezvcard.util.TestUtils.assertEqualsAndHash;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotSame;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.mockito.Matchers.any;
19 import static org.mockito.Matchers.anyList;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.verify;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.Arrays;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29 import org.junit.Test;
30
31 import ezvcard.SupportedVersions;
32 import ezvcard.VCard;
33 import ezvcard.VCardVersion;
34 import ezvcard.parameter.VCardParameters;
35
36 /*
37 Copyright (c) 2012-2023, Michael Angstadt
38 All rights reserved.
39
40 Redistribution and use in source and binary forms, with or without
41 modification, are permitted provided that the following conditions are met:
42
43 1. Redistributions of source code must retain the above copyright notice, this
44 list of conditions and the following disclaimer.
45 2. Redistributions in binary form must reproduce the above copyright notice,
46 this list of conditions and the following disclaimer in the documentation
47 and/or other materials provided with the distribution.
48
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
50 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
53 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
60 The views and conclusions contained in the software and documentation are those
61 of the authors and should not be interpreted as representing official policies,
62 either expressed or implied, of the FreeBSD Project.
63 */
64
65 /**
66 * @author Michael Angstadt
67 */
68 public class VCardPropertyTest {
69 @SuppressWarnings("unchecked")
70 @Test
71 public void validate_overrideable_method_called() {
72 VCardPropertyImpl property = spy(new VCardPropertyImpl());
73 assertValidate(property).versions(V2_1).run();
74 verify(property)._validate(anyList(), eq(V2_1), any(VCard.class));
75 }
76
77 @Test
78 public void validate_unsupported_version() {
79 Version3Property property = new Version3Property();
80 assertValidate(property).versions(V2_1, V4_0).run(2);
81 assertValidate(property).versions(V3_0).run();
82 }
83
84 @Test
85 public void validate_valid_group() {
86 VCardPropertyImpl property = new VCardPropertyImpl();
87 property.setGroup("Group-1");
88 assertValidate(property).run();
89 }
90
91 @Test
92 public void validate_invalid_group() {
93 VCardPropertyImpl property = new VCardPropertyImpl();
94 property.setGroup("mr.smith");
95 assertValidate(property).versions(V2_1).run(32);
96 assertValidate(property).versions(V3_0, V4_0).run(23);
97 }
98
99 @Test
100 public void validate_parameters() {
101 VCardPropertyImpl property = new VCardPropertyImpl();
102 property.setParameter("ALTID", "1");
103 assertValidate(property).versions(V2_1, V3_0).run(6);
104 assertValidate(property).versions(V4_0).run();
105 }
106
107 @Test
108 public void getSupportedVersions() {
109 VCardPropertyImpl withoutSupportedVersions = new VCardPropertyImpl();
110 assertArrayEquals(VCardVersion.values(), withoutSupportedVersions.getSupportedVersions());
111
112 Version3Property withSupportedVersions = new Version3Property();
113 assertArrayEquals(new VCardVersion[] { V3_0 }, withSupportedVersions.getSupportedVersions());
114 }
115
116 @Test
117 public void isSupportedBy() {
118 VCardPropertyImpl withoutSupportedVersions = new VCardPropertyImpl();
119 for (VCardVersion version : VCardVersion.values()) {
120 assertTrue(withoutSupportedVersions.isSupportedBy(version));
121 }
122
123 Version3Property withSupportedVersions = new Version3Property();
124 assertFalse(withSupportedVersions.isSupportedBy(V2_1));
125 assertTrue(withSupportedVersions.isSupportedBy(V3_0));
126 assertFalse(withSupportedVersions.isSupportedBy(V4_0));
127 }
128
129 @Test
130 public void group() {
131 VCardPropertyImpl property = new VCardPropertyImpl();
132 assertNull(property.getGroup());
133
134 property.setGroup("group");
135 assertEquals("group", property.getGroup());
136 }
137
138 @Test
139 public void parameters() {
140 VCardPropertyImpl property = new VCardPropertyImpl();
141 assertEquals(new VCardParameters(), property.getParameters());
142
143 try {
144 property.setParameters(null);
145 fail("NPE expected.");
146 } catch (NullPointerException e) {
147 //expected
148 }
149
150 VCardParameters parameters = new VCardParameters();
151 property.setParameters(parameters);
152 assertSame(parameters, property.getParameters());
153
154 property.addParameter("PARAM", "value");
155 property.addParameter("PARAM", "value2");
156 assertEquals("value", property.getParameter("PARAM"));
157 assertEquals(Arrays.asList("value", "value2"), property.getParameters("PARAM"));
158 VCardParameters expected = new VCardParameters();
159 expected.put("PARAM", "value");
160 expected.put("PARAM", "value2");
161 assertEquals(expected, property.getParameters());
162
163 property.setParameter("PARAM", "one");
164 assertEquals("one", property.getParameter("PARAM"));
165 assertEquals(Arrays.asList("one"), property.getParameters("PARAM"));
166 expected = new VCardParameters();
167 expected.put("PARAM", "one");
168 assertEquals(expected, property.getParameters());
169
170 property.removeParameter("PARAM");
171 assertNull(property.getParameter("PARAM"));
172 assertEquals(Arrays.asList(), property.getParameters("PARAM"));
173 expected = new VCardParameters();
174 assertEquals(expected, property.getParameters());
175 }
176
177 @Test
178 public void compareTo() {
179 VCardPropertyImpl one = new VCardPropertyImpl();
180 one.getParameters().setPref(1);
181
182 VCardPropertyImpl two = new VCardPropertyImpl();
183 one.getParameters().setPref(2);
184
185 VCardPropertyImpl null1 = new VCardPropertyImpl();
186 VCardPropertyImpl null2 = new VCardPropertyImpl();
187
188 assertEquals(-1, one.compareTo(two));
189 assertEquals(1, two.compareTo(one));
190 assertEquals(0, one.compareTo(one));
191 assertEquals(-1, one.compareTo(null1));
192 assertEquals(1, null1.compareTo(one));
193 assertEquals(0, null1.compareTo(null2));
194 }
195
196 @Test
197 public void copy() {
198 CopyConstructorTest property = new CopyConstructorTest("value");
199 property.setGroup("group");
200 property.getParameters().setLanguage("en-us");
201 CopyConstructorTest copy = (CopyConstructorTest) property.copy();
202
203 assertNotSame(property, copy);
204 assertEquals(property.value, copy.value);
205
206 assertEquals(property.getGroup(), copy.getGroup());
207 assertNotSame(property.getParameters(), copy.getParameters());
208 assertEquals(property.getParameters(), copy.getParameters());
209 }
210
211 @Test
212 public void copy_constructor_throws_exception() {
213 RuntimeException exception = new RuntimeException();
214 CopyConstructorThrowsExceptionTest property = new CopyConstructorThrowsExceptionTest(exception);
215 try {
216 property.copy();
217 fail("Expected an exception to be thrown.");
218 } catch (UnsupportedOperationException e) {
219 assertTrue(e.getCause() instanceof InvocationTargetException);
220 assertSame(e.getCause().getCause(), exception);
221 }
222 }
223
224 @Test
225 public void copy_no_copy_constructor() {
226 VCardPropertyImpl property = new VCardPropertyImpl();
227 try {
228 property.copy();
229 fail("Expected an exception to be thrown.");
230 } catch (UnsupportedOperationException e) {
231 assertTrue(e.getCause() instanceof NoSuchMethodException);
232 }
233 }
234
235 @Test
236 public void toStringValues() {
237 VCardPropertyImpl property = new VCardPropertyImpl();
238 assertTrue(property.toStringValues().isEmpty());
239 }
240
241 @Test
242 public void toString_() {
243 VCardProperty property = new VCardPropertyImpl();
244 assertEquals(VCardPropertyImpl.class.getName() + " [ group=null | parameters={} ]", property.toString());
245
246 property = new CopyConstructorTest("text");
247 property.setGroup("group");
248 property.addParameter("PARAM", "value");
249 assertEquals(CopyConstructorTest.class.getName() + " [ group=group | parameters={PARAM=[value]} | value=text ]", property.toString());
250 }
251
252 @Test
253 public void equals() {
254 //@formatter:off
255 assertNothingIsEqual(new CopyConstructorTest(""));
256
257 assertEqualsMethod(VCardPropertyImpl.class)
258 .constructor();
259 //@formatter:on
260 }
261
262 @Test
263 public void equals_group_ignore_case() {
264 VCardPropertyImpl one = new VCardPropertyImpl();
265 one.setGroup("GROUP");
266 VCardPropertyImpl two = new VCardPropertyImpl();
267 two.setGroup("group");
268 assertEqualsAndHash(one, two);
269 }
270
271 private static class CopyConstructorTest extends VCardProperty {
272 private String value;
273
274 public CopyConstructorTest(String value) {
275 this.value = value;
276 }
277
278 @SuppressWarnings("unused")
279 public CopyConstructorTest(CopyConstructorTest original) {
280 super(original);
281 value = original.value;
282 }
283
284 @Override
285 protected Map<String, Object> toStringValues() {
286 Map<String, Object> values = new LinkedHashMap<>();
287 values.put("value", value);
288 return values;
289 }
290 }
291
292 private static class CopyConstructorThrowsExceptionTest extends VCardProperty {
293 private final RuntimeException e;
294
295 public CopyConstructorThrowsExceptionTest(RuntimeException e) {
296 this.e = e;
297 }
298
299 @SuppressWarnings("unused")
300 public CopyConstructorThrowsExceptionTest(CopyConstructorThrowsExceptionTest original) {
301 throw original.e;
302 }
303 }
304
305 public static class VCardPropertyImpl extends VCardProperty {
306 //empty
307 }
308
309 @SupportedVersions(V3_0)
310 private class Version3Property extends VCardProperty {
311 //empty
312 }
313 }