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.MediaBean;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaFormatRegistry;

import org.objectweb.jonas.emb.mfb.formats.image.JpegFormat;
import org.objectweb.jonas.emb.mfb.formats.image.JpegHeader;

import com.ibm.emb.junit.EMBTestRunner;

public class JpegFormatTest extends EmbeddedMediaFormatTest {

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

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

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

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

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

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

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

        // full path of the jpeg sample
        String mediaJpegFullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getJpgPictureName1();

        // creates media bean
        File mediaJpegFile = new File(mediaJpegFullPath);
        MediaBean mediaBean = new MediaBean(mediaJpegFile, "jpg");

        // creates jpeg format
        JpegFormat jpegFormat = new JpegFormat();

        // creates jpegHeader
        InputStream mediaJpegInputStream = new FileInputStream(mediaJpegFile);
        JpegHeader jpegHeader = (JpegHeader) jpegFormat.extractHeader(mediaJpegInputStream);
        mediaJpegInputStream.close();

        // test
        assertTrue("test 1", jpegHeader instanceof JpegHeader);

        testTrace("test 1 passed");

        succeed();

    }

    /**
     * <pre>
     * 
     *      Testcase Name: extractSurrogate(Media)
     *      Testcase Number: TEMB0066SBA  
     *      
     *      Test 1:
     *      call extractSurrogate passing 
     *      check result to be an instance of Media
     *      access the content of the resulting media ----&gt;  to verify it is accessible
     *      check if format of result is an instance of com.ibm.emb.meb.JpegFormat
     *      
     * </pre>
     */
    public void testTEMB0066SBA() throws MediaException, IOException {
        //
        // test 1
        //

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

        // full path of the jpg sample
        String mediaJpegFullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getJpgPictureName1();

        // creates media bean
        File mediaJpegFile = new File(mediaJpegFullPath);
        MediaBean mediaBean = new MediaBean(mediaJpegFile, "jpg");

        // creates jpg format
        InputStream mediaJpegInputStream = new FileInputStream(mediaJpegFile);
        JpegFormat jpegFormat = new JpegFormat();
        // gets surrogate
        MediaBean mediaBeanSurrogate = (MediaBean) jpegFormat.extractProxy(mediaJpegInputStream);
        mediaJpegInputStream.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);

        JpegFormat jpegFormat1 = (JpegFormat) mediaBeanSurrogate.getFormat();

        assertTrue("test 1", jpegFormat1 instanceof JpegFormat && mediaBeanSurrogate.getMimeType().equals("jpg"));
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *      Testcase Name: getDefaultMimeType()
     *      Testcase Number: TEMB0067SBA  
     *      
     *      Test 1:
     *      call getDefaultMimeType
     *      ---&gt;check if result equals &quot;image/jpeg&quot;
     *      
     *      
     * </pre>
     */
    public void testTEMB0067SBA() throws MediaException {
        //
        // test 1
        //

        // creates jpg format
        JpegFormat jpegFormat = new JpegFormat();

        assertEquals("test1: ", "image/jpeg", jpegFormat.getDefaultMimeType());
        testTrace("test 1 passed");

        succeed();
    }

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

        // creates jpg format
        JpegFormat jpegFormat = new JpegFormat();

        assertTrue("test1: ", !jpegFormat.isStreamingDesirable());
        testTrace("test 1 passed");

        succeed();
    }

}