Debian Codegraph @

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