001: package com.ibm.emb.test;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.util.zip.GZIPInputStream;
008:
009: import javax.emb.ContentAccessException;
010: import javax.emb.GenericMediaFormat;
011: import javax.emb.Media;
012: import javax.emb.MediaBean;
013: import javax.emb.MediaException;
014: import javax.emb.MediaFormatRegistry;
015:
016: import org.objectweb.jonas.emb.mfb.formats.video.MpegVideoFormat;
017:
018: import com.ibm.emb.junit.EMBTestRunner;
019:
020: /**
021: * <pre>
022: *
023: *
024: *
025: * Line Item: MFB API
026: * Subcategory 1: javax.emb
027: * Subcategory 2: MediaBean
028: *
029: * This is the test class for javax.emb.MediaBean. Many of the tests
030: * are implemented in the super class MediaTest and only a few initialization
031: * methods are provided here.
032: *
033: *
034: *
035: * </pre>
036: */
037: public class MediaBeanTest extends MediaTest {
038:
039: public MediaBeanTest(String name) throws MediaException {
040: super (name);
041: }
042:
043: public static void main(String args[]) {
044: EMBTestRunner.run(MediaBeanTest.class);
045: }
046:
047: /**
048: * creates an instance of MediaBean with the given parameters used in super
049: * class of MediaTest
050: */
051: public Media createMedia(File inputFile, String mimeType)
052: throws MediaException {
053: return new MediaBean(inputFile, mimeType);
054: }
055:
056: /**
057: * creates an instance of MediaBean with the given parameters used in super
058: * class of MediaTest
059: */
060: public Media createMedia(InputStream in, String mimeType,
061: String name) throws MediaException {
062: return new MediaBean(in, mimeType, name);
063: }
064:
065: /**
066: * <pre>
067: *
068: *
069: *
070: * Testcase Name: MediaBean(File mediaFile, String mimeType)
071: * Testcase Number: EMB011
072: *
073: * setup: media has to be provided in local file system
074: *
075: * test procedure:
076: * I.
077: * a) java.io.file pointing to media file is provided in the constructor call.
078: * ->1.instance is created
079: * ->2.mimetype is queried and verified
080: * ->3.media file content size is checked by comparing byte array size and file size.
081: * ->4.Media.getName is not null and name contains exactly 1 dot.
082: * b) provide null as mime-type -> same as getFormat().getDefaultMimeType()
083: * c) provide "test" as mime-type -> "test"
084: *
085: * II. call MediaBean(null, null) -> NullPointerException
086: *
087: * III. call MediaBean with nonexistent file and null mimetype -> ContentAccessException
088: *
089: *
090: *
091: * </pre>
092: */
093: public void testEMB011() throws IOException, MediaException {
094: File mpegTestFile = new File(embTestConfig.getMediaDir()
095: + File.separatorChar
096: + embTestConfig.getMpegMovieName1());
097:
098: // register media format
099: javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
100: formatRegistry.rebind("mpeg", new MpegVideoFormat());
101: formatRegistry.rebind("test", new GenericMediaFormat());
102:
103: MediaBean testInstance = new MediaBean(mpegTestFile,
104: "video/mpeg");
105: //
106: // test I a1
107: //
108: assertNotNull("test I a1: ", testInstance);
109: testTrace("test I a1 passed");
110: //
111: // test I a2
112: //
113: assertEquals("test I a2: ", "video/mpeg", testInstance
114: .getMimeType());
115: testTrace("test I a2 passed");
116: //
117: // test I a3
118: //
119: assertEquals("test I a3: ", mpegTestFile.length(), testInstance
120: .getSize());
121: testTrace("test I a3 passed");
122: //
123: // test I a4
124: //
125: int index = testInstance.getName().indexOf(".");
126: int postIndex = testInstance.getName().indexOf(".", index + 1);
127: assertTrue("test I a4 :", (testInstance.getName() != null)
128: && ((index != -1) && (postIndex == -1)));
129: testTrace("test I a4 passed");
130: //
131: // test I b
132: //
133: //
134: testInstance = new MediaBean(mpegTestFile, null);
135: assertEquals("test I b: ", testInstance.getFormat()
136: .getDefaultMimeType(), testInstance.getMimeType());
137: testTrace("test I b passed");
138: //
139: // test I c
140: //
141: testInstance = new MediaBean(mpegTestFile, "test");
142: assertEquals("test I c: ", "test", testInstance.getMimeType());
143: testTrace("test I c passed");
144: //
145: // test II
146: //
147: try {
148: testInstance = new MediaBean(null, "test");
149: fail("test II : Should throw a NullPointerException");
150: } catch (NullPointerException e) {
151: testTrace("test II passed");
152: } catch (Exception e) {
153: fail("test II: Should throw a NullPointerException but threw "
154: + e.toString());
155: }
156: //
157: // test III
158: //
159: try {
160: testInstance = new MediaBean(new File(
161: "nonexistingFile.mpeg"), "mpeg");
162: fail("test III: Should throw an ContentAccessException");
163: } catch (javax.emb.ContentAccessException e) {
164: testTrace("test III passed");
165: } catch (Exception e) {
166: fail("test III: Should throw a ContentAccessException but threw "
167: + e.toString());
168: }
169:
170: succeed();
171: }
172:
173: /**
174: * <pre>
175: *
176: *
177: *
178: * Testcase Name: MediaBean(InputStream contentStream, String mimeType, String name)
179: * Testcase Number: EMB012
180: *
181: * setup: input stream has to be provided by using a file input stream
182: *
183: * test procedure:
184: * I.
185: * a)mimetype is null
186: * ->1.mimetype = getFormat().getDefaultMimeType()
187: * ->2.stream content size is checked by comparing byte array size of Media.getContent() and stream size
188: * b)mimetype is "test" (parameter "name" has to be != null) -> mimetype = "test"
189: *
190: * II. contentStream is null -> NullPointerException
191: *
192: * III. name is null -> NullPointerException
193: *
194: * IV. create MediaBean using input stream from corrupt GZIP file (can open but not read)
195: * -> ContentAccessException
196: *
197: *
198: *
199: * </pre>
200: */
201: public void testEMB012() throws IOException, MediaException {
202:
203: MediaBean testInstance = null;
204: // file input stream
205: FileInputStream fileInputStream = new FileInputStream(
206: embTestConfig.getMediaDir() + File.separatorChar
207: + embTestConfig.getMpegMovieName1());
208:
209: // register media format
210: javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
211: formatRegistry.rebind("test",
212: new javax.emb.GenericMediaFormat());
213: formatRegistry.rebind("mpeg", new MpegVideoFormat());
214:
215: //
216: // test 1 a
217: //
218: int streamSize = 0;
219: while (fileInputStream.read() != -1) {
220: streamSize++;
221: }
222: fileInputStream.close();
223: fileInputStream = new FileInputStream(embTestConfig
224: .getMediaDir()
225: + File.separatorChar
226: + embTestConfig.getMpegMovieName1());
227: testInstance = new MediaBean(fileInputStream, null, "test.mpeg");
228: assertEquals("test I a1: ", testInstance.getFormat()
229: .getDefaultMimeType(), testInstance.getMimeType());
230: assertEquals("test I a2: ", streamSize, testInstance.getSize());
231: testTrace("test I a passed");
232: //
233: // test 1 b
234: //
235: fileInputStream = new FileInputStream(embTestConfig
236: .getMediaDir()
237: + File.separatorChar
238: + embTestConfig.getMpegMovieName1());
239: testInstance = new MediaBean(fileInputStream, "test",
240: "test.mpeg");
241: assertEquals("test I b: ", "test", testInstance.getMimeType());
242: testTrace("test I b passed");
243: //
244: // test 2
245: //
246: try {
247: testInstance = new MediaBean(null, "test", "test.mpeg");
248: fail(" test II: Should throw a NullPointerException");
249: } catch (NullPointerException e) {
250: testTrace("test II passed");
251: } catch (Exception e) {
252: fail("test II: should throw a NullPointerException but threw "
253: + e.toString());
254: }
255: //
256: // test 3
257: //
258: fileInputStream = new FileInputStream(embTestConfig
259: .getMediaDir()
260: + File.separatorChar
261: + embTestConfig.getMpegMovieName1());
262: try {
263: testInstance = new MediaBean(fileInputStream, "test", null);
264: fail(" test III: Should throw a NullPointerException");
265: } catch (NullPointerException e) {
266: testTrace("test III passed");
267: } catch (Exception e) {
268: fail("test III: Should throw a NullPointerException but threw "
269: + e.toString());
270: }
271: fileInputStream.close();
272: //
273: // test 4
274: //
275: String gzipFile = embTestConfig.getGzipCorruptFileName();
276: String gzipFileFullPath = embTestConfig.getMediaDir()
277: + File.separatorChar + gzipFile;
278: GZIPInputStream inputStream = new GZIPInputStream(
279: new FileInputStream(gzipFileFullPath));
280: try {
281: testInstance = new MediaBean(inputStream, "video/mpeg",
282: "testMpeg.mpeg");
283: fail("test IV: Should throw a ContentAccessException");
284: } catch (ContentAccessException e) {
285: testTrace("test IV passed");
286: } catch (Exception e) {
287: fail("test IV: Should throw a ContentAccessException but threw "
288: + e.toString());
289: }
290: inputStream.close();
291:
292: succeed();
293: }
294: }
|