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

import org.objectweb.jonas.emb.mfb.formats.image.BmpFormat;
import org.objectweb.jonas.emb.mfb.formats.image.BmpHeader;

import com.ibm.emb.junit.EMBTestRunner;
import com.ibm.emb.test.MediaHeaderTest;

public class BmpHeaderTest extends MediaHeaderTest {

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

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

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

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

    /**
     * creates an instance of BmpHeader with the given Parameters used in super
     * class of BmpHeaderTest
     */
    public MediaHeader createMediaHeader() throws MediaException, IOException {

        // 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
        InputStream mediaBmpInputStream = new FileInputStream(mediaBmpFile);
        BmpFormat bmpFormat = new BmpFormat();
        BmpHeader bmpHeader = (BmpHeader) bmpFormat.extractHeader(mediaBmpInputStream);
        mediaBmpInputStream.close();

        return bmpHeader;

    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getField(String)
     *    Testcase Number: TEMB0062SBA
     *
     *    Test 1:
     *    iterate over fieldnames, check for these results:
     *    version: &quot;BM&quot;
     *    fileSize: 699994
     *    dataOffset: 54
     *
     *
     *
     * </pre>
     */
    public void testTEMB0062SBA() throws MediaException, IOException {

        BmpHeader testInstance;
        testInstance = (BmpHeader) createMediaHeader();

        //
        // test 1
        //

        // error report
        logProductError("reported error TEMB0053RMD, NullPointerException");

        assertTrue("test 1", testInstance.getField("version").toString().equals("BM")
                && testInstance.getField("filesize").toString().equals("699994")
                && testInstance.getField("dataoffset").toString().equals("54"));
        testTrace("test 1 passed");

        //
        // test 2
        //

        //TODO  : EMB 006 : getInfoHeader does not exist any more
//        BmpHeader mediaHeader = (BmpHeader) testInstance.getInfoHeader();
//
//        assertTrue("test 2", mediaHeader.getField("infoHeaderSize").toString().equals("40")
//                && mediaHeader.getField("width").toString().equals("526")
//                && mediaHeader.getField("hight").toString().equals("443")
//                && mediaHeader.getField("bumberOfPlanes").toString().equals("1")
//                && mediaHeader.getField("colorDepth").toString().toString().equals("24")
//                && mediaHeader.getField("compressionIdentifier").toString().equals("0")
//                && mediaHeader.getField("datasize").toString().equals("699940")
//                && mediaHeader.getField("horizontalResolution").toString().equals("3780")
//                && mediaHeader.getField("verticalResolution").toString().equals("3780")
//                && mediaHeader.getField("numberOfColorsIndexes").toString().equals("0")
//                && mediaHeader.getField("numberOfDiplayColorIndexes").toString().equals("0"));
//        testTrace("test 2 passed");

        succeed();

    }

}