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 javax.emb.MediaHeader;

import org.objectweb.jonas.emb.mfb.formats.audio.MpegAudioFormat;

import com.ibm.emb.junit.EMBTestRunner;

public class MpegAudioFormatTest extends EmbeddedMediaFormatTest {

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

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

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

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

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

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: extractHeader(Media)
     *        Testcase Number: TEMB0079SBA
     *
     *        Test 1:
     *        call extractHeader passing a mp3 audio file
     *        --&gt; check result to be an instance of com.ibm.emb.meb.MpegAudioHeader
     *
     *
     *
     * </pre>
     */
    public void testTEMB0079SBA() throws MediaException, IOException {

        //
        // test 1
        //

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

        // full path of the mp3 audio sample
        String mediaMp3FullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getMp3SoundName1();

        // creates media bean'
        File mediaMp3File = new File(mediaMp3FullPath);
        MediaBean mediaBean = new MediaBean(mediaMp3File, "mp3");

        // creates mpeg audio format
        MpegAudioFormat mpegAudioFormat = new MpegAudioFormat();

        // creates mpegAudioHeader
        InputStream mediaMp3InputStream = new FileInputStream(mediaMp3File);
        MediaHeader mpegAudioHeader = mpegAudioFormat.extractHeader(mediaMp3InputStream);
        mediaMp3InputStream.close();

        // test
        //assertTrue("test 1", mpegAudioHeader instanceof MpegAudioHeader);

        testTrace("test 1 passed");

        succeed();
    }

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

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

        // full path of the bmp sample
        String mediaMp3FullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getMp3SoundName1();

        // creates media bean
        File mediaMp3File = new File(mediaMp3FullPath);
        MediaBean mediaBean = new MediaBean(mediaMp3File, "mp3");

        // creates jpg format
        InputStream mediaMp3InputStream = new FileInputStream(mediaMp3File);
        MpegAudioFormat mpegAudioFormat = new MpegAudioFormat();
        // gets surrogate
        MediaBean mediaBeanSurrogate = (MediaBean) mpegAudioFormat.extractProxy(mediaMp3InputStream);
        mediaMp3InputStream.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: TEMB0081SBA
     *
     *        Test 1:
     *        call getDefaultMimeType
     *        ---&gt;check if result equals &quot;audio/x-mpeg&quot;
     *
     *
     *
     * </pre>
     */
    public void testTEMB0081SBA() throws MediaException {
        //
        // test 1
        //

        // creates mpeg audio format
        MpegAudioFormat mpegAudioFormat = new MpegAudioFormat();

        assertEquals("test 1: ", "audio/x-mpeg", mpegAudioFormat.getDefaultMimeType());
        testTrace("test 1 passed");

        succeed();
    }

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

        // creates mpeg audio format
        MpegAudioFormat mpegAudioFormat = new MpegAudioFormat();

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

        succeed();
    }

}