Debian Codegraph @

src/test/java/ezvcard/property/asserter/VCardAsserter.java

raw symbols SCIP
1 package ezvcard.property.asserter;
2
3 import static ezvcard.util.TestUtils.assertParseWarnings;
4 import static ezvcard.util.TestUtils.assertValidate;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertNull;
7
8 import java.io.IOException;
9
10 import ezvcard.VCard;
11 import ezvcard.VCardVersion;
12 import ezvcard.io.StreamReader;
13 import ezvcard.property.Address;
14 import ezvcard.property.BinaryProperty;
15 import ezvcard.property.DateOrTimeProperty;
16 import ezvcard.property.Email;
17 import ezvcard.property.Geo;
18 import ezvcard.property.Impp;
19 import ezvcard.property.ListProperty;
20 import ezvcard.property.RawProperty;
21 import ezvcard.property.SimpleProperty;
22 import ezvcard.property.StructuredName;
23 import ezvcard.property.Telephone;
24 import ezvcard.property.Timezone;
25 import ezvcard.property.VCardProperty;
26 import ezvcard.property.Xml;
27 import ezvcard.util.TestUtils.VCardValidateChecker;
28
29 /*
30 Copyright (c) 2012-2023, Michael Angstadt
31 All rights reserved.
32
33 Redistribution and use in source and binary forms, with or without
34 modification, are permitted provided that the following conditions are met:
35
36 1. Redistributions of source code must retain the above copyright notice, this
37 list of conditions and the following disclaimer.
38 2. Redistributions in binary form must reproduce the above copyright notice,
39 this list of conditions and the following disclaimer in the documentation
40 and/or other materials provided with the distribution.
41
42 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
43 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
46 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
51 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52
53 The views and conclusions contained in the software and documentation are those
54 of the authors and should not be interpreted as representing official policies,
55 either expressed or implied, of the FreeBSD Project.
56 */
57
58 /**
59 * Helper class used to assert the contents of the {@link VCard} objects
60 * produced by a {@link StreamReader}.
61 * @author Michael Angstadt
62 */
63 public class VCardAsserter {
64 private final StreamReader reader;
65
66 private VCard vcard;
67 private boolean first = true;
68 private boolean warningsChecked = false;
69 private int propertiesChecked;
70
71 public VCardAsserter(StreamReader reader) {
72 this.reader = reader;
73 }
74
75 /**
76 * Reads the next vCard object. Also checks to make sure all the properties
77 * from the previous vCard were verified.
78 * @param expectedVersion the expected version of the next vCard
79 * @return the vCard
80 * @throws IOException if there's a problem reading from the data stream
81 */
82 public VCard next(VCardVersion expectedVersion) throws IOException {
83 if (first) {
84 first = false;
85 } else {
86 if (!warningsChecked) {
87 warnings();
88 }
89
90 int total = vcard.getProperties().size();
91 assertEquals("The vCard has " + total + " properties, but only " + propertiesChecked + " were checked.", propertiesChecked, total);
92 }
93
94 vcard = reader.readNext();
95 propertiesChecked = 0;
96 warningsChecked = false;
97
98 if (vcard != null) {
99 assertEquals(expectedVersion, vcard.getVersion());
100 }
101
102 return vcard;
103 }
104
105 /**
106 * Gets the current vCard.
107 * @return the vCard
108 */
109 public VCard getVCard() {
110 return vcard;
111 }
112
113 /**
114 * Asserts the contents of the vCard's properties that match the given
115 * class.
116 * @param clazz the property class
117 * @return the asserter object
118 */
119 public <T extends VCardProperty> PropertyImplAsserterImpl<T> property(Class<T> clazz) {
120 return new PropertyImplAsserterImpl<>(vcard.getProperties(clazz), this);
121 }
122
123 /**
124 * Asserts the contents of the vCard's properties that match the given
125 * class.
126 * @param clazz the property class (must extend {@link SimpleProperty})
127 * @return the asserter object
128 */
129 @SuppressWarnings("rawtypes")
130 public <T extends SimpleProperty> SimplePropertyAsserter<T> simpleProperty(Class<T> clazz) {
131 return new SimplePropertyAsserter<>(vcard.getProperties(clazz), this);
132 }
133
134 /**
135 * Asserts the contents of the vCard's properties that match the given
136 * class.
137 * @param clazz the property class (must extend {@link ListProperty})
138 * @return the asserter object
139 */
140 public <T extends ListProperty<String>> ListPropertyAsserter<T> listProperty(Class<T> clazz) {
141 return new ListPropertyAsserter<>(vcard.getProperties(clazz), this);
142 }
143
144 /**
145 * Asserts the contents of the vCard's properties that match the given
146 * class.
147 * @param clazz the property class (must extend {@link DateOrTimeProperty})
148 * @return the asserter object
149 */
150 public <T extends DateOrTimeProperty> DateOrTimePropertyAsserter<T> dateProperty(Class<T> clazz) {
151 return new DateOrTimePropertyAsserter<>(vcard.getProperties(clazz), this);
152 }
153
154 /**
155 * Asserts the contents of the vCard's properties that match the given
156 * class.
157 * @param clazz the property class (must extend {@link BinaryProperty})
158 * @return the asserter object
159 */
160 @SuppressWarnings("rawtypes")
161 public <T extends BinaryProperty> BinaryPropertyAsserter<T> binaryProperty(Class<T> clazz) {
162 return new BinaryPropertyAsserter<>(vcard.getProperties(clazz), this);
163 }
164
165 /**
166 * Asserts the contents of the vCard's {@link Geo} properties.
167 * @return the asserter object
168 */
169 public GeoAsserter geo() {
170 return new GeoAsserter(vcard.getGeos(), this);
171 }
172
173 /**
174 * Asserts the contents of the vCard's {@link Timezone} properties.
175 * @return the asserter object
176 */
177 public TimezoneAsserter timezone() {
178 return new TimezoneAsserter(vcard.getTimezones(), this);
179 }
180
181 /**
182 * Asserts the contents of the vCard's {@link Email} properties.
183 * @return the asserter object
184 */
185 public EmailAsserter email() {
186 return new EmailAsserter(vcard.getEmails(), this);
187 }
188
189 /**
190 * Asserts the contents of the vCard's {@link Impp} properties.
191 * @return the asserter object
192 */
193 public ImppAsserter impp() {
194 return new ImppAsserter(vcard.getImpps(), this);
195 }
196
197 /**
198 * Asserts the contents of the vCard's {@link Address} properties.
199 * @return the asserter object
200 */
201 public AddressAsserter address() {
202 return new AddressAsserter(vcard.getAddresses(), this);
203 }
204
205 /**
206 * Asserts the contents of the vCard's {@link Telephone} properties.
207 * @return the asserter object
208 */
209 public TelephoneAsserter telephone() {
210 return new TelephoneAsserter(vcard.getTelephoneNumbers(), this);
211 }
212
213 /**
214 * Asserts the contents of the vCard's {@link StructuredName} properties.
215 * @return the asserter object
216 */
217 public StructuredNameAsserter structuredName() {
218 return new StructuredNameAsserter(vcard.getStructuredNames(), this);
219 }
220
221 /**
222 * Asserts the contents of the vCard's {@link Xml} properties.
223 * @return the asserter object
224 */
225 public XmlAsserter xml() {
226 return new XmlAsserter(vcard.getXmls(), this);
227 }
228
229 /**
230 * Asserts the contents of the vCard's {@link RawProperty} properties.
231 * @param name the property name
232 * @return the asserter object
233 */
234 public RawPropertyAsserter rawProperty(String name) {
235 return new RawPropertyAsserter(vcard.getExtendedProperties(name), name, this);
236 }
237
238 public void warnings(Integer... expected) {
239 assertParseWarnings(reader, expected);
240 warningsChecked = true;
241 }
242
243 public VCardValidateChecker validate() {
244 return assertValidate(vcard).versions(vcard.getVersion());
245 }
246
247 /**
248 * Asserts that there are no more vCards in the data stream.
249 * @throws IOException if there's a problem reading from the data stream
250 */
251 public void done() throws IOException {
252 next(null);
253 assertNull(vcard);
254 reader.close();
255 }
256
257 void incPropertiesChecked() {
258 propertiesChecked++;
259 }
260 }