Project

General

Profile

Download (4.07 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.common;
12

    
13

    
14
import java.io.File;
15
import java.io.IOException;
16
import java.net.MalformedURLException;
17
import java.net.URI;
18
import java.net.URL;
19

    
20
import org.apache.http.HttpException;
21
import org.apache.log4j.Logger;
22
import org.junit.After;
23
import org.junit.AfterClass;
24
import org.junit.Assert;
25
import org.junit.Before;
26
import org.junit.BeforeClass;
27
import org.junit.Test;
28

    
29
import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
30
import eu.etaxonomy.cdm.common.mediaMetaData.MetaDataFactory;
31
import eu.etaxonomy.cdm.common.mediaMetaData.MimeType;
32

    
33
/**
34
 * @author n.hoffmann
35
 * @created 13.11.2008
36
 * @version 1.0
37
 */
38
public class MediaMetaDataTest {
39
	private static final Logger logger = Logger.getLogger(MediaMetaDataTest.class);
40

    
41
	/**
42
	 * @throws java.lang.Exception
43
	 */
44
	@BeforeClass
45
	public static void setUpBeforeClass() throws Exception {
46
		if (logger.isDebugEnabled()){logger.debug("setUpBeforeClass");}
47
	}
48

    
49
	/**
50
	 * @throws java.lang.Exception
51
	 */
52
	@AfterClass
53
	public static void tearDownAfterClass() throws Exception {
54
	}
55

    
56
	/**
57
	 * @throws java.lang.Exception
58
	 */
59
	@Before
60
	public void setUp() throws Exception {
61
	}
62

    
63
	/**
64
	 * @throws java.lang.Exception
65
	 */
66
	@After
67
	public void tearDown() throws Exception {
68
	}
69
	
70
	/********************* TESTS 
71
	 * @throws IOException 
72
	 * @throws HttpException ********************/
73
	
74
	@Test
75
	public void readImageInfoFromFile() throws IOException, HttpException {
76
		File imageFile = new File("./src/test/resources/images/OregonScientificDS6639-DSC_0307-small.jpg");
77
		MetaDataFactory metaFactory = MetaDataFactory.getInstance();
78
		ImageMetaData imageMetaData = (ImageMetaData) metaFactory.readMediaData(imageFile.toURI(), MimeType.JPEG, 0);
79
		//imageMetaData.readFrom(imageFile);
80
		
81
		assertImageInfo(imageMetaData);		
82
		imageFile = new File("./src/test/resources/images/OregonScientificDS6639-DSC_0307-small.tif");
83
		
84
		 imageMetaData = (ImageMetaData) metaFactory.readMediaData(imageFile.toURI(), MimeType.IMAGE, 0);
85
		 assertTiffInfo(imageMetaData);		
86
	}
87
	
88
	@Test
89
	public void readImageInfoFromUrl() throws IOException, HttpException {
90
		try {
91
			
92
			//TODO make ready for windows
93
			//URL imageUrl = new URL("file://" + new File("").getAbsolutePath()+ "/src/test/resources/images/OregonScientificDS6639-DSC_0307-small.jpg");
94
			URL imageUrl = new URL("http://wp5.e-taxonomy.eu/media/palmae/photos/palm_tc_100447_6.jpg");
95
			MetaDataFactory metaFactory = MetaDataFactory.getInstance();
96
			ImageMetaData imageMetaData;
97
			
98
			try {
99
				imageMetaData = (ImageMetaData) metaFactory.readMediaData(CdmUtils.string2Uri(imageUrl.toString()), MimeType.JPEG, 30000);
100
				//imageMetaData.readImageMetaData(imageUrl);
101
				
102
				Assert.assertNotNull(imageMetaData);
103
			} catch (Exception e) {
104
				if (UriUtils.isInternetAvailable(URI.create(imageUrl.toString()))){
105
					Assert.fail("Exception when reading media meta data though internet is available");
106
				}else{
107
					logger.warn("Internet not available. Can't test reading image meta data");
108
				}
109
			}
110

    
111
			
112
		} catch (MalformedURLException e) {
113
			e.printStackTrace();
114
		}
115
	}
116
	
117
	private void assertImageInfo (ImageMetaData imageMetaData){
118
		Assert.assertEquals(24, imageMetaData.getBitPerPixel());
119
		Assert.assertEquals("JPEG (Joint Photographic Experts Group) Format", imageMetaData.getFormatName());
120
		Assert.assertEquals(300, imageMetaData.getWidth());
121
		Assert.assertEquals(225, imageMetaData.getHeight());
122
		Assert.assertEquals("image/jpeg", imageMetaData.getMimeType());
123
		
124
	}
125
	
126
	private void assertTiffInfo (ImageMetaData imageMetaData){
127
		
128
		Assert.assertEquals(24, imageMetaData.getBitPerPixel());
129
		Assert.assertEquals("TIFF Tag-based Image File Format", imageMetaData.getFormatName());
130
		Assert.assertEquals(300, imageMetaData.getWidth());
131
		Assert.assertEquals(225, imageMetaData.getHeight());
132
		Assert.assertEquals("image/tiff", imageMetaData.getMimeType());
133
	}
134
}
(2-2/5)