Project

General

Profile

Download (8.92 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.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

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

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

    
28
    public static final Logger logger = Logger.getLogger(CdmImageInfoTest.class);
29

    
30
    private URI jpegUri;
31
    private URI tiffUri;
32
    private CdmImageInfo jpegInstance;
33
    private CdmImageInfo tifInstance;
34

    
35
    private URI remotePngUri;
36
    private CdmImageInfo pngInstance;
37

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

    
43
        URL tiffUrl = CdmImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.tif");
44
        tiffUri = new URI(tiffUrl);
45

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

    
49
    @Test
50
    public void testNewInstanceJpeg(){
51
        try {
52
            CdmImageInfo.NewInstance(jpegUri, 0);
53
        } catch (Exception e) {
54
            fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
55
        }
56
    }
57

    
58
    @Test
59
    public void testNewInstanceTiff() {
60
        try {
61
            CdmImageInfo.NewInstance(tiffUri, 0);
62
        } catch (Exception e) {
63
            fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
64
        }
65
    }
66

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

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

    
84
        CdmImageInfo.NewInstance(nonExistentUri, 0);
85
    }
86

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
239
    @Test
240
    public void testReadMetaDataJpeg() throws IOException, HttpException{
241
        CdmImageInfo instance = getJpegInstance();
242

    
243
        instance.readMetaData(0);
244
        Map<String, String> metaData = instance.getMetaData();
245
        Assert.assertEquals(52, metaData.size());
246

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

    
254

    
255
    @Test
256
    public void testReadMetaDataTif() throws IOException, HttpException{
257
        CdmImageInfo instance = getTifInstance();
258
        instance.readMetaData(0);
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 = getRemotePngInstance();
268
            instance.readMetaData(3000);
269
            Map<String, String> metaData = instance.getMetaData();
270
            Assert.assertEquals(1, metaData.size());
271

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