| 1 |
package ezvcard.io; |
| 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.util.TestUtils.each; |
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.assertFalse; |
| 9 |
import static org.junit.Assert.assertNotEquals; |
| 10 |
import static org.junit.Assert.assertNull; |
| 11 |
import static org.junit.Assert.assertTrue; |
| 12 |
import static org.junit.Assert.fail; |
| 13 |
|
| 14 |
import java.io.IOException; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Arrays; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.junit.Before; |
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import ezvcard.VCard; |
| 24 |
import ezvcard.VCardVersion; |
| 25 |
import ezvcard.parameter.AddressType; |
| 26 |
import ezvcard.property.Address; |
| 27 |
import ezvcard.property.FormattedName; |
| 28 |
import ezvcard.property.Gender; |
| 29 |
import ezvcard.property.Label; |
| 30 |
import ezvcard.property.Mailer; |
| 31 |
import ezvcard.property.ProductId; |
| 32 |
import ezvcard.property.RawProperty; |
| 33 |
import ezvcard.property.VCardProperty; |
| 34 |
import ezvcard.util.ListMultimap; |
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public class StreamWriterTest { |
| 69 |
private StreamWriterStub writer; |
| 70 |
private VCard vcard; |
| 71 |
|
| 72 |
@Before |
| 73 |
public void before() { |
| 74 |
writer = new StreamWriterStub(); |
| 75 |
vcard = new VCard(); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void unregistered_property() throws Exception { |
| 80 |
vcard.addProperty(new TestProperty()); |
| 81 |
|
| 82 |
try { |
| 83 |
writer.write(vcard); |
| 84 |
fail("Expected IllegalArgumentException"); |
| 85 |
} catch (IllegalArgumentException e) { |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
assertNull(writer.properties); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void productId() throws IOException { |
| 94 |
assertNull(vcard.getProductId()); |
| 95 |
|
| 96 |
|
| 97 |
{ |
| 98 |
vcard.setProductId((String) null); |
| 99 |
{ |
| 100 |
writer.write(vcard, V2_1); |
| 101 |
assertEquals(1, writer.count()); |
| 102 |
assertEquals(1, writer.count(RawProperty.class)); |
| 103 |
|
| 104 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 105 |
writer.write(vcard, version); |
| 106 |
assertEquals(1, writer.count()); |
| 107 |
assertEquals(1, writer.count(ProductId.class)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
vcard.setProductId("value"); |
| 112 |
{ |
| 113 |
writer.write(vcard, V2_1); |
| 114 |
assertEquals(1, writer.count()); |
| 115 |
assertEquals(1, writer.count(RawProperty.class)); |
| 116 |
|
| 117 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 118 |
writer.write(vcard, version); |
| 119 |
assertEquals(1, writer.count()); |
| 120 |
ProductId prodid = writer.first(ProductId.class); |
| 121 |
assertNotEquals("value", prodid.getValue()); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
writer.setAddProdId(false); |
| 127 |
{ |
| 128 |
vcard.setProductId((String) null); |
| 129 |
{ |
| 130 |
for (VCardVersion version : VCardVersion.values()) { |
| 131 |
writer.write(vcard, version); |
| 132 |
assertEquals(0, writer.count()); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
vcard.setProductId("value"); |
| 137 |
{ |
| 138 |
writer.write(vcard, V2_1); |
| 139 |
assertEquals(0, writer.count()); |
| 140 |
|
| 141 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 142 |
writer.write(vcard, version); |
| 143 |
ProductId prodId = writer.first(ProductId.class); |
| 144 |
assertEquals("value", prodId.getValue()); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
writer.setAddProdId(true); |
| 150 |
{ |
| 151 |
vcard.setProductId((String) null); |
| 152 |
{ |
| 153 |
writer.write(vcard, V2_1); |
| 154 |
assertEquals(1, writer.count()); |
| 155 |
assertEquals(1, writer.count(RawProperty.class)); |
| 156 |
|
| 157 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 158 |
writer.write(vcard, version); |
| 159 |
assertEquals(1, writer.count()); |
| 160 |
assertEquals(1, writer.count(ProductId.class)); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
vcard.setProductId("value"); |
| 165 |
{ |
| 166 |
writer.write(vcard, V2_1); |
| 167 |
assertEquals(1, writer.count()); |
| 168 |
assertEquals(1, writer.count(RawProperty.class)); |
| 169 |
|
| 170 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 171 |
writer.write(vcard, version); |
| 172 |
assertEquals(1, writer.count()); |
| 173 |
ProductId prodid = writer.first(ProductId.class); |
| 174 |
assertNotEquals("value", prodid.getValue()); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@Test |
| 184 |
public void productId_order() throws IOException { |
| 185 |
vcard.setFormattedName("Name"); |
| 186 |
|
| 187 |
{ |
| 188 |
writer.write(vcard, V2_1); |
| 189 |
|
| 190 |
Iterator<VCardProperty> it = writer.propertiesList.iterator(); |
| 191 |
|
| 192 |
VCardProperty property = it.next(); |
| 193 |
assertTrue(property instanceof RawProperty); |
| 194 |
property = it.next(); |
| 195 |
assertTrue(property instanceof FormattedName); |
| 196 |
|
| 197 |
assertFalse(it.hasNext()); |
| 198 |
} |
| 199 |
|
| 200 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 201 |
writer.write(vcard, version); |
| 202 |
|
| 203 |
Iterator<VCardProperty> it = writer.propertiesList.iterator(); |
| 204 |
|
| 205 |
VCardProperty property = it.next(); |
| 206 |
assertTrue(property instanceof ProductId); |
| 207 |
property = it.next(); |
| 208 |
assertTrue(property instanceof FormattedName); |
| 209 |
|
| 210 |
assertFalse(it.hasNext()); |
| 211 |
} |
| 212 |
|
| 213 |
vcard.setProductId("value"); |
| 214 |
writer.setAddProdId(false); |
| 215 |
for (VCardVersion version : each(V3_0, V4_0)) { |
| 216 |
writer.write(vcard, version); |
| 217 |
|
| 218 |
Iterator<VCardProperty> it = writer.propertiesList.iterator(); |
| 219 |
|
| 220 |
VCardProperty property = it.next(); |
| 221 |
assertTrue(property instanceof ProductId); |
| 222 |
property = it.next(); |
| 223 |
assertTrue(property instanceof FormattedName); |
| 224 |
|
| 225 |
assertFalse(it.hasNext()); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
@Test |
| 230 |
public void versionStrict() throws Exception { |
| 231 |
writer.setAddProdId(false); |
| 232 |
vcard.setGender(Gender.male()); |
| 233 |
vcard.setMailer("value"); |
| 234 |
|
| 235 |
|
| 236 |
{ |
| 237 |
for (VCardVersion version : each(V2_1, V3_0)) { |
| 238 |
writer.write(vcard, version); |
| 239 |
assertEquals(1, writer.count()); |
| 240 |
assertEquals(1, writer.count(Mailer.class)); |
| 241 |
} |
| 242 |
|
| 243 |
writer.write(vcard, V4_0); |
| 244 |
assertEquals(1, writer.count()); |
| 245 |
assertEquals(1, writer.count(Gender.class)); |
| 246 |
} |
| 247 |
|
| 248 |
writer.setVersionStrict(false); |
| 249 |
{ |
| 250 |
for (VCardVersion version : VCardVersion.values()) { |
| 251 |
writer.write(vcard, version); |
| 252 |
assertEquals(2, writer.count()); |
| 253 |
assertEquals(1, writer.count(Mailer.class)); |
| 254 |
assertEquals(1, writer.count(Gender.class)); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
writer.setVersionStrict(true); |
| 259 |
{ |
| 260 |
for (VCardVersion version : each(V2_1, V3_0)) { |
| 261 |
writer.write(vcard, version); |
| 262 |
assertEquals(1, writer.count()); |
| 263 |
assertEquals(1, writer.count(Mailer.class)); |
| 264 |
} |
| 265 |
|
| 266 |
writer.write(vcard, V4_0); |
| 267 |
assertEquals(1, writer.count()); |
| 268 |
assertEquals(1, writer.count(Gender.class)); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
@Test |
| 273 |
public void labels() throws Throwable { |
| 274 |
writer.setAddProdId(false); |
| 275 |
|
| 276 |
|
| 277 |
Address adr = new Address(); |
| 278 |
adr.setLabel("value1"); |
| 279 |
adr.getTypes().add(AddressType.HOME); |
| 280 |
vcard.addAddress(adr); |
| 281 |
|
| 282 |
|
| 283 |
adr = new Address(); |
| 284 |
adr.setLabel("value2"); |
| 285 |
vcard.addAddress(adr); |
| 286 |
|
| 287 |
|
| 288 |
adr = new Address(); |
| 289 |
vcard.addAddress(adr); |
| 290 |
|
| 291 |
for (VCardVersion version : each(V2_1, V3_0)) { |
| 292 |
writer.write(vcard, version); |
| 293 |
assertEquals(5, writer.count()); |
| 294 |
assertEquals(3, writer.count(Address.class)); |
| 295 |
assertEquals(2, writer.count(Label.class)); |
| 296 |
|
| 297 |
Label label = writer.get(Label.class).get(0); |
| 298 |
assertEquals("value1", label.getValue()); |
| 299 |
assertEquals(Arrays.asList(AddressType.HOME), label.getTypes()); |
| 300 |
|
| 301 |
label = writer.get(Label.class).get(1); |
| 302 |
assertEquals("value2", label.getValue()); |
| 303 |
assertEquals(Arrays.asList(), label.getTypes()); |
| 304 |
} |
| 305 |
|
| 306 |
writer.write(vcard, V4_0); |
| 307 |
assertEquals(3, writer.count()); |
| 308 |
assertEquals(3, writer.count(Address.class)); |
| 309 |
} |
| 310 |
|
| 311 |
private class StreamWriterStub extends StreamWriter { |
| 312 |
private VCardVersion targetVersion; |
| 313 |
private List<VCardProperty> propertiesList; |
| 314 |
private ListMultimap<Class<? extends VCardProperty>, VCardProperty> properties = null; |
| 315 |
|
| 316 |
public StreamWriterStub() { |
| 317 |
this(V4_0); |
| 318 |
} |
| 319 |
|
| 320 |
public StreamWriterStub(VCardVersion targetVersion) { |
| 321 |
this.targetVersion = targetVersion; |
| 322 |
} |
| 323 |
|
| 324 |
@Override |
| 325 |
protected VCardVersion getTargetVersion() { |
| 326 |
return targetVersion; |
| 327 |
} |
| 328 |
|
| 329 |
public void write(VCard vcard, VCardVersion targetVersion) throws IOException { |
| 330 |
this.targetVersion = targetVersion; |
| 331 |
super.write(vcard); |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException { |
| 336 |
propertiesList = properties; |
| 337 |
this.properties = new ListMultimap<>(); |
| 338 |
for (VCardProperty property : properties) { |
| 339 |
this.properties.put(property.getClass(), property); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
public int count() { |
| 344 |
return properties.size(); |
| 345 |
} |
| 346 |
|
| 347 |
public int count(Class<? extends VCardProperty> clazz) { |
| 348 |
return properties.get(clazz).size(); |
| 349 |
} |
| 350 |
|
| 351 |
public <T extends VCardProperty> List<T> get(Class<T> clazz) { |
| 352 |
List<VCardProperty> props = properties.get(clazz); |
| 353 |
List<T> casted = new ArrayList<>(props.size()); |
| 354 |
for (VCardProperty property : props) { |
| 355 |
casted.add(clazz.cast(property)); |
| 356 |
} |
| 357 |
return casted; |
| 358 |
} |
| 359 |
|
| 360 |
public <T extends VCardProperty> T first(Class<T> clazz) { |
| 361 |
List<VCardProperty> props = properties.get(clazz); |
| 362 |
return props.isEmpty() ? null : clazz.cast(props.get(0)); |
| 363 |
} |
| 364 |
|
| 365 |
public void close() throws IOException { |
| 366 |
|
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
private class TestProperty extends VCardProperty { |
| 371 |
|
| 372 |
} |
| 373 |
} |