KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > media > format > MediaFormatUnitTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.test.media.format;
9
10 import java.io.File JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
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 /**
33  * JBoss Media TestCase.
34  *
35  * @version <tt>$Revision: 1.1 $</tt>
36  * @author <a HREF="mailto:ricardoarguello@users.sourceforge.net">Ricardo Argüello</a>
37  */

38 public class MediaFormatUnitTestCase extends TestCase
39 {
40    private static final String JavaDoc DATA_DIR = "media/format/data/";
41    private static final String JavaDoc IMAGE_NAME = "image.png";
42    private static final String JavaDoc IMAGE_RESOURCE_NAME = DATA_DIR + IMAGE_NAME;
43    private static final String JavaDoc IMAGE_MIME_TYPE = "image/png";
44
45    private static final Logger log =
46       Logger.getLogger(MediaFormatUnitTestCase.class);
47
48    private File JavaDoc mediaFile;
49    private InputStream JavaDoc contentStream;
50    private Media media1, media2;
51    private byte[] imageData;
52
53    public MediaFormatUnitTestCase(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    protected void setUp() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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    /**
95     * Test Media.readContent(long, byte[]) when the position is the same size
96     * of the media content size.
97     */

98    public void testMediaContent() throws Exception JavaDoc
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 JavaDoc
106    {
107       MediaFormatRegistry registry = MediaFormatRegistry.SINGLETON;
108       Iterator JavaDoc it = registry.getFileExtensions();
109
110       if (it == null)
111       {
112          fail();
113       }
114
115       while (it.hasNext())
116       {
117          String JavaDoc fileExtension = (String JavaDoc) it.next();
118          //log.info("fileExtension: " + fileExtension);
119
}
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 JavaDoc
172    {
173       MediaHeader mediaHeader = media1.getHeader();
174
175       String JavaDoc[] 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 JavaDoc(263), mediaHeader.getField("width"));
180       assertEquals(new Integer JavaDoc(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 JavaDoc
196    {
197       // Image I/O support:
198
Map JavaDoc mediaFormats = IIOMediaFormatFactory.createMediaFormats();
199
200       // MPEG support:
201
mediaFormats.put("mp3", new MpegAudioFormat());
202
203       // Ogg Vorbis support:
204
mediaFormats.put("ogg", new OggVorbisFormat());
205
206       MediaFormatRegistry mediaFormatRegistry = MediaFormatRegistry.SINGLETON;
207
208       Iterator JavaDoc it = mediaFormats.keySet().iterator();
209
210       while (it.hasNext())
211       {
212          String JavaDoc extension = (String JavaDoc) it.next();
213          MediaFormat mediaFormat = (MediaFormat) mediaFormats.get(extension);
214          mediaFormatRegistry.bind(extension, mediaFormat);
215       }
216    }
217
218    public static void deinitializeMappings() throws Exception JavaDoc
219    {
220       // Image I/O support:
221
Map JavaDoc mediaFormats = IIOMediaFormatFactory.createMediaFormats();
222
223       // MPEG support:
224
mediaFormats.put("mp3", new MpegAudioFormat());
225
226       // Ogg Vorbis support:
227
mediaFormats.put("ogg", new OggVorbisFormat());
228
229       MediaFormatRegistry mediaFormatRegistry = MediaFormatRegistry.SINGLETON;
230
231       Iterator JavaDoc it = mediaFormats.keySet().iterator();
232
233       while (it.hasNext())
234       {
235          String JavaDoc extension = (String JavaDoc) it.next();
236          mediaFormatRegistry.unbind(extension);
237       }
238    }
239 }
Popular Tags