| 1 |
package ezvcard.io.scribe; |
| 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.VCardVersion.values; |
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.assertNull; |
| 9 |
import static org.junit.Assert.fail; |
| 10 |
|
| 11 |
import org.junit.Test; |
| 12 |
|
| 13 |
import ezvcard.VCard; |
| 14 |
import ezvcard.VCardDataType; |
| 15 |
import ezvcard.VCardVersion; |
| 16 |
import ezvcard.io.EmbeddedVCardException; |
| 17 |
import ezvcard.io.text.WriteContext; |
| 18 |
import ezvcard.property.Agent; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
public class AgentScribeTest { |
| 53 |
private final AgentScribe scribe = new AgentScribe(); |
| 54 |
private final Sensei<Agent> sensei = new Sensei<>(scribe); |
| 55 |
|
| 56 |
private final VCard vcard = new VCard(); |
| 57 |
private final String url = "http://mi5.co.uk/007"; |
| 58 |
private final Agent withUrl = new Agent(url); |
| 59 |
private final Agent withVCard = new Agent(vcard); |
| 60 |
private final Agent empty = new Agent(); |
| 61 |
|
| 62 |
@Test |
| 63 |
public void dataType() { |
| 64 |
sensei.assertDataType(withUrl).versions(V2_1).run(VCardDataType.URL); |
| 65 |
sensei.assertDataType(withUrl).versions(V3_0, V4_0).run(VCardDataType.URI); |
| 66 |
|
| 67 |
sensei.assertDataType(withVCard).run(null); |
| 68 |
sensei.assertDataType(empty).run(null); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void writeText() { |
| 73 |
sensei.assertWriteText(withUrl).run(url); |
| 74 |
|
| 75 |
for (VCardVersion version : values()) { |
| 76 |
try { |
| 77 |
scribe.writeText(withVCard, new WriteContext(version, null, false)); |
| 78 |
fail(); |
| 79 |
} catch (EmbeddedVCardException e) { |
| 80 |
assertEquals(vcard, e.getVCard()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
sensei.assertWriteText(empty).skipMe(); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void parseText_url() { |
| 89 |
sensei.assertParseText(url).dataType(VCardDataType.URL).run(withUrl); |
| 90 |
sensei.assertParseText(url).dataType(VCardDataType.URI).run(withUrl); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void parseText_vcard_2_1() { |
| 95 |
try { |
| 96 |
sensei.assertParseText("").versions(V2_1).run(); |
| 97 |
fail(); |
| 98 |
} catch (EmbeddedVCardException e) { |
| 99 |
Agent property = (Agent) e.getProperty(); |
| 100 |
assertNull(property.getUrl()); |
| 101 |
assertNull(property.getVCard()); |
| 102 |
|
| 103 |
e.injectVCard(vcard); |
| 104 |
assertNull(property.getUrl()); |
| 105 |
assertEquals(vcard, property.getVCard()); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void parseText_vcard_3_0() { |
| 111 |
try { |
| 112 |
sensei.assertParseText("BEGIN:VCARD\\nEND:VCARD").versions(V3_0).run(); |
| 113 |
fail(); |
| 114 |
} catch (EmbeddedVCardException e) { |
| 115 |
Agent property = (Agent) e.getProperty(); |
| 116 |
assertNull(property.getUrl()); |
| 117 |
assertNull(property.getVCard()); |
| 118 |
|
| 119 |
e.injectVCard(vcard); |
| 120 |
assertNull(property.getUrl()); |
| 121 |
assertEquals(vcard, property.getVCard()); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void parseHtml() { |
| 127 |
sensei.assertParseHtml("<a href=\"" + url + "\"></a>").run(withUrl); |
| 128 |
sensei.assertParseHtml("<div>" + url + "</div>").run(withUrl); |
| 129 |
|
| 130 |
try { |
| 131 |
|
| 132 |
sensei.assertParseHtml( |
| 133 |
"<div class=\"agent vcard\">" + |
| 134 |
"<span class=\"fn\">Jane Doe</span>" + |
| 135 |
"<div class=\"agent vcard\">" + |
| 136 |
"<span class=\"fn\">Joseph Doe</span>" + |
| 137 |
"</div>" + |
| 138 |
"</div>").run(); |
| 139 |
|
| 140 |
|
| 141 |
fail(); |
| 142 |
} catch (EmbeddedVCardException e) { |
| 143 |
Agent property = (Agent) e.getProperty(); |
| 144 |
assertNull(property.getUrl()); |
| 145 |
assertNull(property.getVCard()); |
| 146 |
|
| 147 |
e.injectVCard(vcard); |
| 148 |
assertNull(property.getUrl()); |
| 149 |
assertEquals(vcard, property.getVCard()); |
| 150 |
} |
| 151 |
} |
| 152 |
} |