| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 183 |
assertEqualsMethod(Key.class) |
| 184 |
.constructor() |
| 185 |
.test() |
| 186 |
.method("setText", "text", KeyType.GPG).test(); |
| 187 |
|
| 188 |
} |
| 189 |
} |