Debian Codegraph @

src/test/java/ezvcard/property/TelephoneTest.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.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.junit.Test;
16
17 import ezvcard.VCardVersion;
18 import ezvcard.parameter.TelephoneType;
19 import ezvcard.util.TelUri;
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 TelephoneTest {
54 @Test
55 public void constructors() throws Exception {
56 Telephone property = new Telephone((String) null);
57 assertNull(property.getText());
58 assertNull(property.getUri());
59 assertEquals(Arrays.asList(), property.getTypes());
60
61 property = new Telephone("text");
62 assertEquals("text", property.getText());
63 assertNull(property.getUri());
64 assertEquals(Arrays.asList(), property.getTypes());
65
66 property = new Telephone(new TelUri.Builder("+1").build());
67 assertNull(property.getText());
68 assertEquals(new TelUri.Builder("+1").build(), property.getUri());
69 assertEquals(Arrays.asList(), property.getTypes());
70 }
71
72 @Test
73 public void set_value() {
74 Telephone property = new Telephone((String) null);
75
76 property.setText("text");
77 assertEquals("text", property.getText());
78 assertNull(property.getUri());
79 assertEquals(Arrays.asList(), property.getTypes());
80
81 property.setUri(new TelUri.Builder("+1").build());
82 assertNull(property.getText());
83 assertEquals(new TelUri.Builder("+1").build(), property.getUri());
84 assertEquals(Arrays.asList(), property.getTypes());
85
86 property.setText("text");
87 assertEquals("text", property.getText());
88 assertNull(property.getUri());
89 assertEquals(Arrays.asList(), property.getTypes());
90
91 property.getTypes().add(TelephoneType.WORK);
92 assertEquals("text", property.getText());
93 assertNull(property.getUri());
94 assertEquals(Arrays.asList(TelephoneType.WORK), property.getTypes());
95
96 property.getTypes().add(TelephoneType.HOME);
97 assertEquals("text", property.getText());
98 assertNull(property.getUri());
99 assertEquals(Arrays.asList(TelephoneType.WORK, TelephoneType.HOME), property.getTypes());
100
101 property.getTypes().remove(TelephoneType.HOME);
102 assertEquals("text", property.getText());
103 assertNull(property.getUri());
104 assertEquals(Arrays.asList(TelephoneType.WORK), property.getTypes());
105 }
106
107 @Test
108 public void validate() {
109 Telephone empty = new Telephone((String) null);
110 assertValidate(empty).run(8);
111
112 Telephone withText = new Telephone("(800) 555-5555");
113 assertValidate(withText).run();
114
115 Telephone withUri = new Telephone(new TelUri.Builder("+1-800-555-5555").extension("101").build());
116 assertValidate(withUri).versions(VCardVersion.V2_1).run(19);
117 assertValidate(withUri).versions(VCardVersion.V3_0).run(19);
118 assertValidate(withUri).versions(VCardVersion.V4_0).run();
119
120 Telephone withTypes = new Telephone("(800) 555-5555");
121 withTypes.getTypes().add(TelephoneType.TEXTPHONE);
122 withTypes.getTypes().add(TelephoneType.PREF);
123 assertValidate(withTypes).versions(VCardVersion.V2_1, VCardVersion.V3_0).run(9);
124 assertValidate(withTypes).versions(VCardVersion.V4_0).run();
125 }
126
127 @Test
128 public void toStringValues() {
129 Telephone property = new Telephone("text");
130 assertFalse(property.toStringValues().isEmpty());
131 }
132
133 @Test
134 public void copy() {
135 Telephone original = new Telephone((String) null);
136 assertCopy(original);
137
138 original = new Telephone("text");
139 assertCopy(original);
140
141 original = new Telephone(new TelUri.Builder("+1").build());
142 assertCopy(original);
143
144 original = new Telephone("text");
145 original.getTypes().add(TelephoneType.HOME);
146 assertCopy(original);
147 }
148
149 @Test
150 public void equals() {
151 List<VCardProperty> properties = new ArrayList<>();
152
153 Telephone property = new Telephone((String) null);
154 properties.add(property);
155
156 property = new Telephone("text");
157 properties.add(property);
158
159 property = new Telephone("text");
160 property.getTypes().add(TelephoneType.HOME);
161 properties.add(property);
162
163 property = new Telephone("text2");
164 properties.add(property);
165
166 property = new Telephone(new TelUri.Builder("+1").build());
167 properties.add(property);
168
169 property = new Telephone(new TelUri.Builder("+1").build());
170 property.getTypes().add(TelephoneType.HOME);
171 properties.add(property);
172
173 property = new Telephone(new TelUri.Builder("+2").build());
174 properties.add(property);
175
176 assertNothingIsEqual(properties);
177
178 //@formatter:off
179 assertEqualsMethod(Telephone.class, "text")
180 .constructor(new Class<?>[]{String.class}, (String)null).test()
181 .constructor("text").test()
182 .constructor(new TelUri.Builder("+1").build()).test();
183 //@formatter:on
184 }
185 }