1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.common.media;
|
5
|
|
6
|
import static org.junit.Assert.fail;
|
7
|
|
8
|
import java.io.IOException;
|
9
|
import java.net.URI;
|
10
|
import java.net.URL;
|
11
|
import java.util.Map;
|
12
|
|
13
|
import org.apache.http.HttpException;
|
14
|
import org.junit.Assert;
|
15
|
import org.junit.Before;
|
16
|
import org.junit.Test;
|
17
|
|
18
|
import eu.etaxonomy.cdm.common.UriUtils;
|
19
|
import eu.etaxonomy.cdm.common.media.ImageInfo;
|
20
|
import eu.etaxonomy.cdm.common.media.MimeType;
|
21
|
|
22
|
/**
|
23
|
* @author n.hoffmann
|
24
|
*
|
25
|
*/
|
26
|
public class ImageInfoTest {
|
27
|
|
28
|
private URI jpegUri;
|
29
|
private URI tiffUri;
|
30
|
private ImageInfo jpegInstance;
|
31
|
private ImageInfo tifInstance;
|
32
|
|
33
|
private URI remotePngUri;
|
34
|
private ImageInfo pngInstance;
|
35
|
|
36
|
/**
|
37
|
* @throws java.lang.Exception
|
38
|
*/
|
39
|
@Before
|
40
|
public void setUp() throws Exception {
|
41
|
URL jpegUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.jpg");
|
42
|
jpegUri = jpegUrl.toURI();
|
43
|
|
44
|
URL tiffUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.tif");
|
45
|
tiffUri = tiffUrl.toURI();
|
46
|
|
47
|
remotePngUri = URI.create("http://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
|
48
|
}
|
49
|
|
50
|
@Test
|
51
|
public void testNewInstanceJpeg(){
|
52
|
try {
|
53
|
ImageInfo.NewInstance(jpegUri, 0);
|
54
|
} catch (Exception e) {
|
55
|
fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
|
56
|
}
|
57
|
}
|
58
|
|
59
|
@Test
|
60
|
public void testNewInstanceTiff() {
|
61
|
try {
|
62
|
ImageInfo.NewInstance(tiffUri, 0);
|
63
|
} catch (Exception e) {
|
64
|
fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
|
65
|
}
|
66
|
}
|
67
|
|
68
|
@Test
|
69
|
public void testNewInstanceRemotePng() {
|
70
|
try {
|
71
|
ImageInfo.NewInstance(remotePngUri, 3000);
|
72
|
} catch (Exception e) {
|
73
|
fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
|
74
|
}
|
75
|
}
|
76
|
|
77
|
@Test(expected=IOException.class)
|
78
|
public void testNewInstanceFileDoesNotExist() throws HttpException, IOException {
|
79
|
URI nonExistentUri = URI.create("file:///nonExistentImage.jpg");
|
80
|
|
81
|
ImageInfo.NewInstance(nonExistentUri, 0);
|
82
|
}
|
83
|
|
84
|
private ImageInfo getJpegInstance(){
|
85
|
if(jpegInstance == null){
|
86
|
try {
|
87
|
jpegInstance = ImageInfo.NewInstance(jpegUri, 0);
|
88
|
} catch (Exception e) {
|
89
|
fail("This case should have been covered by other tests.");
|
90
|
return null;
|
91
|
}
|
92
|
}
|
93
|
return jpegInstance;
|
94
|
}
|
95
|
|
96
|
private ImageInfo getTifInstance(){
|
97
|
if(tifInstance == null){
|
98
|
try {
|
99
|
tifInstance = ImageInfo.NewInstance(tiffUri, 0);
|
100
|
} catch (Exception e) {
|
101
|
fail("This case should have been covered by other tests.");
|
102
|
return null;
|
103
|
}
|
104
|
}
|
105
|
return tifInstance;
|
106
|
}
|
107
|
|
108
|
private ImageInfo getRemotePngInstance(){
|
109
|
if(pngInstance == null){
|
110
|
try {
|
111
|
pngInstance = ImageInfo.NewInstance(remotePngUri, 3000);
|
112
|
} catch (Exception e) {
|
113
|
fail("This case should have been covered by other tests.");
|
114
|
return null;
|
115
|
}
|
116
|
}
|
117
|
return pngInstance;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getWidth()}.
|
122
|
*/
|
123
|
@Test
|
124
|
public void testGetWidth() {
|
125
|
Assert.assertEquals(300, getJpegInstance().getWidth());
|
126
|
Assert.assertEquals(300, getTifInstance().getWidth());
|
127
|
|
128
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
129
|
Assert.assertEquals(93, getRemotePngInstance().getWidth());
|
130
|
}
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getHeight()}.
|
135
|
*/
|
136
|
@Test
|
137
|
public void testGetHeight() {
|
138
|
Assert.assertEquals(225, getJpegInstance().getHeight());
|
139
|
Assert.assertEquals(225, getTifInstance().getHeight());
|
140
|
|
141
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
142
|
Assert.assertEquals(93, getRemotePngInstance().getHeight());
|
143
|
}
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getBitPerPixel()}.
|
148
|
*/
|
149
|
@Test
|
150
|
public void testGetBitPerPixel() {
|
151
|
Assert.assertEquals(24, getJpegInstance().getBitPerPixel());
|
152
|
Assert.assertEquals(24, getTifInstance().getBitPerPixel());
|
153
|
|
154
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
155
|
Assert.assertEquals(32, getRemotePngInstance().getBitPerPixel());
|
156
|
}
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getFormatName()}.
|
161
|
*/
|
162
|
@Test
|
163
|
public void testGetFormatName() {
|
164
|
Assert.assertEquals("JPEG (Joint Photographic Experts Group) Format", getJpegInstance().getFormatName());
|
165
|
Assert.assertEquals("TIFF Tag-based Image File Format", getTifInstance().getFormatName());
|
166
|
|
167
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
168
|
Assert.assertEquals("PNG Portable Network Graphics", getRemotePngInstance().getFormatName());
|
169
|
}
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getMimeType()}.
|
174
|
*/
|
175
|
@Test
|
176
|
public void testGetMimeType() {
|
177
|
Assert.assertEquals(MimeType.JPEG.getMimeType(), getJpegInstance().getMimeType());
|
178
|
Assert.assertEquals(MimeType.TIFF.getMimeType(), getTifInstance().getMimeType());
|
179
|
|
180
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
181
|
Assert.assertEquals(MimeType.PNG.getMimeType(), getRemotePngInstance().getMimeType());
|
182
|
}
|
183
|
}
|
184
|
|
185
|
@Test
|
186
|
public void testGetLength(){
|
187
|
Assert.assertEquals(63500, getJpegInstance().getLength());
|
188
|
Assert.assertEquals(202926, getTifInstance().getLength());
|
189
|
|
190
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
191
|
Assert.assertEquals(9143, getRemotePngInstance().getLength());
|
192
|
}
|
193
|
}
|
194
|
|
195
|
@Test
|
196
|
public void testReadMetaDataJpeg() throws IOException, HttpException{
|
197
|
ImageInfo instance = getJpegInstance();
|
198
|
|
199
|
instance.readMetaData(0);
|
200
|
|
201
|
Map<String, String> metaData = instance.getMetaData();
|
202
|
|
203
|
Assert.assertEquals(48, metaData.size());
|
204
|
}
|
205
|
|
206
|
|
207
|
@Test
|
208
|
public void testReadMetaDataTif() throws IOException, HttpException{
|
209
|
ImageInfo instance = getTifInstance();
|
210
|
|
211
|
instance.readMetaData(0);
|
212
|
|
213
|
Map<String, String> metaData = instance.getMetaData();
|
214
|
|
215
|
Assert.assertEquals(15, metaData.size());
|
216
|
}
|
217
|
|
218
|
@Test
|
219
|
public void testReadMetaDataRemotePng() throws IOException, HttpException{
|
220
|
ImageInfo instance = getRemotePngInstance();
|
221
|
|
222
|
instance.readMetaData(3000);
|
223
|
|
224
|
Map<String, String> metaData = instance.getMetaData();
|
225
|
|
226
|
Assert.assertEquals(1, metaData.size());
|
227
|
}
|
228
|
}
|