Debian Codegraph @

src/test/java/ezvcard/io/scribe/ImagePropertyScribeTest.java

raw symbols SCIP
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 Copyright (c) 2012-2023, Michael Angstadt
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20
21 1. Redistributions of source code must retain the above copyright notice, this
22 list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright notice,
24 this list of conditions and the following disclaimer in the documentation
25 and/or other materials provided with the distribution.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 The views and conclusions contained in the software and documentation are those
39 of the authors and should not be interpreted as representing official policies,
40 either expressed or implied, of the FreeBSD Project.
41 */
42
43 /**
44 * @author Michael Angstadt
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 //call super.parseHtml() if it's not an <img> tag
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 }