Project

General

Profile

Download (8.55 KB) Statistics
| Branch: | Tag: | Revision:
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.apache.log4j.Logger;
15
import org.junit.Assert;
16
import org.junit.Before;
17
import org.junit.Test;
18

    
19
import eu.etaxonomy.cdm.common.UriUtils;
20

    
21
/**
22
 * @author n.hoffmann
23
 *
24
 */
25
public class ImageInfoTest {
26

    
27
    private static final String OFFLINE = "OFFLINE";
28

    
29
    public static final Logger logger = Logger.getLogger(ImageInfoTest.class);
30

    
31
    private URI jpegUri;
32
    private URI tiffUri;
33
    private ImageInfo jpegInstance;
34
    private ImageInfo tifInstance;
35

    
36
    private URI remotePngUri;
37
    private ImageInfo pngInstance;
38

    
39
    /**
40
     * @throws java.lang.Exception
41
     */
42
    @Before
43
    public void setUp() throws Exception {
44
        URL jpegUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.jpg");
45
        jpegUri = jpegUrl.toURI();
46

    
47
        URL tiffUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.tif");
48
        tiffUri = tiffUrl.toURI();
49

    
50
        remotePngUri = URI.create("https://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
51
    }
52

    
53
    @Test
54
    public void testNewInstanceJpeg(){
55
        try {
56
            ImageInfo.NewInstance(jpegUri, 0);
57
        } catch (Exception e) {
58
            fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
59
        }
60
    }
61

    
62
    @Test
63
    public void testNewInstanceTiff() {
64
        try {
65
            ImageInfo.NewInstance(tiffUri, 0);
66
        } catch (Exception e) {
67
            fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
68
        }
69
    }
70

    
71
    @Test
72
    public void testNewInstanceRemotePng() {
73
        if(UriUtils.isInternetAvailable(remotePngUri)){
74
            try {
75
                ImageInfo.NewInstance(remotePngUri, 3000);
76
            } catch (Exception e) {
77
                fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
78
            }
79
        } else {
80
            logger.warn("test testNewInstanceRemotePng() skipped, since server is not available");
81
        }
82
    }
83

    
84
    @Test(expected=IOException.class)
85
    public void testNewInstanceFileDoesNotExist() throws HttpException, IOException {
86
        URI nonExistentUri = URI.create("file:///nonExistentImage.jpg");
87

    
88
        ImageInfo.NewInstance(nonExistentUri, 0);
89
    }
90

    
91
    private ImageInfo getJpegInstance(){
92
        if(jpegInstance == null){
93
            try {
94
                jpegInstance = ImageInfo.NewInstance(jpegUri, 0);
95
            } catch (Exception e) {
96
                fail("This case should have been covered by other tests.");
97
                return null;
98
            }
99
        }
100
        return jpegInstance;
101
    }
102

    
103
    private ImageInfo getTifInstance(){
104
        if(tifInstance == null){
105
            try {
106
                tifInstance = ImageInfo.NewInstance(tiffUri, 0);
107
            } catch (Exception e) {
108
                fail("This case should have been covered by other tests.");
109
                return null;
110
            }
111
        }
112
        return tifInstance;
113
    }
114

    
115
    private ImageInfo getRemotePngInstance() throws IOException{
116
        if (!UriUtils.isInternetAvailable(remotePngUri)){
117
            throw new IOException(OFFLINE);
118
        }
119
        if(pngInstance == null){
120
            try {
121
                pngInstance = ImageInfo.NewInstance(remotePngUri, 3000);
122
            } catch (Exception e) {
123
                fail("This case should have been covered by other tests.");
124
                return null;
125
            }
126
        }
127
        return pngInstance;
128
    }
129

    
130
    /**
131
     * Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getWidth()}.
132
     */
133
    @Test
134
    public void testGetWidth() {
135
        Assert.assertEquals(300, getJpegInstance().getWidth());
136
        Assert.assertEquals(300, getTifInstance().getWidth());
137

    
138
        try {
139
            Assert.assertEquals(93, getRemotePngInstance().getWidth());
140
        } catch (IOException e){
141
            if(e.getMessage().equals(OFFLINE)){
142
                logger.warn("test part skipped, since server is not available.");
143
            }
144
        }
145
    }
146

    
147
    /**
148
     * Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getHeight()}.
149
     */
150
    @Test
151
    public void testGetHeight() {
152
        Assert.assertEquals(225, getJpegInstance().getHeight());
153
        Assert.assertEquals(225, getTifInstance().getHeight());
154

    
155
        try {
156
            Assert.assertEquals(93, getRemotePngInstance().getHeight());
157
        } catch (IOException e){
158
            if(e.getMessage().equals(OFFLINE)){
159
                logger.warn("test part skipped, since server is not available.");
160
            }
161
        }
162
    }
163

    
164
    /**
165
     * Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getBitPerPixel()}.
166
     */
167
    @Test
168
    public void testGetBitPerPixel() {
169
        Assert.assertEquals(24, getJpegInstance().getBitPerPixel());
170
        Assert.assertEquals(24, getTifInstance().getBitPerPixel());
171

    
172
        try {
173
            Assert.assertEquals(32, getRemotePngInstance().getBitPerPixel());
174
        } catch (IOException e){
175
            if(e.getMessage().equals(OFFLINE)){
176
                logger.warn("test part skipped, since server is not available.");
177
            }
178
        }
179
    }
180

    
181
    /**
182
     * Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getFormatName()}.
183
     */
184
    @Test
185
    public void testGetFormatName() {
186
        Assert.assertEquals("JPEG (Joint Photographic Experts Group) Format", getJpegInstance().getFormatName());
187
        Assert.assertEquals("TIFF Tag-based Image File Format", getTifInstance().getFormatName());
188

    
189
        try {
190
            Assert.assertEquals("PNG Portable Network Graphics", getRemotePngInstance().getFormatName());
191
        } catch (IOException e){
192
            if(e.getMessage().equals(OFFLINE)){
193
                logger.warn("test part skipped, since server is not available.");
194
            }
195
        }
196
    }
197

    
198
    /**
199
     * Test method for {@link eu.etaxonomy.cdm.common.media.ImageInfo#getMimeType()}.
200
     */
201
    @Test
202
    public void testGetMimeType() {
203
        Assert.assertEquals(MimeType.JPEG.getMimeType(), getJpegInstance().getMimeType());
204
        Assert.assertEquals(MimeType.TIFF.getMimeType(), getTifInstance().getMimeType());
205

    
206
        try {
207
            Assert.assertEquals(MimeType.PNG.getMimeType(), getRemotePngInstance().getMimeType());
208
        } catch (IOException e){
209
            if(e.getMessage().equals(OFFLINE)){
210
                logger.warn("test part skipped, since server is not available.");
211
            }
212
        }
213
    }
214

    
215
    @Test
216
    public void testGetLength(){
217
        Assert.assertEquals(63500, getJpegInstance().getLength());
218
        Assert.assertEquals(202926, getTifInstance().getLength());
219

    
220
        try {
221
            Assert.assertEquals(9143, getRemotePngInstance().getLength());
222
        } catch (IOException e){
223
            if(e.getMessage().equals(OFFLINE)){
224
                logger.warn("test part skipped, since server is not available.");
225
            }
226
        }
227
    }
228

    
229
    @Test
230
    public void testGetSuffix(){
231
        Assert.assertEquals("jpg", getJpegInstance().getSuffix());
232
        Assert.assertEquals("tif", getTifInstance().getSuffix());
233

    
234
        try {
235
            Assert.assertEquals("png", getRemotePngInstance().getSuffix());
236
        } catch (IOException e){
237
            if(e.getMessage().equals(OFFLINE)){
238
                logger.warn("test part skipped, since server is not available.");
239
            }
240
        }
241
    }
242

    
243

    
244

    
245
    @Test
246
    public void testReadMetaDataJpeg() throws IOException, HttpException{
247
        ImageInfo instance = getJpegInstance();
248

    
249
        instance.readMetaData(0);
250

    
251
        Map<String, String> metaData = instance.getMetaData();
252

    
253
        Assert.assertEquals(48, metaData.size());
254
    }
255

    
256

    
257
    @Test
258
    public void testReadMetaDataTif() throws IOException, HttpException{
259
        ImageInfo instance = getTifInstance();
260

    
261
        instance.readMetaData(0);
262

    
263
        Map<String, String> metaData = instance.getMetaData();
264

    
265
        Assert.assertEquals(15, metaData.size());
266
    }
267

    
268
    @Test
269
    public void testReadMetaDataRemotePng() throws IOException, HttpException{
270

    
271
        try {
272
            ImageInfo instance = getRemotePngInstance();
273

    
274
            instance.readMetaData(3000);
275

    
276
            Map<String, String> metaData = instance.getMetaData();
277

    
278
            Assert.assertEquals(1, metaData.size());
279

    
280
        } catch (IOException e){
281
            if(e.getMessage().equals(OFFLINE)){
282
                logger.warn("test testReadMetaDataRemotePng() skipped, since server is not available.");
283
            }
284
        }
285

    
286

    
287
    }
288
}
    (1-1/1)