Debian Codegraph @

src/test/java/ezvcard/property/BinaryPropertyTest.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import static ezvcard.property.PropertySensei.assertCopy;
4 import static ezvcard.property.PropertySensei.assertEqualsMethod;
5 import static ezvcard.property.PropertySensei.assertNothingIsEqual;
6 import static ezvcard.property.PropertySensei.assertValidate;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNull;
10
11 import java.io.ByteArrayInputStream;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.nio.file.Files;
15 import java.nio.file.Path;
16 import java.nio.file.Paths;
17
18 import org.junit.Test;
19
20 import ezvcard.parameter.ImageType;
21
22 /*
23 Copyright (c) 2012-2023, Michael Angstadt
24 All rights reserved.
25
26 Redistribution and use in source and binary forms, with or without
27 modification, are permitted provided that the following conditions are met:
28
29 1. Redistributions of source code must retain the above copyright notice, this
30 list of conditions and the following disclaimer.
31 2. Redistributions in binary form must reproduce the above copyright notice,
32 this list of conditions and the following disclaimer in the documentation
33 and/or other materials provided with the distribution.
34
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
36 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
39 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46 The views and conclusions contained in the software and documentation are those
47 of the authors and should not be interpreted as representing official policies,
48 either expressed or implied, of the FreeBSD Project.
49 */
50
51 /**
52 * @author Michael Angstadt
53 */
54 public class BinaryPropertyTest {
55 @Test
56 public void constructors() throws Exception {
57 BinaryPropertyImpl property = new BinaryPropertyImpl();
58 assertNull(property.getContentType());
59 assertNull(property.getUrl());
60 assertNull(property.getData());
61
62 property = new BinaryPropertyImpl(new byte[0], ImageType.JPEG);
63 assertEquals(ImageType.JPEG, property.getContentType());
64 assertNull(property.getUrl());
65 assertEquals(0, property.getData().length);
66
67 property = new BinaryPropertyImpl("one", ImageType.PNG);
68 assertEquals(ImageType.PNG, property.getContentType());
69 assertEquals("one", property.getUrl());
70 assertNull(property.getData());
71
72 Path file = Paths.get("pom.xml");
73 property = new BinaryPropertyImpl(file, ImageType.JPEG);
74 assertEquals(ImageType.JPEG, property.getContentType());
75 assertNull(property.getUrl());
76 assertEquals(Files.size(file), property.getData().length);
77
78 InputStream in = new ByteArrayInputStream("data".getBytes());
79 property = new BinaryPropertyImpl(in, ImageType.JPEG);
80 assertEquals(ImageType.JPEG, property.getContentType());
81 assertNull(property.getUrl());
82 assertEquals("data", new String(property.getData()));
83 }
84
85 @Test
86 public void set_value() {
87 BinaryPropertyImpl property = new BinaryPropertyImpl();
88
89 property.setUrl("one", ImageType.PNG);
90 assertEquals(ImageType.PNG, property.getContentType());
91 assertEquals("one", property.getUrl());
92 assertNull(property.getData());
93
94 property.setData("data".getBytes(), ImageType.JPEG);
95 assertEquals(ImageType.JPEG, property.getContentType());
96 assertNull(property.getUrl());
97 assertEquals("data", new String(property.getData()));
98
99 property.setUrl("one", ImageType.PNG);
100 assertEquals(ImageType.PNG, property.getContentType());
101 assertEquals("one", property.getUrl());
102 assertNull(property.getData());
103 }
104
105 @Test
106 public void validate() {
107 BinaryPropertyImpl empty = new BinaryPropertyImpl();
108 assertValidate(empty).run(8);
109
110 BinaryPropertyImpl withUrl = new BinaryPropertyImpl();
111 withUrl.setUrl("http://example.com/image.jpg", ImageType.JPEG);
112 assertValidate(withUrl).run();
113
114 BinaryPropertyImpl withData = new BinaryPropertyImpl();
115 withData.setData("data".getBytes(), ImageType.JPEG);
116 assertValidate(withData).run();
117 }
118
119 @Test
120 public void toStringValues() {
121 BinaryPropertyImpl property = new BinaryPropertyImpl();
122 assertFalse(property.toStringValues().isEmpty());
123 }
124
125 @Test
126 public void copy() {
127 BinaryPropertyImpl original = new BinaryPropertyImpl();
128 assertCopy(original);
129
130 original = new BinaryPropertyImpl("one", ImageType.PNG);
131 assertCopy(original);
132
133 original = new BinaryPropertyImpl("data".getBytes(), ImageType.JPEG);
134 assertCopy(original).notSame("getData");
135 }
136
137 @Test
138 public void equals() {
139 //@formatter:off
140 assertNothingIsEqual(
141 new BinaryPropertyImpl(),
142 new BinaryPropertyImpl("value", ImageType.PNG),
143 new BinaryPropertyImpl("value2", ImageType.PNG),
144 new BinaryPropertyImpl("value", ImageType.JPEG),
145 new BinaryPropertyImpl("value", null),
146 new BinaryPropertyImpl("data".getBytes(), ImageType.PNG),
147 new BinaryPropertyImpl("data2".getBytes(), ImageType.PNG),
148 new BinaryPropertyImpl("data".getBytes(), ImageType.JPEG),
149 new BinaryPropertyImpl("data".getBytes(), null)
150 );
151
152 assertEqualsMethod(BinaryPropertyImpl.class)
153 .constructor().test()
154 .constructor("value", ImageType.PNG).test()
155 .constructor(new Class<?>[]{String.class, ImageType.class}, "value", null).test()
156 .constructor("data".getBytes(), ImageType.PNG).test()
157 .constructor(new Class<?>[]{byte[].class, ImageType.class}, "data".getBytes(), null).test();
158 //@formatter:on
159 }
160
161 public static class BinaryPropertyImpl extends BinaryProperty<ImageType> {
162 public BinaryPropertyImpl() {
163 super();
164 }
165
166 public BinaryPropertyImpl(String url, ImageType type) {
167 super(url, type);
168 }
169
170 public BinaryPropertyImpl(byte[] data, ImageType type) {
171 super(data, type);
172 }
173
174 public BinaryPropertyImpl(InputStream in, ImageType type) throws IOException {
175 super(in, type);
176 }
177
178 public BinaryPropertyImpl(Path file, ImageType type) throws IOException {
179 super(file, type);
180 }
181
182 public BinaryPropertyImpl(BinaryPropertyImpl original) {
183 super(original);
184 }
185 }
186 }