package com.ibm.emb.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

import javax.emb.ContentAccessException;
import javax.emb.GenericMediaFormat;
import javax.emb.Media;
import javax.emb.MediaBean;
import javax.emb.MediaException;
import javax.emb.MediaFormatRegistry;

import org.objectweb.jonas.emb.mfb.formats.video.MpegVideoFormat;

import com.ibm.emb.junit.EMBTestRunner;

/**
 * <pre>
 *
 *
 *
 *     Line Item: MFB API
 *     Subcategory 1: javax.emb
 *     Subcategory 2: MediaBean
 *
 *     This is the test class for javax.emb.MediaBean.  Many of the tests
 *     are implemented in the super class MediaTest and only a few initialization
 *     methods are provided here.
 *
 *
 *
 * </pre>
 */
public class MediaBeanTest extends MediaTest {

    public MediaBeanTest(String name) throws MediaException {
        super(name);
    }

    public static void main(String args[]) {
        EMBTestRunner.run(MediaBeanTest.class);
    }

    /**
     * creates an instance of MediaBean with the given parameters used in super
     * class of MediaTest
     */
    public Media createMedia(File inputFile, String mimeType) throws MediaException {
        return new MediaBean(inputFile, mimeType);
    }

    /**
     * creates an instance of MediaBean with the given parameters used in super
     * class of MediaTest
     */
    public Media createMedia(InputStream in, String mimeType, String name) throws MediaException {
        return new MediaBean(in, mimeType, name);
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: MediaBean(File mediaFile, String mimeType)
     *    Testcase Number: EMB011
     *
     *    setup: media has to be provided in local file system
     *
     *    test procedure:
     *    I.
     *    a) java.io.file pointing to media file is provided in the constructor call.
     *    	-&gt;1.instance is created
     *     -&gt;2.mimetype is queried and verified
     *    	-&gt;3.media file content size is checked by comparing byte array size and file size.
     *     -&gt;4.Media.getName is not null and name contains exactly 1 dot.
     *    b) provide null as mime-type  -&gt;  same as getFormat().getDefaultMimeType()
     *    c) provide &quot;test&quot; as mime-type -&gt; &quot;test&quot;
     *
     *    II. call MediaBean(null, null) -&gt; NullPointerException
     *
     *    III. call MediaBean with nonexistent file and null mimetype -&gt; ContentAccessException
     *
     *
     *
     * </pre>
     */
    public void testEMB011() throws IOException, MediaException {
        File mpegTestFile = new File(embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getMpegMovieName1());

        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("mpeg", new MpegVideoFormat());
        formatRegistry.rebind("test", new GenericMediaFormat());

        MediaBean testInstance = new MediaBean(mpegTestFile, "video/mpeg");
        //
        // test I a1
        //
        assertNotNull("test I a1: ", testInstance);
        testTrace("test I a1 passed");
        //
        // test I a2
        //
        assertEquals("test I a2: ", "video/mpeg", testInstance.getMimeType());
        testTrace("test I a2 passed");
        //
        // test I a3
        //
        assertEquals("test I a3: ", mpegTestFile.length(), testInstance.getSize());
        testTrace("test I a3 passed");
        //
        // test I a4
        //
        int index = testInstance.getName().indexOf(".");
        int postIndex = testInstance.getName().indexOf(".", index + 1);
        assertTrue("test I a4 :", (testInstance.getName() != null) && ((index != -1) && (postIndex == -1)));
        testTrace("test I a4 passed");
        //
        // test I b
        //
        //
        testInstance = new MediaBean(mpegTestFile, null);
        assertEquals("test I b: ", testInstance.getFormat().getDefaultMimeType(), testInstance.getMimeType());
        testTrace("test I b passed");
        //
        // test I c
        //
        testInstance = new MediaBean(mpegTestFile, "test");
        assertEquals("test I c: ", "test", testInstance.getMimeType());
        testTrace("test I c passed");
        //
        // test II
        //
        try {
            testInstance = new MediaBean(null, "test");
            fail("test II : Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test II passed");
        } catch (Exception e) {
            fail("test II: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test III
        //
        try {
            testInstance = new MediaBean(new File("nonexistingFile.mpeg"), "mpeg");
            fail("test III: Should throw an ContentAccessException");
        } catch (javax.emb.ContentAccessException e) {
            testTrace("test III passed");
        } catch (Exception e) {
            fail("test III: Should throw a ContentAccessException but threw " + e.toString());
        }

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: MediaBean(InputStream contentStream, String mimeType, String name)
     *    Testcase Number: EMB012
     *
     *    setup: input stream has to be provided by using a file input stream
     *
     *    test procedure:
     *    I.
     *    a)mimetype is null
     *    	  -&gt;1.mimetype = getFormat().getDefaultMimeType()
     *    	  -&gt;2.stream content size is checked by comparing byte array size of Media.getContent() and stream size
     *    b)mimetype is &quot;test&quot; (parameter &quot;name&quot; has to be != null) -&gt; mimetype = &quot;test&quot;
     *
     *    II. contentStream is null -&gt; NullPointerException
     *
     *    III. name is null -&gt; NullPointerException
     *
     *    IV. create MediaBean using input stream from corrupt GZIP file (can open but not read)
     *     	-&gt; ContentAccessException
     *
     *
     *
     * </pre>
     */
    public void testEMB012() throws IOException, MediaException {

        MediaBean testInstance = null;
        // file input stream
        FileInputStream fileInputStream = new FileInputStream(embTestConfig.getMediaDir() + File.separatorChar
                + embTestConfig.getMpegMovieName1());

        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("test", new javax.emb.GenericMediaFormat());
        formatRegistry.rebind("mpeg", new MpegVideoFormat());

        //
        // test 1 a
        //
        int streamSize = 0;
        while (fileInputStream.read() != -1) {
            streamSize++;
        }
        fileInputStream.close();
        fileInputStream = new FileInputStream(embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getMpegMovieName1());
        testInstance = new MediaBean(fileInputStream, null, "test.mpeg");
        assertEquals("test I a1: ", testInstance.getFormat().getDefaultMimeType(), testInstance.getMimeType());
        assertEquals("test I a2: ", streamSize, testInstance.getSize());
        testTrace("test I a passed");
        //
        // test 1 b
        //
        fileInputStream = new FileInputStream(embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getMpegMovieName1());
        testInstance = new MediaBean(fileInputStream, "test", "test.mpeg");
        assertEquals("test I b: ", "test", testInstance.getMimeType());
        testTrace("test I b passed");
        //
        // test 2
        //
        try {
            testInstance = new MediaBean(null, "test", "test.mpeg");
            fail(" test II: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test II passed");
        } catch (Exception e) {
            fail("test II: should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 3
        //
        fileInputStream = new FileInputStream(embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getMpegMovieName1());
        try {
            testInstance = new MediaBean(fileInputStream, "test", null);
            fail(" test III: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test III passed");
        } catch (Exception e) {
            fail("test III: Should throw a NullPointerException but threw " + e.toString());
        }
        fileInputStream.close();
        //
        // test 4
        //
        String gzipFile = embTestConfig.getGzipCorruptFileName();
        String gzipFileFullPath = embTestConfig.getMediaDir() + File.separatorChar + gzipFile;
        GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(gzipFileFullPath));
        try {
            testInstance = new MediaBean(inputStream, "video/mpeg", "testMpeg.mpeg");
            fail("test IV: Should throw a ContentAccessException");
        } catch (ContentAccessException e) {
            testTrace("test IV passed");
        } catch (Exception e) {
            fail("test IV: Should throw a ContentAccessException but threw " + e.toString());
        }
        inputStream.close();

        succeed();
    }
}