Project

General

Profile

Download (8.65 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
import eu.etaxonomy.cdm.common.media.ImageInfo;
21
import eu.etaxonomy.cdm.common.media.MimeType;
22

    
23
/**
24
 * @author n.hoffmann
25
 *
26
 */
27
public class ImageInfoTest {
28

    
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
    }
290
}
    (1-1/1)