| 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 |
|
| 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 |
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 |
|
| 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 |
|
| 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 |
} |