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.BmpFormat;
import org.objectweb.jonas.emb.mfb.formats.image.BmpHeader;

import com.ibm.emb.junit.EMBTestRunner;

public class BmpFormatTest extends EmbeddedMediaFormatTest {

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

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

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

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

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

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

        //
        // test 1
        //

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

        // name of a bmp sample
        String mediaBmp = embTestConfig.getBmpPictureName1();

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

        // creates media bean
        File mediaBmpFile = new File(mediaBmpFullPath);
        MediaBean mediaBean = new MediaBean(mediaBmpFile, "bmp");

        // creates bmp format
        BmpFormat bmpFormat = new BmpFormat();

        // creates bmpHeader
        InputStream mediaBmpInputStream = new FileInputStream(mediaBmpFile);
        BmpHeader bmpHeader = (BmpHeader) bmpFormat.extractHeader(mediaBmpInputStream);
        mediaBmpInputStream.close();

        // test
        assertTrue("test 1", bmpHeader instanceof BmpHeader);

        testTrace("test 1 passed");

        succeed();

    }

    /**
     * <pre>
     * 
     *      Testcase Name: extractSurrogate(Media)
     *      Testcase Number: TEMB0085SBA  
     *      
     *      Test 1:
     *      call extractSurrogate passing a bmp media
     *      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.BmpFormat
     *      
     *      
     * </pre>
     */
    public void testTEMB0085SBA() throws MediaException, IOException {
        //
        // test 1
        //

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

        // name of a bmp sample
        String mediaBmp = embTestConfig.getBmpPictureName1();

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

        // creates media bean
        File mediaBmpFile = new File(mediaBmpFullPath);
        MediaBean mediaBean = new MediaBean(mediaBmpFile, "bmp");

        // creates bmp format
        BmpFormat bmpFormat = new BmpFormat();

        // error report
        logProductError("reported error PEMB0052RMD, invalid bmp header data");

        // gets surrogate
        InputStream mediaBmpInputStream = new FileInputStream(mediaBmpFile);
        MediaBean mediaBeanSurrogate = (MediaBean) bmpFormat.extractProxy(mediaBmpInputStream);
        mediaBmpInputStream.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);

        BmpFormat bmpFormat1 = (BmpFormat) mediaBeanSurrogate.getFormat();

        assertTrue("test 1", bmpFormat1 instanceof BmpFormat && mediaBeanSurrogate.getMimeType().equals("image/gif"));
        testTrace("test 1 passed");

        succeed();

    }

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

        // creates bmp format
        BmpFormat bmpFormat = new BmpFormat();
        assertEquals("test 1", "image/bmp", bmpFormat.getDefaultMimeType());

        testTrace("test 1 passed");

        succeed();

    }

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

        // creates bmp format
        BmpFormat bmpFormat = new BmpFormat();
        assertTrue("test 1", !bmpFormat.isStreamingDesirable());

        testTrace("test 1 passed");

        succeed();

    }
}