| 1 |
package ezvcard.io.json; |
| 2 |
|
| 3 |
import static ezvcard.util.StringUtils.NEWLINE; |
| 4 |
import static org.junit.Assert.assertEquals; |
| 5 |
|
| 6 |
import java.io.StringWriter; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.List; |
| 9 |
|
| 10 |
import org.junit.Before; |
| 11 |
import org.junit.Test; |
| 12 |
|
| 13 |
import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 |
import com.fasterxml.jackson.databind.SerializationFeature; |
| 15 |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 16 |
|
| 17 |
import ezvcard.Ezvcard; |
| 18 |
import ezvcard.VCard; |
| 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 |
|
| 53 |
public class JCardSerializerTest { |
| 54 |
|
| 55 |
|
| 56 |
private ObjectMapper mapper; |
| 57 |
|
| 58 |
@Before |
| 59 |
public void before() { |
| 60 |
mapper = new ObjectMapper(); |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void serialize_single() throws Exception { |
| 65 |
VCard vcard = new VCard(); |
| 66 |
vcard.setFormattedName("John Doe"); |
| 67 |
|
| 68 |
JCardModule module = new JCardModule(); |
| 69 |
module.setAddProdId(false); |
| 70 |
mapper.registerModule(module); |
| 71 |
|
| 72 |
String actual = mapper.writeValueAsString(vcard); |
| 73 |
|
| 74 |
|
| 75 |
String expected = |
| 76 |
"[\"vcard\"," + |
| 77 |
"[" + |
| 78 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 79 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 80 |
"]" + |
| 81 |
"]"; |
| 82 |
|
| 83 |
assertEquals(expected, actual); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void serialize_single_prettyPrint() throws Exception { |
| 88 |
VCard vcard = new VCard(); |
| 89 |
vcard.setFormattedName("John Doe"); |
| 90 |
|
| 91 |
JCardModule module = new JCardModule(); |
| 92 |
module.setAddProdId(false); |
| 93 |
mapper.registerModule(module); |
| 94 |
mapper.setDefaultPrettyPrinter(new JCardPrettyPrinter()); |
| 95 |
mapper.configure(SerializationFeature.INDENT_OUTPUT, true); |
| 96 |
|
| 97 |
String actual = mapper.writeValueAsString(vcard); |
| 98 |
|
| 99 |
|
| 100 |
String expected = |
| 101 |
"[" + NEWLINE + |
| 102 |
" \"vcard\"," + NEWLINE + |
| 103 |
" [" + NEWLINE + |
| 104 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 105 |
" [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE + |
| 106 |
" ]" + NEWLINE + |
| 107 |
"]"; |
| 108 |
|
| 109 |
assertEquals(expected, actual); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void serialize_multiple() throws Exception { |
| 114 |
List<VCard> vcards = new ArrayList<>(); |
| 115 |
VCard vcard = new VCard(); |
| 116 |
vcard.setFormattedName("John Doe"); |
| 117 |
vcards.add(vcard); |
| 118 |
|
| 119 |
vcard = new VCard(); |
| 120 |
vcard.setFormattedName("Jane Doe"); |
| 121 |
vcards.add(vcard); |
| 122 |
|
| 123 |
JCardModule module = new JCardModule(); |
| 124 |
module.setAddProdId(false); |
| 125 |
mapper.registerModule(module); |
| 126 |
|
| 127 |
String actual = mapper.writeValueAsString(vcards); |
| 128 |
|
| 129 |
|
| 130 |
String expected = |
| 131 |
"[" + |
| 132 |
"[\"vcard\"," + |
| 133 |
"[" + |
| 134 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 135 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 136 |
"]" + |
| 137 |
"]," + |
| 138 |
"[\"vcard\"," + |
| 139 |
"[" + |
| 140 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 141 |
"[\"fn\",{},\"text\",\"Jane Doe\"]" + |
| 142 |
"]" + |
| 143 |
"]" + |
| 144 |
"]"; |
| 145 |
|
| 146 |
assertEquals(expected, actual); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void serialize_multiple_prettyPrint() throws Exception { |
| 151 |
List<VCard> vcards = new ArrayList<>(); |
| 152 |
VCard vcard = new VCard(); |
| 153 |
vcard.setFormattedName("John Doe"); |
| 154 |
vcards.add(vcard); |
| 155 |
|
| 156 |
vcard = new VCard(); |
| 157 |
vcard.setFormattedName("Jane Doe"); |
| 158 |
vcards.add(vcard); |
| 159 |
|
| 160 |
JCardModule module = new JCardModule(); |
| 161 |
module.setAddProdId(false); |
| 162 |
mapper.registerModule(module); |
| 163 |
mapper.setDefaultPrettyPrinter(new JCardPrettyPrinter()); |
| 164 |
mapper.configure(SerializationFeature.INDENT_OUTPUT, true); |
| 165 |
|
| 166 |
String actual = mapper.writeValueAsString(vcards); |
| 167 |
|
| 168 |
|
| 169 |
String expected = |
| 170 |
"[" + NEWLINE + |
| 171 |
" [" + NEWLINE + |
| 172 |
" \"vcard\"," + NEWLINE + |
| 173 |
" [" + NEWLINE + |
| 174 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 175 |
" [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE + |
| 176 |
" ]" + NEWLINE + |
| 177 |
" ]," + NEWLINE + |
| 178 |
" [" + NEWLINE + |
| 179 |
" \"vcard\"," + NEWLINE + |
| 180 |
" [" + NEWLINE + |
| 181 |
" [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE + |
| 182 |
" [ \"fn\", { }, \"text\", \"Jane Doe\" ]" + NEWLINE + |
| 183 |
" ]" + NEWLINE + |
| 184 |
" ]" + NEWLINE + |
| 185 |
"]"; |
| 186 |
|
| 187 |
assertEquals(expected, actual); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void container() throws Exception { |
| 192 |
VCard vcard = new VCard(); |
| 193 |
vcard.setFormattedName("John Doe"); |
| 194 |
vcard.setMailer("mailer"); |
| 195 |
Container container = new Container(vcard); |
| 196 |
|
| 197 |
StringWriter result = new StringWriter(); |
| 198 |
mapper.writeValue(result, container); |
| 199 |
String actual = result.toString(); |
| 200 |
|
| 201 |
|
| 202 |
String expected = |
| 203 |
"{" + |
| 204 |
"\"contact\":[\"vcard\"," + |
| 205 |
"[" + |
| 206 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 207 |
"[\"prodid\",{},\"text\",\"ez-vcard " + Ezvcard.VERSION + "\"]," + |
| 208 |
"[\"fn\",{},\"text\",\"John Doe\"]" + |
| 209 |
"]" + |
| 210 |
"]" + |
| 211 |
"}"; |
| 212 |
|
| 213 |
assertEquals(expected, actual); |
| 214 |
} |
| 215 |
|
| 216 |
@Test |
| 217 |
public void container_null() throws Exception { |
| 218 |
Container container = new Container(null); |
| 219 |
|
| 220 |
StringWriter result = new StringWriter(); |
| 221 |
mapper.writeValue(result, container); |
| 222 |
String actual = result.toString(); |
| 223 |
|
| 224 |
|
| 225 |
String expected = |
| 226 |
"{" + |
| 227 |
"\"contact\":null"+ |
| 228 |
"}"; |
| 229 |
|
| 230 |
assertEquals(expected, actual); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void container_annotation() throws Exception { |
| 235 |
VCard vcard = new VCard(); |
| 236 |
vcard.setFormattedName("John Doe"); |
| 237 |
vcard.setMailer("mailer"); |
| 238 |
ContainerAnnotation container = new ContainerAnnotation(vcard); |
| 239 |
|
| 240 |
StringWriter result = new StringWriter(); |
| 241 |
mapper.writeValue(result, container); |
| 242 |
String actual = result.toString(); |
| 243 |
|
| 244 |
|
| 245 |
String expected = |
| 246 |
"{" + |
| 247 |
"\"contact\":[\"vcard\"," + |
| 248 |
"[" + |
| 249 |
"[\"version\",{},\"text\",\"4.0\"]," + |
| 250 |
"[\"fn\",{},\"text\",\"John Doe\"]," + |
| 251 |
"[\"mailer\",{},\"text\",\"mailer\"]" + |
| 252 |
"]" + |
| 253 |
"]" + |
| 254 |
"}"; |
| 255 |
|
| 256 |
assertEquals(expected, actual); |
| 257 |
} |
| 258 |
|
| 259 |
private static class Container { |
| 260 |
private final VCard contact; |
| 261 |
|
| 262 |
public Container(VCard contact) { |
| 263 |
this.contact = contact; |
| 264 |
} |
| 265 |
|
| 266 |
@JsonSerialize(using = JCardSerializer.class) |
| 267 |
public VCard getContact() { |
| 268 |
return contact; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
private static class ContainerAnnotation { |
| 273 |
private final VCard contact; |
| 274 |
|
| 275 |
public ContainerAnnotation(VCard contact) { |
| 276 |
this.contact = contact; |
| 277 |
} |
| 278 |
|
| 279 |
@JCardFormat(addProdId = false, versionStrict = false) |
| 280 |
@JsonSerialize(using = JCardSerializer.class) |
| 281 |
public VCard getContact() { |
| 282 |
return contact; |
| 283 |
} |
| 284 |
} |
| 285 |
} |