Project

General

Profile

Download (9.22 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.cdm.api.service.media;
5

    
6
import static org.junit.Assert.fail;
7

    
8
import java.io.IOException;
9
import java.net.URL;
10
import java.util.Map;
11

    
12
import org.apache.http.HttpException;
13
import org.apache.log4j.Logger;
14
import org.junit.Assert;
15
import org.junit.Before;
16
import org.junit.Test;
17

    
18
import eu.etaxonomy.cdm.common.URI;
19
import eu.etaxonomy.cdm.common.UriUtils;
20
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
21
import eu.etaxonomy.cdm.common.media.MimeType;
22

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

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

    
30
    public static final Logger logger = Logger.getLogger(MediaInfoFileReaderTest.class);
31

    
32
    private URI jpegUri;
33
    private URI tiffUri;
34
    private CdmImageInfo jpegInstance;
35
    private CdmImageInfo tifInstance;
36

    
37
    private URI remotePngUri;
38
    private CdmImageInfo pngInstance;
39

    
40
    @Before
41
    public void setUp() throws Exception {
42
        URL jpegUrl = MediaInfoFileReaderTest.class.getResource("./images/OregonScientificDS6639-DSC_0307-small.jpg");
43
        jpegUri = new URI(jpegUrl);
44

    
45
        URL tiffUrl = MediaInfoFileReaderTest.class.getResource("./images/OregonScientificDS6639-DSC_0307-small.tif");
46
        tiffUri = new URI(tiffUrl);
47

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

    
51
    @Test
52
    public void testNewInstanceJpeg(){
53
        try {
54
            new MediaInfoFileReader(jpegUri).readBaseInfo();
55
        } catch (Exception e) {
56
            fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
57
        }
58
    }
59

    
60
    @Test
61
    public void testNewInstanceTiff() {
62
        try {
63
            new MediaInfoFileReader(tiffUri).readBaseInfo();
64
        } catch (Exception e) {
65
            fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
66
        }
67
    }
68

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

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

    
86
        new MediaInfoFileReader(nonExistentUri).readBaseInfo();
87
    }
88

    
89
    private CdmImageInfo getJpegInstance(){
90
        if(jpegInstance == null){
91
            try {
92
                jpegInstance =  new MediaInfoFileReader(jpegUri).readBaseInfo().getCdmImageInfo();
93
            } catch (Exception e) {
94
                fail("This case should have been covered by other tests.");
95
                return null;
96
            }
97
        }
98
        return jpegInstance;
99
    }
100

    
101
    private CdmImageInfo getTifInstance(){
102
        if(tifInstance == null){
103
            try {
104
                tifInstance = new MediaInfoFileReader(tiffUri).readBaseInfo().getCdmImageInfo();
105
            } catch (Exception e) {
106
                fail("This case should have been covered by other tests.");
107
                return null;
108
            }
109
        }
110
        return tifInstance;
111
    }
112

    
113
    private CdmImageInfo getRemotePngBaseInfo() throws IOException{
114
        if (!UriUtils.isInternetAvailable(remotePngUri)){
115
            throw new IOException(OFFLINE);
116
        }
117
        if(pngInstance == null){
118
            try {
119
                pngInstance = new MediaInfoFileReader(remotePngUri).readBaseInfo().getCdmImageInfo();
120
            } catch (Exception e) {
121
                fail("This case should have been covered by other tests.");
122
                return null;
123
            }
124
        }
125
        return pngInstance;
126
    }
127

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

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

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

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

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

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

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

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

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

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

    
213
    @Test
214
    public void testGetLength(){
215
        Assert.assertEquals(55872, getJpegInstance().getLength());
216
        Assert.assertEquals(202926, getTifInstance().getLength());
217

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

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

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

    
241
    @Test
242
    public void testReadMetaDataJpeg() throws IOException, HttpException{
243

    
244
        CdmImageInfo instance = new MediaInfoFileReader(jpegUri).readMetaData().getCdmImageInfo();
245
        Map<String, String> metaData = instance.getMetaData();
246
        Assert.assertEquals(52, metaData.size());
247

    
248
        Assert.assertEquals("My taxon", metaData.get("Taxon"));
249
        Assert.assertEquals("on the road", metaData.get("Locality"));
250
        Assert.assertEquals("15.02.1955", metaData.get("Date"));
251
        Assert.assertEquals("Any person", metaData.get("Photographer"));
252
        Assert.assertEquals("My Keyword; Second Keyword", metaData.get("Keywords"));
253
    }
254

    
255

    
256
    @Test
257
    public void testReadMetaDataTif() throws IOException, HttpException{
258
        CdmImageInfo instance = new MediaInfoFileReader(tiffUri).readBaseInfo().readMetaData().getCdmImageInfo();
259
        Map<String, String> metaData = instance.getMetaData();
260
        Assert.assertEquals(15, metaData.size());
261
    }
262

    
263
    @Test
264
    public void testReadMetaDataRemotePng() throws HttpException {
265

    
266
        try {
267
            CdmImageInfo instance = new MediaInfoFileReader(remotePngUri).readMetaData().getCdmImageInfo();
268
            Map<String, String> metaData = instance.getMetaData();
269
            Assert.assertEquals(1, metaData.size());
270

    
271
        } catch (IOException e){
272
            if(e.getMessage().equals(OFFLINE)){
273
                logger.warn("test testReadMetaDataRemotePng() skipped, since server is not available.");
274
            }
275
        }
276
    }
277
}
(1-1/2)