package com.ibm.emb.test.mfb;

// java stuff
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.emb.Media;
import javax.emb.MediaBean;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaFormatRegistry;

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

import com.ibm.emb.junit.EMBTestRunner;

public class MpegVideoFormatTest extends EmbeddedMediaFormatTest {

    /**
     * constructor
     */
    public MpegVideoFormatTest(String name) throws MediaException {
        super(name);
    }

    /**
     * main method
     */
    public static void main(String args[]) {
        EMBTestRunner.run(MpegVideoFormatTest.class);
    }

    /**
     * creates an instance of MpegVideoFormat with the given Parameters used in
     * super class of MpegVideoFormatTest
     */
    public MediaFormat createMediaFormat() {
        return new MpegVideoFormat();
    }

    /**
     * do initialisation here
     */
    protected void setUp() {
    }

    /**
     * freeup resources
     */
    protected void tearDown() {
    }

    /**
     * <pre>
     *
     *      Testcase Name: extractHeader(Media)
     *      Testcase Number: TEMB0086SBA
     *
     *      Test 1:
     *      call extractHeader passing suitable MPEG video
     *      --&gt; check result to be an instance of com.ibm.emb.meb.MpegVideoHeader
     *
     * </pre>
     */
    public void testTEMB0086SBA() throws MediaException, IOException {

        //
        // test 1
        //

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

        // full path of the mpeg video sample
        String mediaMpegFullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getMpegMovieName1();

        // creates media bean
        File mediaMpegFile = new File(mediaMpegFullPath);
        MediaBean mediaBean = new MediaBean(mediaMpegFile, "mpeg");

        // creates mpeg video format
        MpegVideoFormat mpegVideoFormat = new MpegVideoFormat();

        // creates mpegVideoHeader

//      TODO : MpegHeader does not exist any more
//        InputStream mediaMpegInputStream = new FileInputStream(mediaMpegFile);
//        MpegVideoHeader mpegVideoHeader = (MpegVideoHeader) mpegVideoFormat.extractHeader(mediaMpegInputStream);
//        mediaMpegInputStream.close();
//
//        // test
//        assertTrue("test 1", mpegVideoHeader instanceof MpegVideoHeader);

        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *      Testcase Name: extractSurrogate(Media)
     *      Testcase Number: TEMB0087SBA
     *
     *      Test 1:
     *      call extractSurrogate passing suitable MPEG video.
     *      check result to be an instance of Media
     *      access the content of the resulting media ----&gt;  to verify it is accessible
     *
     * </pre>
     */
    public void testTEMB0087SBA() throws MediaException, IOException {
        //
        // test 1
        //

        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("mpg", new MpegVideoFormat());

        // full path of the mpeg video sample
        String mediaMpegFullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getMpegMovieName1();

        // creates media bean
        File mediaMpegFile = new File(mediaMpegFullPath);
        MediaBean mediaBean = new MediaBean(mediaMpegFile, "mpeg");

        InputStream mediaMpegInputStream = new FileInputStream(mediaMpegFile);
        // creates mpeg video format
        MpegVideoFormat mpegVideoFormat = new MpegVideoFormat();
        // gets surrogate
        MediaBean mediaBeanSurrogate = (MediaBean) mpegVideoFormat.extractProxy(mediaMpegInputStream);
        mediaMpegInputStream.close();

        // check access to content and check format
        byte[] content = mediaBeanSurrogate.getContent();
        byte[] content_ = new byte[content.length];
        System.arraycopy(content, 0, content_, 0, content.length);

        assertTrue("test 1", mediaBeanSurrogate instanceof Media);
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *      Testcase Name: getDefaultMimeType()
     *      Testcase Number: TEMB0088SBA
     *
     *      Test 1:
     *      call getDefaultMimeType
     *      ---&gt;check if result equals &quot;video/mpeg&quot;
     *
     * </pre>
     */
    public void testTEMB0088SBA() throws MediaException {
        //
        // test 1
        //

        // creates mpeg video format
        MpegVideoFormat mpegVideoFormat = new MpegVideoFormat();

        assertEquals("test 1: ", "video/mpeg", mpegVideoFormat.getDefaultMimeType());
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *      Testcase Name: isStreamingDesirable()
     *      Testcase Number: TEMB0089SBA
     *
     *      Test 1:
     *      call isStreamingDesirable
     *      ---&gt;check if result is true
     *
     * </pre>
     */
    public void testTEMB0089SBA() throws MediaException {
        //
        // test 1
        //

        // creates mpeg video format
        MpegVideoFormat mpegVideoFormat = new MpegVideoFormat();

        assertTrue("test 1: ", mpegVideoFormat.isStreamingDesirable());
        testTrace("test 1 passed");

        succeed();
    }

}