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

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

public class JpegHeaderTest extends MediaHeaderTest {

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

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

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

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

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

        // 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 jpeg format
        InputStream mediaJpegInputStream = new FileInputStream(mediaJpegFile);
        JpegFormat jpegFormat = new JpegFormat();
        JpegHeader jpegHeader = (JpegHeader) jpegFormat.extractHeader(mediaJpegInputStream);
        mediaJpegInputStream.close();
        return jpegHeader;
    }

    /**
     * <pre>
     * 
     *      Testcase Name: getField(String)
     *      Testcase Number: TEMB0071SBA
     * 
     *      Test 1:
     *      iterate over fieldnames, check for these results:
     *      startOfImageSegment: 216
     *      applicationMarkerSegmentType: 224
     *      applicationMarkerSegmentSize: 16
     *      applicationIdentifier: &quot;JFIF&quot;
     * 
     *      Test 2:
     *      extract segment header using getJfifSegmentHeader();
     *      iterate over fieldnames, check for these results:
     *      identifier: &quot;JFIF&quot;
     *      formatRevision: &quot;1.01&quot;
     *      resolutionUnit: 1
     *      horizontalResolution: 96
     *      verticalResolution: 96
     *      horizontalThumbnailPixelCount: 0
     *      verticalThumbnailPixelCount: 0
     *  
     * </pre>
     */
    public void testTEMB0071SBA() throws MediaException, IOException {

        JpegHeader testInstance;
        testInstance = (JpegHeader) createMediaHeader();

        //
        // test 1
        //

        assertTrue("test 1", testInstance.getField("startOfImageSegment").toString().equals("216")
                && testInstance.getField("applicationMarkerSegmentType").toString().equals("224")
                && testInstance.getField("applicationMarkerSegmentSize").toString().equals("16")
                && testInstance.getField("applicationIdentifier").toString().equals("JFIF"));
        testTrace("test 1 passed");

        //
        // test 2
        //

        /*
         * MediaHeader mediaHeader = (JpegJfifSegmentHeader)
         * testInstance.getApplicationMarkerSegment(); assertTrue( "test 2",
         * mediaHeader.getField("identifier").toString().equals("JFIF") &&
         * mediaHeader.getField("formatRevision").toString().equals("1.01") &&
         * mediaHeader.getField("resolutionUnit").toString().equals("1") &&
         * mediaHeader.getField("horizontalResolution").toString().equals("96") &&
         * mediaHeader.getField("verticalResolution").toString().equals("96") &&
         * mediaHeader.getField("horizontalThumbnailPixelCount").toString().equals("0") &&
         * mediaHeader.getField("verticalThumbnailPixelCount").toString().equals("0"));
         * testTrace("test 2 passed");
         */

        succeed();
    }
}