1 7 8 package org.jboss.test.media.format; 9 10 import java.io.File ; 11 import java.io.InputStream ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import javax.emb.FormatAlreadyBoundException; 16 import javax.emb.FormatNotFoundException; 17 import javax.emb.GenericMediaFormat; 18 import javax.emb.Media; 19 import javax.emb.MediaBean; 20 import javax.emb.MediaFormat; 21 import javax.emb.MediaFormatRegistry; 22 import javax.emb.MediaHeader; 23 24 import junit.framework.TestCase; 25 26 import org.jboss.logging.Logger; 27 import org.jboss.media.format.audio.mpeg.MpegAudioFormat; 28 import org.jboss.media.format.audio.oggvorbis.OggVorbisFormat; 29 import org.jboss.media.format.image.iio.IIOMediaFormatFactory; 30 import org.jboss.media.util.ResourceUtils; 31 32 38 public class MediaFormatUnitTestCase extends TestCase 39 { 40 private static final String DATA_DIR = "media/format/data/"; 41 private static final String IMAGE_NAME = "image.png"; 42 private static final String IMAGE_RESOURCE_NAME = DATA_DIR + IMAGE_NAME; 43 private static final String IMAGE_MIME_TYPE = "image/png"; 44 45 private static final Logger log = 46 Logger.getLogger(MediaFormatUnitTestCase.class); 47 48 private File mediaFile; 49 private InputStream contentStream; 50 private Media media1, media2; 51 private byte[] imageData; 52 53 public MediaFormatUnitTestCase(String name) 54 { 55 super(name); 56 } 57 58 protected void setUp() throws Exception 59 { 60 initializeMappings(); 61 62 mediaFile = 63 ResourceUtils.createFileFromResource( 64 IMAGE_RESOURCE_NAME, 65 "temp", 66 IMAGE_NAME); 67 contentStream = 68 ResourceUtils.createStreamFromResource(IMAGE_RESOURCE_NAME); 69 media1 = new MediaBean(mediaFile, IMAGE_MIME_TYPE); 70 media2 = 71 new MediaBean(contentStream, IMAGE_MIME_TYPE, mediaFile.getName()); 72 imageData = ResourceUtils.loadBinaryData(IMAGE_RESOURCE_NAME); 73 } 74 75 protected void tearDown() throws Exception 76 { 77 deinitializeMappings(); 78 79 mediaFile = null; 80 contentStream = null; 81 media1 = null; 82 media2 = null; 83 imageData = null; 84 } 85 86 public void testMediaBeanProperties() throws Exception 87 { 88 assertEquals(media1.getName(), media2.getName()); 89 assertEquals(media1.getMimeType(), media2.getMimeType()); 90 assertEquals(media1.getSize(), media2.getSize()); 91 assertEquals(media1.getContent(), media2.getContent()); 92 } 93 94 98 public void testMediaContent() throws Exception 99 { 100 byte[] buffer = new byte[(int) media1.getSize()]; 101 int i = media1.readContent(media1.getSize(), buffer); 102 assertEquals(buffer, new byte[(int) media1.getSize()]); 103 } 104 105 public void testMediaFormatRegistry() throws Exception 106 { 107 MediaFormatRegistry registry = MediaFormatRegistry.SINGLETON; 108 Iterator it = registry.getFileExtensions(); 109 110 if (it == null) 111 { 112 fail(); 113 } 114 115 while (it.hasNext()) 116 { 117 String fileExtension = (String ) it.next(); 118 } 120 121 try 122 { 123 registry.bind("foo", new GenericMediaFormat()); 124 } 125 catch (FormatAlreadyBoundException e) 126 { 127 fail(); 128 } 129 130 try 131 { 132 registry.bind("foo", new GenericMediaFormat()); 133 fail(); 134 } 135 catch (FormatAlreadyBoundException e) 136 { 137 } 138 139 MediaFormatRegistry registry2 = MediaFormatRegistry.SINGLETON; 140 141 try 142 { 143 MediaFormat foo = registry2.lookup("foo"); 144 } 145 catch (FormatNotFoundException e) 146 { 147 fail(); 148 } 149 150 registry.rebind("foo", new GenericMediaFormat()); 151 152 try 153 { 154 registry.unbind("foo"); 155 } 156 catch (FormatNotFoundException e) 157 { 158 fail(); 159 } 160 161 try 162 { 163 MediaFormat foo = registry2.lookup("foo"); 164 fail(); 165 } 166 catch (FormatNotFoundException e) 167 { 168 } 169 } 170 171 public void testHeaderExtraction() throws Exception 172 { 173 MediaHeader mediaHeader = media1.getHeader(); 174 175 String [] fields = mediaHeader.getFieldNames(); 176 for (int i = 0; i < fields.length; i++) 177 log.info(fields[i] + "=" + mediaHeader.getField(fields[i])); 178 179 assertEquals(new Integer (263), mediaHeader.getField("width")); 180 assertEquals(new Integer (54), mediaHeader.getField("height")); 181 assertEquals("8", mediaHeader.getField("bitDepth")); 182 assertEquals("RGB", mediaHeader.getField("colorType")); 183 assertEquals("deflate", mediaHeader.getField("compressionMethod")); 184 assertEquals("adaptive", mediaHeader.getField("filterMethod")); 185 assertEquals("none", mediaHeader.getField("interlaceMethod")); 186 } 187 188 private void assertEquals(byte[] expected, byte[] actual) 189 { 190 assertEquals(expected.length, actual.length); 191 for (int i = 0; i < expected.length; ++i) 192 assertEquals(expected[i], actual[i]); 193 } 194 195 public static void initializeMappings() throws Exception 196 { 197 Map mediaFormats = IIOMediaFormatFactory.createMediaFormats(); 199 200 mediaFormats.put("mp3", new MpegAudioFormat()); 202 203 mediaFormats.put("ogg", new OggVorbisFormat()); 205 206 MediaFormatRegistry mediaFormatRegistry = MediaFormatRegistry.SINGLETON; 207 208 Iterator it = mediaFormats.keySet().iterator(); 209 210 while (it.hasNext()) 211 { 212 String extension = (String ) it.next(); 213 MediaFormat mediaFormat = (MediaFormat) mediaFormats.get(extension); 214 mediaFormatRegistry.bind(extension, mediaFormat); 215 } 216 } 217 218 public static void deinitializeMappings() throws Exception 219 { 220 Map mediaFormats = IIOMediaFormatFactory.createMediaFormats(); 222 223 mediaFormats.put("mp3", new MpegAudioFormat()); 225 226 mediaFormats.put("ogg", new OggVorbisFormat()); 228 229 MediaFormatRegistry mediaFormatRegistry = MediaFormatRegistry.SINGLETON; 230 231 Iterator it = mediaFormats.keySet().iterator(); 232 233 while (it.hasNext()) 234 { 235 String extension = (String ) it.next(); 236 mediaFormatRegistry.unbind(extension); 237 } 238 } 239 } | Popular Tags |