Project

General

Profile

Download (8.61 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
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
    /**
39
     * @throws java.lang.Exception
40
     */
41
    @Before
42
    public void setUp() throws Exception {
43
        URL jpegUrl = CdmImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.jpg");
44
        jpegUri = jpegUrl.toURI();
45

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

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

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

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

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

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

    
87
        CdmImageInfo.NewInstance(nonExistentUri, 0);
88
    }
89

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
242

    
243

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

    
248
        instance.readMetaData(0);
249

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

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

    
255

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

    
260
        instance.readMetaData(0);
261

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

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

    
267
    @Test
268
    public void testReadMetaDataRemotePng() throws HttpException{
269

    
270
        try {
271
            CdmImageInfo instance = getRemotePngInstance();
272

    
273
            instance.readMetaData(3000);
274

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

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

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

    
285

    
286
    }
287
}
    (1-1/1)