| 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 |
|
| 7 |
import java.util.Arrays; |
| 8 |
|
| 9 |
import org.junit.Test; |
| 10 |
|
| 11 |
import ezvcard.io.json.JCardValue; |
| 12 |
import ezvcard.property.StructuredName; |
| 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 |
|
| 46 |
public class StructuredNameScribeTest { |
| 47 |
private final StructuredNameScribe scribe = new StructuredNameScribe(); |
| 48 |
private final Sensei<StructuredName> sensei = new Sensei<>(scribe); |
| 49 |
|
| 50 |
private final StructuredName withAllValues = new StructuredName(); |
| 51 |
{ |
| 52 |
withAllValues.setGiven("Jonathan"); |
| 53 |
withAllValues.setFamily("Doe"); |
| 54 |
withAllValues.getAdditionalNames().add("Joh;nny,"); |
| 55 |
withAllValues.getAdditionalNames().add("John"); |
| 56 |
withAllValues.getPrefixes().add("Mr."); |
| 57 |
withAllValues.getSuffixes().add("III"); |
| 58 |
} |
| 59 |
private final StructuredName withEmptyValues = new StructuredName(); |
| 60 |
{ |
| 61 |
withEmptyValues.setGiven("Jonathan"); |
| 62 |
withEmptyValues.setFamily(null); |
| 63 |
withEmptyValues.getAdditionalNames().add("Joh;nny,"); |
| 64 |
withEmptyValues.getAdditionalNames().add("John"); |
| 65 |
} |
| 66 |
private final StructuredName empty = new StructuredName(); |
| 67 |
|
| 68 |
@Test |
| 69 |
public void writeText() { |
| 70 |
sensei.assertWriteText(withAllValues).versions(V2_1).run("Doe;Jonathan;Joh\\;nny,,John;Mr.;III"); |
| 71 |
sensei.assertWriteText(withAllValues).versions(V3_0, V4_0).run("Doe;Jonathan;Joh\\;nny\\,,John;Mr.;III"); |
| 72 |
|
| 73 |
sensei.assertWriteText(withEmptyValues).versions(V2_1).run(";Jonathan;Joh\\;nny,,John"); |
| 74 |
sensei.assertWriteText(withEmptyValues).versions(V3_0, V4_0).run(";Jonathan;Joh\\;nny\\,,John"); |
| 75 |
sensei.assertWriteText(withEmptyValues).includeTrailingSemicolons(true).versions(V3_0, V4_0).run(";Jonathan;Joh\\;nny\\,,John;;"); |
| 76 |
|
| 77 |
sensei.assertWriteText(empty).run(""); |
| 78 |
sensei.assertWriteText(empty).includeTrailingSemicolons(true).run(";;;;"); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void writeXml() { |
| 83 |
|
| 84 |
sensei.assertWriteXml(withAllValues).run( |
| 85 |
"<surname>Doe</surname>" + |
| 86 |
"<given>Jonathan</given>" + |
| 87 |
"<additional>Joh;nny,</additional>" + |
| 88 |
"<additional>John</additional>" + |
| 89 |
"<prefix>Mr.</prefix>" + |
| 90 |
"<suffix>III</suffix>" |
| 91 |
); |
| 92 |
|
| 93 |
sensei.assertWriteXml(withEmptyValues).run( |
| 94 |
"<surname/>" + |
| 95 |
"<given>Jonathan</given>" + |
| 96 |
"<additional>Joh;nny,</additional>" + |
| 97 |
"<additional>John</additional>" + |
| 98 |
"<prefix/>" + |
| 99 |
"<suffix/>" |
| 100 |
); |
| 101 |
|
| 102 |
sensei.assertWriteXml(empty).run( |
| 103 |
"<surname/>" + |
| 104 |
"<given/>" + |
| 105 |
"<additional/>" + |
| 106 |
"<prefix/>" + |
| 107 |
"<suffix/>" |
| 108 |
); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void writeJson() { |
| 114 |
sensei.assertWriteJson(withAllValues).run(JCardValue.structured("Doe", "Jonathan", Arrays.asList("Joh;nny,", "John"), "Mr.", "III")); |
| 115 |
sensei.assertWriteJson(withEmptyValues).run(JCardValue.structured("", "Jonathan", Arrays.asList("Joh;nny,", "John"), "", "")); |
| 116 |
sensei.assertWriteJson(empty).run(JCardValue.structured("", "", "", "", "")); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void parseText() { |
| 121 |
StructuredName withAllValuesV2_1 = new StructuredName(withAllValues); |
| 122 |
withAllValuesV2_1.getAdditionalNames().clear(); |
| 123 |
withAllValuesV2_1.getAdditionalNames().add("Joh;nny,,John"); |
| 124 |
sensei.assertParseText("Doe;Jonathan;Joh\\;nny\\,,John;Mr.;III").versions(V2_1).run(withAllValuesV2_1); |
| 125 |
sensei.assertParseText("Doe;Jonathan;Joh\\;nny\\,,John;Mr.;III").versions(V3_0, V4_0).run(withAllValues); |
| 126 |
|
| 127 |
StructuredName withEmptyValuesV2_1 = new StructuredName(withEmptyValues); |
| 128 |
withEmptyValuesV2_1.getAdditionalNames().clear(); |
| 129 |
withEmptyValuesV2_1.getAdditionalNames().add("Joh;nny,,John"); |
| 130 |
sensei.assertParseText(";Jonathan;Joh\\;nny\\,,John;;").versions(V2_1).run(withEmptyValuesV2_1); |
| 131 |
sensei.assertParseText(";Jonathan;Joh\\;nny\\,,John;;").versions(V3_0, V4_0).run(withEmptyValues); |
| 132 |
|
| 133 |
sensei.assertParseText(";;;;").run(empty); |
| 134 |
sensei.assertParseText("").run(empty); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void parseXml() { |
| 139 |
|
| 140 |
sensei.assertParseXml( |
| 141 |
"<surname>Doe</surname>" + |
| 142 |
"<given>Jonathan</given>" + |
| 143 |
"<additional>Joh;nny,</additional>" + |
| 144 |
"<additional>John</additional>" + |
| 145 |
"<prefix>Mr.</prefix>" + |
| 146 |
"<suffix>III</suffix>" |
| 147 |
).run(withAllValues); |
| 148 |
|
| 149 |
sensei.assertParseXml( |
| 150 |
"<surname/>" + |
| 151 |
"<given>Jonathan</given>" + |
| 152 |
"<additional>Joh;nny,</additional>" + |
| 153 |
"<additional>John</additional>" + |
| 154 |
"<prefix/>" + |
| 155 |
"<suffix/>" |
| 156 |
).run(withEmptyValues); |
| 157 |
|
| 158 |
sensei.assertParseXml( |
| 159 |
"<surname/>" + |
| 160 |
"<given/>" + |
| 161 |
"<additional/>" + |
| 162 |
"<prefix/>" + |
| 163 |
"<suffix/>" |
| 164 |
).run(empty); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void parseHtml() { |
| 170 |
|
| 171 |
sensei.assertParseHtml( |
| 172 |
"<div>" + |
| 173 |
"<span class=\"family-name\">Doe</span>" + |
| 174 |
"<span class=\"given-name\">Jonathan</span>" + |
| 175 |
"<span class=\"additional-name\">Joh;nny,</span>" + |
| 176 |
"<span class=\"additional-name\">John</span>" + |
| 177 |
"<span class=\"honorific-prefix\">Mr.</span>" + |
| 178 |
"<span class=\"honorific-suffix\">III</span>" + |
| 179 |
"</div>" |
| 180 |
).run(withAllValues); |
| 181 |
|
| 182 |
sensei.assertParseHtml( |
| 183 |
"<div>" + |
| 184 |
"<span class=\"given-name\">Jonathan</span>" + |
| 185 |
"<span class=\"additional-name\">Joh;nny,</span>" + |
| 186 |
"<span class=\"additional-name\">John</span>" + |
| 187 |
"</div>" |
| 188 |
).run(withEmptyValues); |
| 189 |
|
| 190 |
sensei.assertParseHtml( |
| 191 |
"<div>" + |
| 192 |
"<span class=\"given-name\"></span>" + |
| 193 |
"</div>" |
| 194 |
).run(empty); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
@Test |
| 199 |
public void parseJson() { |
| 200 |
JCardValue value = JCardValue.structured("Doe", "Jonathan", Arrays.asList("Joh;nny,", "John"), "Mr.", "III"); |
| 201 |
sensei.assertParseJson(value).run(withAllValues); |
| 202 |
|
| 203 |
value = JCardValue.structured(null, "Jonathan", Arrays.asList("Joh;nny,", "John"), "", null); |
| 204 |
sensei.assertParseJson(value).run(withEmptyValues); |
| 205 |
|
| 206 |
value = JCardValue.structured(null, "Jonathan", Arrays.asList("Joh;nny,", "John")); |
| 207 |
sensei.assertParseJson(value).run(withEmptyValues); |
| 208 |
|
| 209 |
value = JCardValue.structured(null, null, "", null, null); |
| 210 |
sensei.assertParseJson(value).run(empty); |
| 211 |
|
| 212 |
sensei.assertParseJson("").run(empty); |
| 213 |
} |
| 214 |
} |