| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
public VCard getVCard() { |
| 110 |
return vcard; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public <T extends VCardProperty> PropertyImplAsserterImpl<T> property(Class<T> clazz) { |
| 120 |
return new PropertyImplAsserterImpl<>(vcard.getProperties(clazz), this); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public <T extends DateOrTimeProperty> DateOrTimePropertyAsserter<T> dateProperty(Class<T> clazz) { |
| 151 |
return new DateOrTimePropertyAsserter<>(vcard.getProperties(clazz), this); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
public GeoAsserter geo() { |
| 170 |
return new GeoAsserter(vcard.getGeos(), this); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public TimezoneAsserter timezone() { |
| 178 |
return new TimezoneAsserter(vcard.getTimezones(), this); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public EmailAsserter email() { |
| 186 |
return new EmailAsserter(vcard.getEmails(), this); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public ImppAsserter impp() { |
| 194 |
return new ImppAsserter(vcard.getImpps(), this); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public AddressAsserter address() { |
| 202 |
return new AddressAsserter(vcard.getAddresses(), this); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public TelephoneAsserter telephone() { |
| 210 |
return new TelephoneAsserter(vcard.getTelephoneNumbers(), this); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public StructuredNameAsserter structuredName() { |
| 218 |
return new StructuredNameAsserter(vcard.getStructuredNames(), this); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public XmlAsserter xml() { |
| 226 |
return new XmlAsserter(vcard.getXmls(), this); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 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 |
|
| 249 |
|
| 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 |
} |