| 1 |
package ezvcard.io.scribe; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertArrayEquals; |
| 4 |
import static org.junit.Assert.assertEquals; |
| 5 |
import static org.junit.Assert.assertNull; |
| 6 |
|
| 7 |
import org.junit.Test; |
| 8 |
|
| 9 |
import ezvcard.io.scribe.Sensei.Check; |
| 10 |
import ezvcard.parameter.ImageType; |
| 11 |
import ezvcard.property.ImageProperty; |
| 12 |
import ezvcard.util.DataUri; |
| 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 ImagePropertyScribeTest { |
| 47 |
private final ImagePropertyScribeImpl scribe = new ImagePropertyScribeImpl(); |
| 48 |
private final Sensei<ImagePropertyImpl> sensei = new Sensei<>(scribe); |
| 49 |
|
| 50 |
private final String url = "http://example.com/image.jpg"; |
| 51 |
private final String urlWithoutExtension = "http://example.com/image"; |
| 52 |
private final byte[] data = "data".getBytes(); |
| 53 |
private final String dataUri = new DataUri("image/jpeg", data).toString(); |
| 54 |
|
| 55 |
@Test |
| 56 |
public void parseHtml() { |
| 57 |
sensei.assertParseHtml("<img />").cannotParse(13); |
| 58 |
sensei.assertParseHtml("<img src=\"\" />").cannotParse(13); |
| 59 |
sensei.assertParseHtml("<img src=\"" + url + "\" />").run(hasUrl(url, ImageType.JPEG)); |
| 60 |
sensei.assertParseHtml("<img src=\"" + urlWithoutExtension + "\" />").run(hasUrl(urlWithoutExtension, null)); |
| 61 |
sensei.assertParseHtml("<img src=\"" + dataUri + "\" />").run(hasData(data, ImageType.JPEG)); |
| 62 |
|
| 63 |
|
| 64 |
sensei.assertParseHtml("<object type=\"image/gif\" data=\"" + url + "\" />").run(hasUrl(url, ImageType.GIF)); |
| 65 |
} |
| 66 |
|
| 67 |
private static class ImagePropertyScribeImpl extends ImagePropertyScribe<ImagePropertyImpl> { |
| 68 |
public ImagePropertyScribeImpl() { |
| 69 |
super(ImagePropertyImpl.class, "IMAGE"); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
protected ImagePropertyImpl _newInstance(String uri, ImageType contentType) { |
| 74 |
return new ImagePropertyImpl(uri, contentType); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
protected ImagePropertyImpl _newInstance(byte[] data, ImageType contentType) { |
| 79 |
return new ImagePropertyImpl(data, contentType); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private static class ImagePropertyImpl extends ImageProperty { |
| 84 |
public ImagePropertyImpl(byte[] data, ImageType type) { |
| 85 |
super(data, type); |
| 86 |
} |
| 87 |
|
| 88 |
public ImagePropertyImpl(String uri, ImageType type) { |
| 89 |
super(uri, type); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private Check<ImagePropertyImpl> hasUrl(final String url, final ImageType contentType) { |
| 94 |
return actual -> { |
| 95 |
assertEquals(url, actual.getUrl()); |
| 96 |
assertNull(actual.getData()); |
| 97 |
assertEquals(contentType, actual.getContentType()); |
| 98 |
}; |
| 99 |
} |
| 100 |
|
| 101 |
private Check<ImagePropertyImpl> hasData(final byte[] data, final ImageType contentType) { |
| 102 |
return actual -> { |
| 103 |
assertNull(actual.getUrl()); |
| 104 |
assertArrayEquals(data, actual.getData()); |
| 105 |
assertEquals(contentType, actual.getContentType()); |
| 106 |
}; |
| 107 |
} |
| 108 |
} |