4 package eu
.etaxonomy
.cdm
.common
.media
;
6 import static org
.junit
.Assert
.fail
;
8 import java
.io
.IOException
;
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
;
19 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
24 public class CdmImageInfoTest
{
26 private static final String OFFLINE
= "OFFLINE";
28 public static final Logger logger
= Logger
.getLogger(CdmImageInfoTest
.class);
32 private CdmImageInfo jpegInstance
;
33 private CdmImageInfo tifInstance
;
35 private URI remotePngUri
;
36 private CdmImageInfo pngInstance
;
39 * @throws java.lang.Exception
42 public void setUp() throws Exception
{
43 URL jpegUrl
= CdmImageInfoTest
.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.jpg");
44 jpegUri
= jpegUrl
.toURI();
46 URL tiffUrl
= CdmImageInfoTest
.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.tif");
47 tiffUri
= tiffUrl
.toURI();
49 remotePngUri
= URI
.create("https://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
53 public void testNewInstanceJpeg(){
55 CdmImageInfo
.NewInstance(jpegUri
, 0);
56 } catch (Exception e
) {
57 fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
62 public void testNewInstanceTiff() {
64 CdmImageInfo
.NewInstance(tiffUri
, 0);
65 } catch (Exception e
) {
66 fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
71 public void testNewInstanceRemotePng() {
72 if(UriUtils
.isInternetAvailable(remotePngUri
)){
74 CdmImageInfo
.NewInstance(remotePngUri
, 3000);
75 } catch (Exception e
) {
76 fail("NewInstance method should not throw exceptions for existing uncorrupted images.");
79 logger
.warn("test testNewInstanceRemotePng() skipped, since server is not available");
83 @Test(expected
=IOException
.class)
84 public void testNewInstanceFileDoesNotExist() throws HttpException
, IOException
{
85 URI nonExistentUri
= URI
.create("file:///nonExistentImage.jpg");
87 CdmImageInfo
.NewInstance(nonExistentUri
, 0);
90 private CdmImageInfo
getJpegInstance(){
91 if(jpegInstance
== null){
93 jpegInstance
= CdmImageInfo
.NewInstance(jpegUri
, 0);
94 } catch (Exception e
) {
95 fail("This case should have been covered by other tests.");
102 private CdmImageInfo
getTifInstance(){
103 if(tifInstance
== null){
105 tifInstance
= CdmImageInfo
.NewInstance(tiffUri
, 0);
106 } catch (Exception e
) {
107 fail("This case should have been covered by other tests.");
114 private CdmImageInfo
getRemotePngInstance() throws IOException
{
115 if (!UriUtils
.isInternetAvailable(remotePngUri
)){
116 throw new IOException(OFFLINE
);
118 if(pngInstance
== null){
120 pngInstance
= CdmImageInfo
.NewInstance(remotePngUri
, 3000);
121 } catch (Exception e
) {
122 fail("This case should have been covered by other tests.");
130 * Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getWidth()}.
133 public void testGetWidth() {
134 Assert
.assertEquals(300, getJpegInstance().getWidth());
135 Assert
.assertEquals(300, getTifInstance().getWidth());
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.");
147 * Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getHeight()}.
150 public void testGetHeight() {
151 Assert
.assertEquals(225, getJpegInstance().getHeight());
152 Assert
.assertEquals(225, getTifInstance().getHeight());
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.");
164 * Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getBitPerPixel()}.
167 public void testGetBitPerPixel() {
168 Assert
.assertEquals(24, getJpegInstance().getBitPerPixel());
169 Assert
.assertEquals(24, getTifInstance().getBitPerPixel());
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.");
181 * Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getFormatName()}.
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());
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.");
198 * Test method for {@link eu.etaxonomy.cdm.common.media.CdmImageInfo#getMimeType()}.
201 public void testGetMimeType() {
202 Assert
.assertEquals(MimeType
.JPEG
.getMimeType(), getJpegInstance().getMimeType());
203 Assert
.assertEquals(MimeType
.TIFF
.getMimeType(), getTifInstance().getMimeType());
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.");
215 public void testGetLength(){
216 Assert
.assertEquals(55872, getJpegInstance().getLength());
217 Assert
.assertEquals(202926, getTifInstance().getLength());
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.");
229 public void testGetSuffix(){
230 Assert
.assertEquals("jpg", getJpegInstance().getSuffix());
231 Assert
.assertEquals("tif", getTifInstance().getSuffix());
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.");
245 public void testReadMetaDataJpeg() throws IOException
, HttpException
{
246 CdmImageInfo instance
= getJpegInstance();
248 instance
.readMetaData(0);
250 Map
<String
, String
> metaData
= instance
.getMetaData();
252 Assert
.assertEquals(52, metaData
.size());
254 Assert
.assertEquals("My taxon", metaData
.get("Taxon"));
255 Assert
.assertEquals("on the road", metaData
.get("Locality"));
256 Assert
.assertEquals("15.02.1955", metaData
.get("Date"));
257 Assert
.assertEquals("Any person", metaData
.get("Photographer"));
258 Assert
.assertEquals("My Keyword; Second Keyword", metaData
.get("Keywords"));
264 public void testReadMetaDataTif() throws IOException
, HttpException
{
265 CdmImageInfo instance
= getTifInstance();
267 instance
.readMetaData(0);
269 Map
<String
, String
> metaData
= instance
.getMetaData();
271 Assert
.assertEquals(15, metaData
.size());
275 public void testReadMetaDataRemotePng() throws HttpException
{
278 CdmImageInfo instance
= getRemotePngInstance();
280 instance
.readMetaData(3000);
282 Map
<String
, String
> metaData
= instance
.getMetaData();
284 Assert
.assertEquals(1, metaData
.size());
286 } catch (IOException e
){
287 if(e
.getMessage().equals(OFFLINE
)){
288 logger
.warn("test testReadMetaDataRemotePng() skipped, since server is not available.");