1 |
1 |
/**
|
2 |
|
*
|
|
2 |
*
|
3 |
3 |
*/
|
4 |
4 |
package eu.etaxonomy.cdm.common.media;
|
5 |
5 |
|
... | ... | |
11 |
11 |
import java.util.Map;
|
12 |
12 |
|
13 |
13 |
import org.apache.http.HttpException;
|
|
14 |
import org.apache.log4j.Logger;
|
14 |
15 |
import org.junit.Assert;
|
15 |
16 |
import org.junit.Before;
|
16 |
17 |
import org.junit.Test;
|
... | ... | |
25 |
26 |
*/
|
26 |
27 |
public class ImageInfoTest {
|
27 |
28 |
|
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 testGetSuffix(){
|
197 |
|
Assert.assertEquals("jpg", getJpegInstance().getSuffix());
|
198 |
|
Assert.assertEquals("tif", getTifInstance().getSuffix());
|
199 |
|
|
200 |
|
if(UriUtils.isInternetAvailable(remotePngUri)){
|
201 |
|
Assert.assertEquals("png", getRemotePngInstance().getSuffix());
|
202 |
|
}
|
203 |
|
}
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
@Test
|
208 |
|
public void testReadMetaDataJpeg() throws IOException, HttpException{
|
209 |
|
ImageInfo instance = getJpegInstance();
|
210 |
|
|
211 |
|
instance.readMetaData(0);
|
212 |
|
|
213 |
|
Map<String, String> metaData = instance.getMetaData();
|
214 |
|
|
215 |
|
Assert.assertEquals(48, metaData.size());
|
216 |
|
}
|
217 |
|
|
218 |
|
|
219 |
|
@Test
|
220 |
|
public void testReadMetaDataTif() throws IOException, HttpException{
|
221 |
|
ImageInfo instance = getTifInstance();
|
222 |
|
|
223 |
|
instance.readMetaData(0);
|
224 |
|
|
225 |
|
Map<String, String> metaData = instance.getMetaData();
|
226 |
|
|
227 |
|
Assert.assertEquals(15, metaData.size());
|
228 |
|
}
|
229 |
|
|
230 |
|
@Test
|
231 |
|
public void testReadMetaDataRemotePng() throws IOException, HttpException{
|
232 |
|
ImageInfo instance = getRemotePngInstance();
|
233 |
|
|
234 |
|
instance.readMetaData(3000);
|
235 |
|
|
236 |
|
Map<String, String> metaData = instance.getMetaData();
|
237 |
|
|
238 |
|
Assert.assertEquals(1, metaData.size());
|
239 |
|
}
|
|
29 |
private static final String OFFLINE = "OFFLINE";
|
|
30 |
|
|
31 |
public static final Logger logger = Logger.getLogger(ImageInfoTest.class);
|
|
32 |
|
|
33 |
private URI jpegUri;
|
|
34 |
private URI tiffUri;
|
|
35 |
private ImageInfo jpegInstance;
|
|
36 |
private ImageInfo tifInstance;
|
|
37 |
|
|
38 |
private URI remotePngUri;
|
|
39 |
private ImageInfo pngInstance;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* @throws java.lang.Exception
|
|
43 |
*/
|
|
44 |
@Before
|
|
45 |
public void setUp() throws Exception {
|
|
46 |
URL jpegUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.jpg");
|
|
47 |
jpegUri = jpegUrl.toURI();
|
|
48 |
|
|
49 |
URL tiffUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.tif");
|
|
50 |
tiffUri = tiffUrl.toURI();
|
|
51 |
|
|
52 |
remotePngUri = URI.create("http://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
|
|
53 |
}
|
|
54 |
|
|
55 |
@Test
|
|
56 |
public void testNewInstanceJpeg(){
|
|
57 |
try {
|
|
58 |
ImageInfo.NewInstance(jpegUri, 0);
|
|
59 |
} catch (Exception e) {
|
|
60 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
@Test
|
|
65 |
public void testNewInstanceTiff() {
|
|
66 |
try {
|
|
67 |
ImageInfo.NewInstance(tiffUri, 0);
|
|
68 |
} catch (Exception e) {
|
|
69 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
@Test
|
|
74 |
public void testNewInstanceRemotePng() {
|
|
75 |
if(UriUtils.isInternetAvailable(remotePngUri)){
|
|
76 |
try {
|
|
77 |
ImageInfo.NewInstance(remotePngUri, 3000);
|
|
78 |
} catch (Exception e) {
|
|
79 |
fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
|
|
80 |
}
|
|
81 |
} else {
|
|
82 |
logger.warn("test testNewInstanceRemotePng() skipped, since server is not available");
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
@Test(expected=IOException.class)
|
|
87 |
public void testNewInstanceFileDoesNotExist() throws HttpException, IOException {
|
|
88 |
URI nonExistentUri = URI.create("file:///nonExistentImage.jpg");
|
|
89 |
|
|
90 |
ImageInfo.NewInstance(nonExistentUri, 0);
|
|
91 |
}
|
|
92 |
|
|
93 |
private ImageInfo getJpegInstance(){
|
|
94 |
if(jpegInstance == null){
|
|
95 |
try {
|
|
96 |
jpegInstance = ImageInfo.NewInstance(jpegUri, 0);
|
|
97 |
} catch (Exception e) {
|
|
98 |
fail("This case should have been covered by other tests.");
|
|
99 |
return null;
|
|
100 |
}
|
|
101 |
}
|
|
102 |
return jpegInstance;
|
|
103 |
}
|
|
104 |
|
|
105 |
private ImageInfo getTifInstance(){
|
|
106 |
if(tifInstance == null){
|
|
107 |
try {
|
|
108 |
tifInstance = ImageInfo.NewInstance(tiffUri, 0);
|
|
109 |
} catch (Exception e) {
|
|
110 |
fail("This case should have been covered by other tests.");
|
|
111 |
return null;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
return tifInstance;
|
|
115 |
}
|
|
116 |
|
|
117 |
private ImageInfo getRemotePngInstance() throws IOException{
|
|
118 |
if (!UriUtils.isInternetAvailable(remotePngUri)){
|
|
119 |
throw new IOException(OFFLINE);
|
|
120 |
}
|
|
121 |
if(pngInstance == null){
|
|
122 |
try {
|
|
123 |
pngInstance = ImageInfo.NewInstance(remotePngUri, 3000);
|
|
124 |
} catch (Exception e) {
|
|
125 |
fail("This case should have been covered by other tests.");
|
|
126 |
return null;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
return pngInstance;
|
|
130 |
}
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getWidth()}.
|
|
134 |
*/
|
|
135 |
@Test
|
|
136 |
public void testGetWidth() {
|
|
137 |
Assert.assertEquals(300, getJpegInstance().getWidth());
|
|
138 |
Assert.assertEquals(300, getTifInstance().getWidth());
|
|
139 |
|
|
140 |
try {
|
|
141 |
Assert.assertEquals(93, getRemotePngInstance().getWidth());
|
|
142 |
} catch (IOException e){
|
|
143 |
if(e.getMessage().equals(OFFLINE)){
|
|
144 |
logger.warn("test part skipped, since server is not available.");
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getHeight()}.
|
|
151 |
*/
|
|
152 |
@Test
|
|
153 |
public void testGetHeight() {
|
|
154 |
Assert.assertEquals(225, getJpegInstance().getHeight());
|
|
155 |
Assert.assertEquals(225, getTifInstance().getHeight());
|
|
156 |
|
|
157 |
try {
|
|
158 |
Assert.assertEquals(93, getRemotePngInstance().getHeight());
|
|
159 |
} catch (IOException e){
|
|
160 |
if(e.getMessage().equals(OFFLINE)){
|
|
161 |
logger.warn("test part skipped, since server is not available.");
|
|
162 |
}
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getBitPerPixel()}.
|
|
168 |
*/
|
|
169 |
@Test
|
|
170 |
public void testGetBitPerPixel() {
|
|
171 |
Assert.assertEquals(24, getJpegInstance().getBitPerPixel());
|
|
172 |
Assert.assertEquals(24, getTifInstance().getBitPerPixel());
|
|
173 |
|
|
174 |
try {
|
|
175 |
Assert.assertEquals(32, getRemotePngInstance().getBitPerPixel());
|
|
176 |
} catch (IOException e){
|
|
177 |
if(e.getMessage().equals(OFFLINE)){
|
|
178 |
logger.warn("test part skipped, since server is not available.");
|
|
179 |
}
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
/**
|
|
184 |
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getFormatName()}.
|
|
185 |
*/
|
|
186 |
@Test
|
|
187 |
public void testGetFormatName() {
|
|
188 |
Assert.assertEquals("JPEG (Joint Photographic Experts Group) Format", getJpegInstance().getFormatName());
|
|
189 |
Assert.assertEquals("TIFF Tag-based Image File Format", getTifInstance().getFormatName());
|
|
190 |
|
|
191 |
try {
|
|
192 |
Assert.assertEquals("PNG Portable Network Graphics", getRemotePngInstance().getFormatName());
|
|
193 |
} catch (IOException e){
|
|
194 |
if(e.getMessage().equals(OFFLINE)){
|
|
195 |
logger.warn("test part skipped, since server is not available.");
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
/**
|
|
201 |
* Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getMimeType()}.
|
|
202 |
*/
|
|
203 |
@Test
|
|
204 |
public void testGetMimeType() {
|
|
205 |
Assert.assertEquals(MimeType.JPEG.getMimeType(), getJpegInstance().getMimeType());
|
|
206 |
Assert.assertEquals(MimeType.TIFF.getMimeType(), getTifInstance().getMimeType());
|
|
207 |
|
|
208 |
try {
|
|
209 |
Assert.assertEquals(MimeType.PNG.getMimeType(), getRemotePngInstance().getMimeType());
|
|
210 |
} catch (IOException e){
|
|
211 |
if(e.getMessage().equals(OFFLINE)){
|
|
212 |
logger.warn("test part skipped, since server is not available.");
|
|
213 |
}
|
|
214 |
}
|
|
215 |
}
|
|
216 |
|
|
217 |
@Test
|
|
218 |
public void testGetLength(){
|
|
219 |
Assert.assertEquals(63500, getJpegInstance().getLength());
|
|
220 |
Assert.assertEquals(202926, getTifInstance().getLength());
|
|
221 |
|
|
222 |
try {
|
|
223 |
Assert.assertEquals(9143, getRemotePngInstance().getLength());
|
|
224 |
} catch (IOException e){
|
|
225 |
if(e.getMessage().equals(OFFLINE)){
|
|
226 |
logger.warn("test part skipped, since server is not available.");
|
|
227 |
}
|
|
228 |
}
|
|
229 |
}
|
|
230 |
|
|
231 |
@Test
|
|
232 |
public void testGetSuffix(){
|
|
233 |
Assert.assertEquals("jpg", getJpegInstance().getSuffix());
|
|
234 |
Assert.assertEquals("tif", getTifInstance().getSuffix());
|
|
235 |
|
|
236 |
try {
|
|
237 |
Assert.assertEquals("png", getRemotePngInstance().getSuffix());
|
|
238 |
} catch (IOException e){
|
|
239 |
if(e.getMessage().equals(OFFLINE)){
|
|
240 |
logger.warn("test part skipped, since server is not available.");
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
@Test
|
|
248 |
public void testReadMetaDataJpeg() throws IOException, HttpException{
|
|
249 |
ImageInfo instance = getJpegInstance();
|
|
250 |
|
|
251 |
instance.readMetaData(0);
|
|
252 |
|
|
253 |
Map<String, String> metaData = instance.getMetaData();
|
|
254 |
|
|
255 |
Assert.assertEquals(48, metaData.size());
|
|
256 |
}
|
|
257 |
|
|
258 |
|
|
259 |
@Test
|
|
260 |
public void testReadMetaDataTif() throws IOException, HttpException{
|
|
261 |
ImageInfo instance = getTifInstance();
|
|
262 |
|
|
263 |
instance.readMetaData(0);
|
|
264 |
|
|
265 |
Map<String, String> metaData = instance.getMetaData();
|
|
266 |
|
|
267 |
Assert.assertEquals(15, metaData.size());
|
|
268 |
}
|
|
269 |
|
|
270 |
@Test
|
|
271 |
public void testReadMetaDataRemotePng() throws IOException, HttpException{
|
|
272 |
|
|
273 |
try {
|
|
274 |
ImageInfo instance = getRemotePngInstance();
|
|
275 |
|
|
276 |
instance.readMetaData(3000);
|
|
277 |
|
|
278 |
Map<String, String> metaData = instance.getMetaData();
|
|
279 |
|
|
280 |
Assert.assertEquals(1, metaData.size());
|
|
281 |
|
|
282 |
} catch (IOException e){
|
|
283 |
if(e.getMessage().equals(OFFLINE)){
|
|
284 |
logger.warn("test testReadMetaDataRemotePng() skipped, since server is not available.");
|
|
285 |
}
|
|
286 |
}
|
|
287 |
|
|
288 |
|
|
289 |
}
|
240 |
290 |
}
|
fixing tests for offline usage