| 1 |
package ezvcard; |
| 2 |
|
| 3 |
import static ezvcard.util.TestUtils.assertCollectionContains; |
| 4 |
import static ezvcard.util.TestUtils.assertEqualsAndHash; |
| 5 |
import static ezvcard.util.TestUtils.assertEqualsMethodEssentials; |
| 6 |
import static ezvcard.util.TestUtils.assertPropertyCount; |
| 7 |
import static ezvcard.util.TestUtils.assertValidate; |
| 8 |
import static java.util.Arrays.asList; |
| 9 |
import static org.junit.Assert.assertEquals; |
| 10 |
import static org.junit.Assert.assertFalse; |
| 11 |
import static org.junit.Assert.assertNotEquals; |
| 12 |
import static org.junit.Assert.assertNotSame; |
| 13 |
import static org.junit.Assert.assertNull; |
| 14 |
import static org.junit.Assert.assertSame; |
| 15 |
import static org.junit.Assert.assertTrue; |
| 16 |
|
| 17 |
import java.util.Collection; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import ezvcard.property.Gender; |
| 23 |
import ezvcard.property.HasAltId; |
| 24 |
import ezvcard.property.Note; |
| 25 |
import ezvcard.property.RawProperty; |
| 26 |
import ezvcard.property.StructuredName; |
| 27 |
import ezvcard.property.VCardProperty; |
| 28 |
import ezvcard.util.StringUtils; |
| 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 |
public class VCardTest { |
| 63 |
@Test |
| 64 |
public void getProperties() { |
| 65 |
VCard vcard = new VCard(); |
| 66 |
assertCollectionContains(vcard.getProperties()); |
| 67 |
assertEquals(asList(), vcard.getProperties(Note.class)); |
| 68 |
assertEquals(asList(), vcard.getProperties(RawProperty.class)); |
| 69 |
|
| 70 |
Note property1 = new Note("value"); |
| 71 |
vcard.addProperty(property1); |
| 72 |
assertCollectionContains(vcard.getProperties(), property1); |
| 73 |
assertEquals(asList(property1), vcard.getProperties(Note.class)); |
| 74 |
assertEquals(asList(), vcard.getProperties(RawProperty.class)); |
| 75 |
|
| 76 |
RawProperty property2 = vcard.addExtendedProperty("X-GENDER", "male"); |
| 77 |
RawProperty property3 = vcard.addExtendedProperty("X-MANAGER", "Michael Scott"); |
| 78 |
assertCollectionContains(vcard.getProperties(), property1, property2, property3); |
| 79 |
assertEquals(asList(property1), vcard.getProperties(Note.class)); |
| 80 |
assertEquals(asList(property2, property3), vcard.getProperties(RawProperty.class)); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void addProperty() { |
| 86 |
VCard vcard = new VCard(); |
| 87 |
assertEquals(asList(), vcard.getProperties(Note.class)); |
| 88 |
|
| 89 |
Note property1 = new Note("value"); |
| 90 |
vcard.addProperty(property1); |
| 91 |
assertEquals(asList(property1), vcard.getProperties(Note.class)); |
| 92 |
|
| 93 |
Note property2 = new Note("value2"); |
| 94 |
vcard.addProperty(property2); |
| 95 |
assertEquals(asList(property1, property2), vcard.getProperties(Note.class)); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void setProperty() { |
| 100 |
VCard vcard = new VCard(); |
| 101 |
|
| 102 |
Note property1 = new Note("value"); |
| 103 |
assertEquals(asList(), vcard.setProperty(property1)); |
| 104 |
assertEquals(asList(property1), vcard.getProperties(Note.class)); |
| 105 |
|
| 106 |
Note property2 = new Note("value2"); |
| 107 |
assertEquals(asList(property1), vcard.setProperty(property2)); |
| 108 |
assertEquals(asList(property2), vcard.getProperties(Note.class)); |
| 109 |
|
| 110 |
Note property3 = new Note("value3"); |
| 111 |
vcard.addProperty(property3); |
| 112 |
assertEquals(asList(property2, property3), vcard.getProperties(Note.class)); |
| 113 |
|
| 114 |
Note property4 = new Note("value4"); |
| 115 |
assertEquals(asList(property2, property3), vcard.setProperty(property4)); |
| 116 |
assertEquals(asList(property4), vcard.getProperties(Note.class)); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void removeProperty() { |
| 121 |
VCard vcard = new VCard(); |
| 122 |
|
| 123 |
Note property1 = new Note("value"); |
| 124 |
assertFalse(vcard.removeProperty(property1)); |
| 125 |
|
| 126 |
vcard.addProperty(property1); |
| 127 |
assertEquals(asList(property1), vcard.getProperties(Note.class)); |
| 128 |
assertTrue(vcard.removeProperty(property1)); |
| 129 |
assertEquals(asList(), vcard.getProperties(Note.class)); |
| 130 |
} |
| 131 |
|
| 132 |
@Test |
| 133 |
public void removeProperties() { |
| 134 |
VCard vcard = new VCard(); |
| 135 |
|
| 136 |
assertEquals(asList(), vcard.removeProperties(Note.class)); |
| 137 |
|
| 138 |
Note property1 = new Note("value"); |
| 139 |
Note property2 = new Note("value2"); |
| 140 |
vcard.addProperty(property1); |
| 141 |
vcard.addProperty(property2); |
| 142 |
assertEquals(asList(property1, property2), vcard.getProperties(Note.class)); |
| 143 |
|
| 144 |
assertEquals(asList(property1, property2), vcard.removeProperties(Note.class)); |
| 145 |
assertEquals(asList(), vcard.getProperties(Note.class)); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void getExtendedProperty() { |
| 150 |
VCard vcard = new VCard(); |
| 151 |
assertNull(vcard.getExtendedProperty("NAME")); |
| 152 |
|
| 153 |
vcard.addExtendedProperty("NAME2", "value"); |
| 154 |
assertNull(vcard.getExtendedProperty("NAME")); |
| 155 |
|
| 156 |
RawProperty property = vcard.addExtendedProperty("NAME", "value"); |
| 157 |
assertEquals(property, vcard.getExtendedProperty("NAME")); |
| 158 |
assertEquals(property, vcard.getExtendedProperty("name")); |
| 159 |
|
| 160 |
vcard.addExtendedProperty("NAME", "value2"); |
| 161 |
assertEquals(property, vcard.getExtendedProperty("NAME")); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void addExtendedProperty() { |
| 166 |
VCard vcard = new VCard(); |
| 167 |
assertEquals(asList(), vcard.getExtendedProperties()); |
| 168 |
|
| 169 |
RawProperty property = vcard.addExtendedProperty("NAME", "value"); |
| 170 |
assertEquals("NAME", property.getPropertyName()); |
| 171 |
assertEquals("value", property.getValue()); |
| 172 |
assertNull(property.getDataType()); |
| 173 |
assertEquals(asList(property), vcard.getExtendedProperties()); |
| 174 |
|
| 175 |
RawProperty property2 = vcard.addExtendedProperty("NAME", "value", VCardDataType.TEXT); |
| 176 |
assertEquals("NAME", property2.getPropertyName()); |
| 177 |
assertEquals("value", property2.getValue()); |
| 178 |
assertEquals(VCardDataType.TEXT, property2.getDataType()); |
| 179 |
assertEquals(asList(property, property2), vcard.getExtendedProperties()); |
| 180 |
} |
| 181 |
|
| 182 |
@Test |
| 183 |
public void setExtendedProperty() { |
| 184 |
VCard vcard = new VCard(); |
| 185 |
assertEquals(asList(), vcard.getExtendedProperties()); |
| 186 |
|
| 187 |
RawProperty property = vcard.setExtendedProperty("NAME", "value"); |
| 188 |
assertEquals("NAME", property.getPropertyName()); |
| 189 |
assertEquals("value", property.getValue()); |
| 190 |
assertNull(property.getDataType()); |
| 191 |
assertEquals(asList(property), vcard.getExtendedProperties()); |
| 192 |
|
| 193 |
RawProperty property2 = vcard.setExtendedProperty("NAME", "value", VCardDataType.TEXT); |
| 194 |
assertEquals("NAME", property2.getPropertyName()); |
| 195 |
assertEquals("value", property2.getValue()); |
| 196 |
assertEquals(VCardDataType.TEXT, property2.getDataType()); |
| 197 |
assertEquals(asList(property2), vcard.getExtendedProperties()); |
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void removeExtendedProperty() { |
| 202 |
VCard vcard = new VCard(); |
| 203 |
assertEquals(asList(), vcard.removeExtendedProperty("NAME")); |
| 204 |
assertEquals(asList(), vcard.getExtendedProperties()); |
| 205 |
|
| 206 |
RawProperty property = vcard.addExtendedProperty("NAME", "value"); |
| 207 |
RawProperty property2 = vcard.addExtendedProperty("NAME", "value2"); |
| 208 |
RawProperty property3 = vcard.addExtendedProperty("NAME2", "value"); |
| 209 |
assertEquals(asList(property, property2, property3), vcard.getExtendedProperties()); |
| 210 |
|
| 211 |
assertEquals(asList(property, property2), vcard.removeExtendedProperty("NAME")); |
| 212 |
assertEquals(asList(property3), vcard.getExtendedProperties()); |
| 213 |
} |
| 214 |
|
| 215 |
@Test |
| 216 |
public void addPropertyAlt() { |
| 217 |
VCard vcard = new VCard(); |
| 218 |
|
| 219 |
HasAltIdImpl one1 = new HasAltIdImpl("1"); |
| 220 |
vcard.addProperty(one1); |
| 221 |
HasAltIdImpl null1 = new HasAltIdImpl(null); |
| 222 |
vcard.addProperty(null1); |
| 223 |
|
| 224 |
HasAltIdImpl two1 = new HasAltIdImpl("3"); |
| 225 |
HasAltIdImpl two2 = new HasAltIdImpl(null); |
| 226 |
|
| 227 |
vcard.addPropertyAlt(HasAltIdImpl.class, two1, two2); |
| 228 |
|
| 229 |
assertEquals(asList(one1, null1, two1, two2), vcard.getProperties(HasAltIdImpl.class)); |
| 230 |
|
| 231 |
assertEquals("1", one1.getAltId()); |
| 232 |
assertEquals(null, null1.getAltId()); |
| 233 |
assertEquals("2", two1.getAltId()); |
| 234 |
assertEquals("2", two2.getAltId()); |
| 235 |
} |
| 236 |
|
| 237 |
@Test |
| 238 |
public void setPropertyAlt() { |
| 239 |
VCard vcard = new VCard(); |
| 240 |
|
| 241 |
HasAltIdImpl property = new HasAltIdImpl(null); |
| 242 |
HasAltIdImpl property2 = new HasAltIdImpl(null); |
| 243 |
assertEquals(asList(), vcard.setPropertyAlt(HasAltIdImpl.class, property, property2)); |
| 244 |
|
| 245 |
HasAltIdImpl property3 = new HasAltIdImpl(null); |
| 246 |
HasAltIdImpl property4 = new HasAltIdImpl(null); |
| 247 |
assertEquals(asList(property, property2), vcard.setPropertyAlt(HasAltIdImpl.class, property3, property4)); |
| 248 |
} |
| 249 |
|
| 250 |
@Test |
| 251 |
public void generateAltId() { |
| 252 |
Collection<HasAltIdImpl> list = asList(new HasAltIdImpl("1"), new HasAltIdImpl("1"), new HasAltIdImpl("2")); |
| 253 |
assertEquals("3", VCard.generateAltId(list)); |
| 254 |
|
| 255 |
list = asList(new HasAltIdImpl("1"), new HasAltIdImpl("1"), new HasAltIdImpl("3")); |
| 256 |
assertEquals("2", VCard.generateAltId(list)); |
| 257 |
|
| 258 |
list = asList(new HasAltIdImpl("2"), new HasAltIdImpl("2"), new HasAltIdImpl("3")); |
| 259 |
assertEquals("1", VCard.generateAltId(list)); |
| 260 |
|
| 261 |
list = asList(); |
| 262 |
assertEquals("1", VCard.generateAltId(list)); |
| 263 |
|
| 264 |
list = asList(new HasAltIdImpl("one"), new HasAltIdImpl("one"), new HasAltIdImpl("three")); |
| 265 |
assertEquals("1", VCard.generateAltId(list)); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void getPropertiesAlt() { |
| 270 |
VCard vcard = new VCard(); |
| 271 |
assertEquals(asList(), vcard.getPropertiesAlt(HasAltIdImpl.class)); |
| 272 |
|
| 273 |
HasAltIdImpl one1 = new HasAltIdImpl("1"); |
| 274 |
vcard.addProperty(one1); |
| 275 |
HasAltIdImpl null1 = new HasAltIdImpl(null); |
| 276 |
vcard.addProperty(null1); |
| 277 |
HasAltIdImpl two1 = new HasAltIdImpl("2"); |
| 278 |
vcard.addProperty(two1); |
| 279 |
HasAltIdImpl one2 = new HasAltIdImpl("1"); |
| 280 |
vcard.addProperty(one2); |
| 281 |
HasAltIdImpl null2 = new HasAltIdImpl(null); |
| 282 |
vcard.addProperty(null2); |
| 283 |
|
| 284 |
|
| 285 |
@SuppressWarnings("unchecked") |
| 286 |
List<List<HasAltIdImpl>> expected = asList( |
| 287 |
asList(one1, one2), |
| 288 |
asList(two1), |
| 289 |
asList(null1), |
| 290 |
asList(null2) |
| 291 |
); |
| 292 |
|
| 293 |
assertEquals(expected, vcard.getPropertiesAlt(HasAltIdImpl.class)); |
| 294 |
} |
| 295 |
|
| 296 |
@Test |
| 297 |
public void validate_vcard() { |
| 298 |
VCard vcard = new VCard(); |
| 299 |
|
| 300 |
assertValidate(vcard).versions(VCardVersion.V2_1).prop(null, 0).run(); |
| 301 |
assertValidate(vcard).versions(VCardVersion.V3_0).prop(null, 0, 1).run(); |
| 302 |
assertValidate(vcard).versions(VCardVersion.V4_0).prop(null, 1).run(); |
| 303 |
|
| 304 |
vcard.setFormattedName("John Doe"); |
| 305 |
assertValidate(vcard).versions(VCardVersion.V2_1, VCardVersion.V3_0).prop(null, 0).run(); |
| 306 |
assertValidate(vcard).versions(VCardVersion.V4_0).run(); |
| 307 |
|
| 308 |
vcard.setStructuredName(new StructuredName()); |
| 309 |
assertValidate(vcard).run(); |
| 310 |
} |
| 311 |
|
| 312 |
@Test |
| 313 |
public void validate_properties() { |
| 314 |
VCard vcard = new VCard(); |
| 315 |
vcard.setFormattedName("John Doe"); |
| 316 |
VCardPropertyImpl prop = new VCardPropertyImpl(); |
| 317 |
vcard.addProperty(prop); |
| 318 |
|
| 319 |
assertValidate(vcard).versions(VCardVersion.V4_0).prop(prop, 0).run(); |
| 320 |
assertEquals(VCardVersion.V4_0, prop.validateVersion); |
| 321 |
assertSame(vcard, prop.validateVCard); |
| 322 |
} |
| 323 |
|
| 324 |
@Test |
| 325 |
public void copy() { |
| 326 |
VCard vcard = new VCard(); |
| 327 |
vcard.setFormattedName("John Doe"); |
| 328 |
vcard.setVersion(VCardVersion.V2_1); |
| 329 |
|
| 330 |
VCard copy = new VCard(vcard); |
| 331 |
|
| 332 |
assertEquals(vcard.getVersion(), copy.getVersion()); |
| 333 |
assertPropertyCount(1, copy); |
| 334 |
assertNotSame(vcard.getFormattedName(), copy.getFormattedName()); |
| 335 |
assertEquals(vcard.getFormattedName().getValue(), copy.getFormattedName().getValue()); |
| 336 |
assertEquals(vcard, copy); |
| 337 |
} |
| 338 |
|
| 339 |
@Test |
| 340 |
public void equals_essentials() { |
| 341 |
VCard one = new VCard(); |
| 342 |
one.setFormattedName("Name"); |
| 343 |
assertEqualsMethodEssentials(one); |
| 344 |
} |
| 345 |
|
| 346 |
@Test |
| 347 |
public void equals_different_version() { |
| 348 |
VCard one = new VCard(); |
| 349 |
one.setFormattedName("Name"); |
| 350 |
one.setVersion(VCardVersion.V2_1); |
| 351 |
|
| 352 |
VCard two = new VCard(); |
| 353 |
two.setFormattedName("Name"); |
| 354 |
two.setVersion(VCardVersion.V3_0); |
| 355 |
|
| 356 |
assertNotEquals(one, two); |
| 357 |
assertNotEquals(two, one); |
| 358 |
} |
| 359 |
|
| 360 |
@Test |
| 361 |
public void equals_different_number_of_properties() { |
| 362 |
VCard one = new VCard(); |
| 363 |
one.setFormattedName("Name"); |
| 364 |
|
| 365 |
VCard two = new VCard(); |
| 366 |
two.setGender(Gender.male()); |
| 367 |
two.setFormattedName("Name"); |
| 368 |
|
| 369 |
assertNotEquals(one, two); |
| 370 |
assertNotEquals(two, one); |
| 371 |
} |
| 372 |
|
| 373 |
@Test |
| 374 |
public void equals_properties_not_equal() { |
| 375 |
VCard one = new VCard(); |
| 376 |
one.setFormattedName("John"); |
| 377 |
|
| 378 |
VCard two = new VCard(); |
| 379 |
two.setFormattedName("Jane"); |
| 380 |
|
| 381 |
assertNotEquals(one, two); |
| 382 |
assertNotEquals(two, one); |
| 383 |
} |
| 384 |
|
| 385 |
@Test |
| 386 |
public void equals_ignore_property_order() { |
| 387 |
VCard one = new VCard(); |
| 388 |
one.setFormattedName("Name"); |
| 389 |
one.setGender(Gender.male()); |
| 390 |
one.addNote("Note 1"); |
| 391 |
one.addNote("Note 2"); |
| 392 |
|
| 393 |
VCard two = new VCard(); |
| 394 |
two.setGender(Gender.male()); |
| 395 |
two.setFormattedName("Name"); |
| 396 |
two.addNote("Note 2"); |
| 397 |
two.addNote("Note 1"); |
| 398 |
|
| 399 |
assertEqualsAndHash(one, two); |
| 400 |
} |
| 401 |
|
| 402 |
@Test |
| 403 |
public void equals_multiple_identical_properties() { |
| 404 |
VCard one = new VCard(); |
| 405 |
one.setFormattedName("Name"); |
| 406 |
one.setGender(Gender.male()); |
| 407 |
one.addNote("Note 1"); |
| 408 |
one.addNote("Note 1"); |
| 409 |
|
| 410 |
VCard two = new VCard(); |
| 411 |
two.setFormattedName("Name"); |
| 412 |
two.setGender(Gender.male()); |
| 413 |
two.addNote("Note 1"); |
| 414 |
two.addNote("Note 1"); |
| 415 |
|
| 416 |
assertEqualsAndHash(one, two); |
| 417 |
} |
| 418 |
|
| 419 |
@Test |
| 420 |
public void toString_() { |
| 421 |
VCard vcard = new VCard(); |
| 422 |
assertEquals("version=3.0", vcard.toString()); |
| 423 |
|
| 424 |
vcard.setVersion(null); |
| 425 |
assertEquals("version=null", vcard.toString()); |
| 426 |
|
| 427 |
vcard.addNote("value"); |
| 428 |
assertEquals("version=null" + StringUtils.NEWLINE + "ezvcard.property.Note [ group=null | parameters={} | value=value ]", vcard.toString()); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
@Test |
| 437 |
public void equals_multiple_identical_properties_not_equal() { |
| 438 |
VCard one = new VCard(); |
| 439 |
one.setFormattedName("Name"); |
| 440 |
one.setGender(Gender.male()); |
| 441 |
one.addNote("Note 1"); |
| 442 |
one.addNote("Note 1"); |
| 443 |
one.addNote("Note 2"); |
| 444 |
|
| 445 |
VCard two = new VCard(); |
| 446 |
two.setFormattedName("Name"); |
| 447 |
two.setGender(Gender.male()); |
| 448 |
two.addNote("Note 1"); |
| 449 |
two.addNote("Note 2"); |
| 450 |
two.addNote("Note 2"); |
| 451 |
|
| 452 |
assertNotEquals(one, two); |
| 453 |
assertNotEquals(two, one); |
| 454 |
} |
| 455 |
|
| 456 |
private class HasAltIdImpl extends VCardProperty implements HasAltId { |
| 457 |
public HasAltIdImpl(String altId) { |
| 458 |
setAltId(altId); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
public String getAltId() { |
| 463 |
return parameters.getAltId(); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
public void setAltId(String altId) { |
| 468 |
parameters.setAltId(altId); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
private class VCardPropertyImpl extends VCardProperty { |
| 473 |
public VCardVersion validateVersion; |
| 474 |
public VCard validateVCard; |
| 475 |
|
| 476 |
@Override |
| 477 |
public void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 478 |
validateVersion = version; |
| 479 |
validateVCard = vcard; |
| 480 |
warnings.add(new ValidationWarning(0)); |
| 481 |
} |
| 482 |
} |
| 483 |
} |