| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
|
| 5 |
import java.net.URI; |
| 6 |
|
| 7 |
import org.junit.Test; |
| 8 |
|
| 9 |
import ezvcard.VCard; |
| 10 |
import ezvcard.VCardVersion; |
| 11 |
import ezvcard.property.Impp; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
public class ImppScribeTest { |
| 46 |
private final ImppScribe scribe = new ImppScribe(); |
| 47 |
private final Sensei<Impp> sensei = new Sensei<>(scribe); |
| 48 |
|
| 49 |
private final String uri = "aim:johndoe"; |
| 50 |
private final String badUri = ":::"; |
| 51 |
|
| 52 |
private final Impp withValue = new Impp(uri); |
| 53 |
private final Impp empty = new Impp((URI) null); |
| 54 |
|
| 55 |
@Test |
| 56 |
public void prepareParameters() { |
| 57 |
Impp withPref = new Impp(uri); |
| 58 |
withPref.setPref(1); |
| 59 |
VCard vcard = new VCard(); |
| 60 |
vcard.addImpp(withPref); |
| 61 |
|
| 62 |
sensei.assertPrepareParams(withPref).vcard(vcard).versions(VCardVersion.V2_1, VCardVersion.V3_0).expected("TYPE", "pref").run(); |
| 63 |
sensei.assertPrepareParams(withPref).vcard(vcard).versions(VCardVersion.V4_0).expected("PREF", "1").run(); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void writeText() { |
| 68 |
sensei.assertWriteText(withValue).run(uri); |
| 69 |
sensei.assertWriteText(empty).run(""); |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
public void writeXml() { |
| 74 |
sensei.assertWriteXml(withValue).run("<uri>" + uri + "</uri>"); |
| 75 |
sensei.assertWriteXml(empty).run("<uri/>"); |
| 76 |
} |
| 77 |
|
| 78 |
@Test |
| 79 |
public void writeJson() { |
| 80 |
sensei.assertWriteJson(withValue).run(uri); |
| 81 |
sensei.assertWriteJson(empty).run(""); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void escape_special_chars() { |
| 86 |
Impp impp = new Impp("special", "öffentlich"); |
| 87 |
String expected = "special:%C3%B6ffentlich"; |
| 88 |
|
| 89 |
sensei.assertWriteText(impp).run(expected); |
| 90 |
sensei.assertWriteXml(impp).run("<uri>" + expected + "</uri>"); |
| 91 |
sensei.assertWriteJson(impp).run(expected); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void parseText() { |
| 96 |
sensei.assertParseText(uri).run(withValue); |
| 97 |
sensei.assertParseText(badUri).cannotParse(15); |
| 98 |
sensei.assertParseText("").run(empty); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void parseXml() { |
| 103 |
sensei.assertParseXml("<uri>" + uri + "</uri>").run(withValue); |
| 104 |
sensei.assertParseXml("<uri>" + badUri + "</uri>").cannotParse(15); |
| 105 |
sensei.assertParseXml("").cannotParse(0); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void parseHtml() { |
| 110 |
sensei.assertParseHtml("<a href=\"aim:goim?screenname=johndoe\">IM me</a>").run(withValue); |
| 111 |
sensei.assertParseHtml("<div>aim:goim?screenname=johndoe</div>").run(withValue); |
| 112 |
sensei.assertParseHtml("<div>johndoe</div>").cannotParse(14); |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void parseJson() { |
| 117 |
sensei.assertParseJson(uri).run(withValue); |
| 118 |
sensei.assertParseJson(badUri).cannotParse(15); |
| 119 |
sensei.assertParseJson("").run(empty); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void parseHtmlLink() { |
| 124 |
|
| 125 |
assertParseHtmlLink("aim:theuser", |
| 126 |
"aim:goim?screenname=theuser", |
| 127 |
"aim:goim?screenname=theuser&message=hello", |
| 128 |
"aim:addbuddy?screenname=theuser" |
| 129 |
); |
| 130 |
|
| 131 |
assertParseHtmlLink("ymsgr:theuser", |
| 132 |
"ymsgr:sendim?theuser", |
| 133 |
"ymsgr:addfriend?theuser", |
| 134 |
"ymsgr:sendfile?theuser", |
| 135 |
"ymsgr:call?theuser" |
| 136 |
); |
| 137 |
|
| 138 |
assertParseHtmlLink("skype:theuser", |
| 139 |
"skype:theuser", |
| 140 |
"skype:theuser?call", |
| 141 |
"skype:theuser?call&topic=theTopic" |
| 142 |
); |
| 143 |
|
| 144 |
assertParseHtmlLink("msnim:theuser", |
| 145 |
"msnim:chat?contact=theuser", |
| 146 |
"msnim:add?contact=theuser", |
| 147 |
"msnim:voice?contact=theuser", |
| 148 |
"msnim:video?contact=theuser" |
| 149 |
); |
| 150 |
|
| 151 |
assertParseHtmlLink("xmpp:theuser", |
| 152 |
"xmpp:theuser", |
| 153 |
"xmpp:theuser?message" |
| 154 |
); |
| 155 |
|
| 156 |
assertParseHtmlLink("icq:123456789", |
| 157 |
"icq:message?uin=123456789" |
| 158 |
); |
| 159 |
|
| 160 |
assertParseHtmlLink("sip:username:password@host:port", |
| 161 |
"sip:username:password@host:port" |
| 162 |
); |
| 163 |
|
| 164 |
assertParseHtmlLink("irc://foobar.org/theuser,isnick", |
| 165 |
"irc://foobar.org/theuser,isnick" |
| 166 |
); |
| 167 |
|
| 168 |
assertParseHtmlLink(null, |
| 169 |
"foo:theuser", |
| 170 |
"theuser", |
| 171 |
"aim:invalid?screenname=theuser" |
| 172 |
); |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
private void assertParseHtmlLink(String expectedUri, String... linksToTest) { |
| 177 |
URI expected = (expectedUri == null) ? null : URI.create(expectedUri); |
| 178 |
for (String link : linksToTest) { |
| 179 |
URI actual = scribe.parseHtmlLink(link); |
| 180 |
assertEquals(expected, actual); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void writeHtmlLink() { |
| 186 |
assertWriteHtmlLink(new Impp((String) null), null); |
| 187 |
assertWriteHtmlLink(Impp.aim("theuser"), "aim:goim?screenname=theuser"); |
| 188 |
assertWriteHtmlLink(Impp.skype("theuser"), "skype:theuser"); |
| 189 |
assertWriteHtmlLink(Impp.icq("123456789"), "icq:message?uin=123456789"); |
| 190 |
assertWriteHtmlLink(Impp.msn("theuser"), "msnim:chat?contact=theuser"); |
| 191 |
assertWriteHtmlLink(Impp.yahoo("theuser"), "ymsgr:sendim?theuser"); |
| 192 |
assertWriteHtmlLink(Impp.irc("theuser"), "irc:theuser"); |
| 193 |
assertWriteHtmlLink(Impp.sip("theuser"), "sip:theuser"); |
| 194 |
assertWriteHtmlLink(Impp.xmpp("theuser"), "xmpp:theuser?message"); |
| 195 |
assertWriteHtmlLink(new Impp("foo", "bar"), "foo:bar"); |
| 196 |
} |
| 197 |
|
| 198 |
private void assertWriteHtmlLink(Impp property, String expectedUri) { |
| 199 |
String actualUri = scribe.writeHtmlLink(property); |
| 200 |
assertEquals(expectedUri, actualUri); |
| 201 |
} |
| 202 |
} |