Debian Codegraph @

src/test/java/ezvcard/util/DataUriTest.java

raw symbols SCIP
1 package ezvcard.util;
2
3 import static ezvcard.util.TestUtils.assertEqualsAndHash;
4 import static ezvcard.util.TestUtils.assertEqualsMethodEssentials;
5 import static ezvcard.util.TestUtils.assertNothingIsEqual;
6 import static org.junit.Assert.assertArrayEquals;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertNotSame;
9 import static org.junit.Assert.assertNull;
10
11 import java.io.UnsupportedEncodingException;
12
13 import org.junit.Test;
14
15 import ezvcard.util.org.apache.commons.codec.binary.Base64;
16
17 /*
18 Copyright (c) 2012-2023, Michael Angstadt
19 All rights reserved.
20
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23
24 1. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 The views and conclusions contained in the software and documentation are those
42 of the authors and should not be interpreted as representing official policies,
43 either expressed or implied, of the FreeBSD Project.
44 */
45
46 /**
47 * @author Michael Angstadt
48 */
49 public class DataUriTest {
50 private final String dataString = "test-data";
51 private final byte[] dataBytes;
52 {
53 try {
54 dataBytes = dataString.getBytes("UTF-8");
55 } catch (UnsupportedEncodingException e) {
56 throw new RuntimeException(e);
57 }
58 }
59 private final String dataBase64 = Base64.encodeBase64String(dataBytes);
60
61 @Test
62 public void constructor() {
63 DataUri uri = new DataUri("text");
64 assertEquals("text/plain", uri.getContentType());
65 assertNull(uri.getData());
66 assertEquals("text", uri.getText());
67
68 uri = new DataUri("text/html", "text");
69 assertEquals("text/html", uri.getContentType());
70 assertNull(uri.getData());
71 assertEquals("text", uri.getText());
72
73 //convert content type to lower case
74 uri = new DataUri("TEXT/HTML", "text");
75 assertEquals("text/html", uri.getContentType());
76 assertNull(uri.getData());
77 assertEquals("text", uri.getText());
78
79 uri = new DataUri("image/png", dataBytes);
80 assertEquals("image/png", uri.getContentType());
81 assertArrayEquals(dataBytes, uri.getData());
82 assertNull(uri.getText());
83
84 //convert null content type to empty string
85 uri = new DataUri(null, dataBytes);
86 assertEquals("", uri.getContentType());
87 }
88
89 @Test
90 public void copy() {
91 DataUri uri = new DataUri("text/html", "text");
92 DataUri copy = new DataUri(uri);
93 assertEquals("text/html", copy.getContentType());
94 assertNull(copy.getData());
95 assertEquals("text", copy.getText());
96 assertEqualsAndHash(uri, copy);
97
98 uri = new DataUri("image/png", dataBytes);
99 copy = new DataUri(uri);
100 assertEquals("image/png", copy.getContentType());
101 assertArrayEquals(dataBytes, copy.getData());
102 assertNotSame(uri.getData(), copy.getData());
103 assertNull(copy.getText());
104 assertEqualsAndHash(uri, copy);
105 }
106
107 @Test
108 public void parse() {
109 DataUri expected = new DataUri("image/png", dataBytes);
110 DataUri actual = DataUri.parse("data:image/png;base64," + dataBase64);
111 assertEquals(expected, actual);
112
113 expected = new DataUri("", dataBytes);
114 actual = DataUri.parse("data:;base64," + dataBase64);
115 assertEquals(expected, actual);
116
117 expected = new DataUri("image/png", dataString);
118 actual = DataUri.parse("data:image/png;charset=UTF-8;base64," + dataBase64);
119 assertEquals(expected, actual);
120
121 //order of charset and base64 matter
122 expected = new DataUri("image/png", dataString);
123 actual = DataUri.parse("data:image/png;base64;charset=UTF-8," + dataBase64);
124 assertEquals(expected, actual);
125
126 //ignore unknown tokens
127 expected = new DataUri("image/png", dataString);
128 actual = DataUri.parse("data:image/png;charset=UTF-8;foo;base64;bar," + dataBase64);
129 assertEquals(expected, actual);
130
131 expected = new DataUri("image/png", dataBase64);
132 actual = DataUri.parse("data:image/png;charset=UTF-8," + dataBase64);
133 assertEquals(expected, actual);
134
135 expected = new DataUri("image/png", dataBase64);
136 actual = DataUri.parse("data:image/png;charset=UTF-8;foobar," + dataBase64);
137 assertEquals(expected, actual);
138 }
139
140 @Test
141 public void parse_comma_in_content_type() {
142 DataUri expected = new DataUri("text/pla", "in;base64," + dataBase64);
143 DataUri actual = DataUri.parse("data:text/pla,in;base64," + dataBase64);
144 assertEquals(expected, actual);
145 }
146
147 @Test(expected = IllegalArgumentException.class)
148 public void parse_short_string() {
149 DataUri.parse("a");
150 }
151
152 @Test(expected = IllegalArgumentException.class)
153 public void parse_not_a_uri() {
154 DataUri.parse("not-valid");
155 }
156
157 @Test(expected = IllegalArgumentException.class)
158 public void parse_wrong_scheme() {
159 DataUri.parse("mailto:[email protected]");
160 }
161
162 @Test(expected = IllegalArgumentException.class)
163 public void parse_no_comma() {
164 DataUri.parse("data:text/plain;base64");
165 }
166
167 @Test(expected = IllegalArgumentException.class)
168 public void parse_bad_charset() {
169 DataUri.parse("data:text/plain;charset=foobar;base64," + dataBase64);
170 }
171
172 @Test
173 public void toString_() {
174 DataUri uri = new DataUri("text/plain", dataBytes);
175 assertEquals("data:text/plain;base64," + dataBase64, uri.toString());
176
177 uri = new DataUri("text/plain", dataBytes);
178 assertEquals("data:text/plain;base64," + dataBase64, uri.toString("UTF-8"));
179
180 uri = new DataUri("text/plain", dataString);
181 assertEquals("data:text/plain," + dataString, uri.toString());
182
183 uri = new DataUri("text/plain", dataString);
184 assertEquals("data:text/plain;charset=UTF-8;base64," + dataBase64, uri.toString("UTF-8"));
185
186 uri = new DataUri("text/plain", (String) null);
187 assertEquals("data:text/plain,", uri.toString());
188 }
189
190 @Test(expected = IllegalArgumentException.class)
191 public void toString_bad_charset() {
192 DataUri uri = new DataUri("text/plain", dataString);
193 uri.toString("foobar");
194 }
195
196 @Test
197 public void equals() {
198 assertEqualsMethodEssentials(new DataUri(""));
199
200 //@formatter:off
201 assertNothingIsEqual(
202 new DataUri("text/plain", dataBytes),
203 new DataUri("image/png", dataBytes),
204 new DataUri("text/plain", "other-string".getBytes()),
205 new DataUri("text/plain", dataString),
206 new DataUri("text/html", dataString),
207 new DataUri("text/plain", "other-string"),
208 new DataUri("text/plain", (String)null)
209 );
210 //@formatter:on
211 }
212 }