| 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 org.junit.Assert.assertEquals; |
| 7 |
|
| 8 |
import org.junit.Test; |
| 9 |
|
| 10 |
import ezvcard.VCardDataType; |
| 11 |
import ezvcard.io.scribe.Sensei.Check; |
| 12 |
import ezvcard.property.VCardProperty; |
| 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 SimplePropertyScribeTest { |
| 47 |
private final SimplePropertyScribeImpl scribe = new SimplePropertyScribeImpl(); |
| 48 |
private final Sensei<TestProperty> sensei = new Sensei<>(scribe); |
| 49 |
|
| 50 |
private final String value = "One, two\nthree; four\\."; |
| 51 |
private final String valueEscaped = "One\\, two\nthree\\; four\\\\."; |
| 52 |
private final String valueHtml = "One, two three; four\\."; |
| 53 |
|
| 54 |
private final TestProperty withValue = new TestProperty(value); |
| 55 |
private final TestProperty empty = new TestProperty(null); |
| 56 |
|
| 57 |
@Test |
| 58 |
public void writeText() { |
| 59 |
sensei.assertWriteText(withValue).versions(V2_1).run(value); |
| 60 |
sensei.assertWriteText(withValue).versions(V3_0, V4_0).run(valueEscaped); |
| 61 |
sensei.assertWriteText(empty).run(""); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void writeXml() { |
| 66 |
sensei.assertWriteXml(withValue).run("<text>" + value + "</text>"); |
| 67 |
sensei.assertWriteXml(empty).run("<text/>"); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void writeJson() { |
| 72 |
sensei.assertWriteJson(withValue).run(value); |
| 73 |
sensei.assertWriteJson(empty).run(""); |
| 74 |
} |
| 75 |
|
| 76 |
@Test |
| 77 |
public void parseText() { |
| 78 |
sensei.assertParseText(valueEscaped).run(withValue); |
| 79 |
sensei.assertParseText("").run(hasText("")); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void parseXml() { |
| 84 |
sensei.assertParseXml("<text>" + value + "</text>").run(withValue); |
| 85 |
sensei.assertParseXml("").cannotParse(0); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void parseHtml() { |
| 90 |
sensei.assertParseHtml("<div>" + value + "</div>").run(hasText(valueHtml)); |
| 91 |
sensei.assertParseHtml("<div></div>").run(hasText("")); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void parseJson() { |
| 96 |
sensei.assertParseJson(value).run(withValue); |
| 97 |
sensei.assertParseJson("").run(hasText("")); |
| 98 |
} |
| 99 |
|
| 100 |
private static class SimplePropertyScribeImpl extends SimplePropertyScribe<TestProperty> { |
| 101 |
public SimplePropertyScribeImpl() { |
| 102 |
super(TestProperty.class, "TEST", VCardDataType.TEXT); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
protected String _writeValue(TestProperty property) { |
| 107 |
return property.value; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
protected TestProperty _parseValue(String value) { |
| 112 |
return new TestProperty(value); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private static class TestProperty extends VCardProperty { |
| 117 |
public String value; |
| 118 |
|
| 119 |
public TestProperty(String value) { |
| 120 |
this.value = value; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
private Check<TestProperty> hasText(final String text) { |
| 125 |
return actual -> assertEquals(text, actual.value); |
| 126 |
} |
| 127 |
} |