package com.ibm.emb.test.mfb;

// java stuff
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.emb.MediaBean;
import javax.emb.MediaConverter;
import javax.emb.MediaConverterSpec;
import javax.emb.MediaException;
import javax.emb.MediaFormatRegistry;

import mfb.converters.BmpToJpegConverterSpec;
import mfb.converters.JpegToBmpConverterSpec;

import org.objectweb.jonas.emb.mfb.converter.image.ImageConverter;
import org.objectweb.jonas.emb.mfb.converter.image.ImageResizeConverterSpec;
import org.objectweb.jonas.emb.mfb.formats.image.JpegFormat;

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

public class ImageConverterTest extends MediaConverterTest {

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

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

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

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

    /**
     * creates an instance of ImageConverter with the given Parameters used in
     * super class of ImageConverterTest
     */
    public MediaConverter createMediaConverter() throws MediaException {

        // creates BmpToJpegConverterSpec
        MediaConverterSpec converterSpec = new BmpToJpegConverterSpec();
        // returns an ImageConverter
        return converterSpec.getConverter();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *        Testcase Name: convert(InputStream)
     *        Testcase Number: TEMB0100SBA  
     *        
     *        Test 1:
     *        
     *        create BmpToJpegConverterSpec
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on a bmp media
     *        create MediaBean with conversion result
     *        close FileInputStream and result stream
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        
     *        
     *        Test 2:
     *        iterate over possible BMP color depths: 32, 24, 16, 8, 4, 1	{
     *        create JpegToBmpConverterSpec, specify color depth
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on a jpg media file
     *        create MediaBean with conversion result
     *        close FileInputStream and result stream
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        
     *        Test 3:
     *        iterate over crop factors (0.4, 1.0, 2.5) and input files ( test.bmp , test.jpg ) {
     *        
     *        create ImageResizeConverterSpec, pass crop factor
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on input file
     *        create MediaBean with conversion result
     *        close FileInputStream and result stream
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        
     *        Test 4:
     *        iterate over crop factor pairs (0.4/2.5, 1.0/1.0, 2.5/0.4) and input files (test.bmp, test.jpg ) {
     *        
     *        create ImageResizeConverterSpec, pass crop factor pair (horizontal/vertical)
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on input file
     *        create MediaBean with conversion result
     *        close FileInputStream and result stream
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        }
     *        
     *        
     *        }
     *        
     *        
     *   
     *  
     * </pre>
     */
    public void testTEMB0100SBA() throws MediaException, IOException {
        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());

        // testinstance
        ImageConverter testInstance;

        // converter specs
        JpegToBmpConverterSpec converterSpec;
        ImageResizeConverterSpec resizeConverterSpec;

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

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

        // bmp input stream
        FileInputStream bmpInputStream;

        // jpeg input stream
        FileInputStream jpegInputStream;

        // test media
        MediaBean testMedia;

        //
        // test 1
        //

        /*
         * try { // stream bmpInputStream = new
         * FileInputStream(mediaBmpFullPath); // creates image converter
         * testInstance = (ImageConverter) createMediaConverter(); // creates
         * media bean with the result of conversion testMedia = new
         * MediaBean(testInstance.process(bmpInputStream), "jpg", "testJpg");
         * bmpInputStream.close(); JpegHeader testJpegHeader = (JpegHeader)
         * testMedia.getHeader(); testTrace("test 1 passed"); succeed(); } catch
         * (Exception e) { fail("test 1 : no exception expected, this is thrown : " +
         * e.toString()); }
         */
        //
        // test 2
        //
        byte[] colorDepth = {32, 24, 16, 8, 4, 1};

        try {
            for (int i = 0; i < colorDepth.length; i++) {

                // creates image converter
                converterSpec = new JpegToBmpConverterSpec(colorDepth[i]);
                testInstance = (ImageConverter) converterSpec.getConverter();

                // creates stream
                jpegInputStream = new FileInputStream(mediaJpegFullPath);

                // creates media bean with the result of conversion
                testMedia = new MediaBean(testInstance.process(jpegInputStream), "bmp", "testBmp");

                // close stream stream
                jpegInputStream.close();
            }
            testTrace("test 2 passed");
            succeed();
        } catch (IllegalArgumentException e) {
            fail("test 2 , thrown exception : " + e.toString());
        }

        //
        // test 3
        //

        double[] cropFactor = {0.4, 1.0, 2.5};

        try {
            for (int i = 0; i < cropFactor.length; i++) {

                // creates image converter
                resizeConverterSpec = new ImageResizeConverterSpec(cropFactor[i]);
                testInstance = (ImageConverter) resizeConverterSpec.getConverter();

                // create input streams
                bmpInputStream = new FileInputStream(mediaBmpFullPath);
                jpegInputStream = new FileInputStream(mediaJpegFullPath);

                // creates media bean with the result of conversion - BMP
                testMedia = new MediaBean(testInstance.process(jpegInputStream), "bmp", "testBmp");

                // error report
                logProductError("consequence of a reported error PEMB0052RMD");

                // creates media bean with the result of conversion - JPEG
                testMedia = new MediaBean(testInstance.process(bmpInputStream), "jpg", "testJpeg");

                // close stream
                jpegInputStream.close();
                bmpInputStream.close();
            }
            testTrace("test 3 passed");
            succeed();
        } catch (IllegalArgumentException e) {
            fail("test 3 , thrown exception : " + e.toString());
        }

        //
        // test 4
        //

        double[][] cropFactor2 = { {0.4, 2.5}, {1.0, 1.0}, {2.5, 0.4}};

        try {
            for (int i = 0; i < cropFactor.length; i++) {

                // creates image converter
                resizeConverterSpec = new ImageResizeConverterSpec(cropFactor2[i][0], cropFactor2[i][1]);
                testInstance = (ImageConverter) resizeConverterSpec.getConverter();

                // create input streams
                bmpInputStream = new FileInputStream(mediaBmpFullPath);
                jpegInputStream = new FileInputStream(mediaJpegFullPath);

                // creates media bean with the result of conversion - BMP
                testMedia = new MediaBean(testInstance.process(jpegInputStream), "bmp", "testBmp");

                // creates media bean with the result of conversion - JPEG
                testMedia = new MediaBean(testInstance.process(bmpInputStream), "jpg", "testJpeg");

                // close stream
                jpegInputStream.close();
                bmpInputStream.close();
            }
            testTrace("test 3 passed");
            succeed();
        } catch (IllegalArgumentException e) {
            fail("test 3 , thrown exception : " + e.toString());
        }

    }

    /**
     * <pre>
     * 
     *  
     *   
     *        Testcase Name: convert(InputStream, OutputStream)
     *        Testcase Number: TEMB0101SBA  
     *        
     *        Test 1:
     *        
     *        create BmpToJpegConverterSpec
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on test.bmp, FileOutputStream on temporary file
     *        close FileInputStream and FileOutputStream
     *        create MediaBean with temporary file
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        
     *        
     *        Test 2:
     *        iterate over possible BMP color depths: 32, 24, 16, 8, 4, 1	{
     *        create JpegToBmpConverterSpec, specify color depth
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on test.jpg, OutputStream on temporary file
     *        close FileInputStream and FileOutputStream
     *        create MediaBean with temporary file
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        }
     *        
     *        
     *        Test 3:
     *        iterate over crop factors (0.4, 1.0, 2.5) and input files (test.bmp, test.jpg) {
     *        
     *        create ImageResizeConverterSpec, pass crop factor
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on input file, OutputStream on temporary file
     *        close FileInputStream and FileOutputStream
     *        create MediaBean with temporary file
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        }
     *        
     *        
     *        Test 4:
     *        iterate over crop factor pairs (0.4/2.5, 1.0/1.0, 2.5/0.4) and input files (test.bmp,test.jpg) {
     *        
     *        create ImageResizeConverterSpec, pass crop factor pair (horizontal/vertical)
     *        query converter instance from converter spec using getConverter()
     *        call convert(InputStream) of converter passing FileInputStream on input file, OutputStream on temporary file
     *        close FileInputStream and FileOutputStream
     *        create MediaBean with temporary file
     *        --&gt; check for no exceptions
     *        --&gt; call getHeader() on media bean and check for no exceptions to verify format
     *        }
     *        
     *        
     *   
     *  
     * </pre>
     */
    public void testTEMB0101SBA() throws MediaException, IOException {

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

        // testinstance
        ImageConverter testInstance;

        // converter specs
        JpegToBmpConverterSpec converterSpec;
        ImageResizeConverterSpec resizeConverterSpec;

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

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

        // temporary file
        File tempFile;
        File tempFile1;

        // bmp input stream
        FileInputStream bmpInputStream;

        // jpeg input stream
        FileInputStream jpegInputStream;

        // temp output stream
        FileOutputStream tempOutputStream;
        FileOutputStream tempOutputStream1;

        // test media
        MediaBean testMedia;

        //
        // test 1
        //

        // error report
        logProductError("consequence of a reported error PEMB0052RMD");

        try {
            // input stream
            bmpInputStream = new FileInputStream(mediaBmpFullPath);

            // creates temp file
            tempFile = File.createTempFile("test", "tmp");

            // creates output stream
            tempOutputStream = new FileOutputStream(tempFile);

            // creates image converter
            testInstance = (ImageConverter) createMediaConverter();

            // process conversion
            testInstance.process(bmpInputStream, tempOutputStream);

            // close streams
            tempOutputStream.close();
            bmpInputStream.close();

            // creates media bean with the result of conversion
            testMedia = new MediaBean(tempFile, "jpg");

            // JpegHeader testJpegHeader = (JpegHeader) testMedia.getHeader();

            // deletes temp file
            tempFile.delete();

            testTrace("test 1 passed");
            succeed();
        } catch (Exception e) {
            fail("test 1 : no exception expected, this is thrown : " + e.toString());
        }

        //
        // test 2
        //

        byte[] colorDepth = {32, 24, 16, 8, 4, 1};

        try {
            for (int i = 0; i < colorDepth.length; i++) {

                // creates image converter
                converterSpec = new JpegToBmpConverterSpec(colorDepth[i]);
                testInstance = (ImageConverter) converterSpec.getConverter();

                // creates temp file
                tempFile = File.createTempFile("test", "tmp");

                // creates input stream
                jpegInputStream = new FileInputStream(mediaJpegFullPath);

                // creates output stream
                tempOutputStream = new FileOutputStream(tempFile);

                // process conversion
                testInstance.process(jpegInputStream, tempOutputStream);

                // close streams
                tempOutputStream.close();
                jpegInputStream.close();

                // creates media bean with the result of conversion
                testMedia = new MediaBean(tempFile, "bmp");

                // deletes temp file
                tempFile.delete();
            }

            testTrace("test 2 passed");
            succeed();
        } catch (IllegalArgumentException e) {
            fail("test 2 , thrown exception : " + e.toString());
        }

        //
        // test 3
        //

        double[] cropFactor = {0.4, 1.0, 2.5};

        try {
            for (int i = 0; i < cropFactor.length; i++) {

                // creates image converter
                resizeConverterSpec = new ImageResizeConverterSpec(cropFactor[i]);
                testInstance = (ImageConverter) resizeConverterSpec.getConverter();

                // creates temp files
                tempFile = File.createTempFile("test", "tmp");
                tempFile1 = File.createTempFile("test", "tmp");

                // create input streams
                bmpInputStream = new FileInputStream(mediaBmpFullPath);
                jpegInputStream = new FileInputStream(mediaJpegFullPath);

                // creates output streams
                tempOutputStream = new FileOutputStream(tempFile);
                tempOutputStream1 = new FileOutputStream(tempFile1);

                // makes conversion and creates media bean with the result of
                // conversion - bmp temp file
                testInstance.process(jpegInputStream, tempOutputStream);

                tempOutputStream.close();
                jpegInputStream.close();

                testMedia = new MediaBean(tempFile, "bmp");
                tempFile.delete();

                // makes conversion and creates media bean with the result of
                // conversion - jpeg temp file
                testInstance.process(bmpInputStream, tempOutputStream1);

                tempOutputStream1.close();
                bmpInputStream.close();

                testMedia = new MediaBean(tempFile1, "jpg");
                tempFile1.delete();
            }

            testTrace("test 3 passed");
            succeed();
        } catch (IllegalArgumentException e) {
            fail("test 3 , thrown exception : " + e.toString());
        }

        //
        // test 4
        //

        double[][] cropFactor2 = { {0.4, 2.5}, {1.0, 1.0}, {2.5, 0.4}};

        try {
            for (int i = 0; i < cropFactor.length; i++) {

                // creates image converter
                resizeConverterSpec = new ImageResizeConverterSpec(cropFactor2[i][0], cropFactor2[i][1]);
                testInstance = (ImageConverter) resizeConverterSpec.getConverter();

                // creates temp files
                tempFile = File.createTempFile("test", "tmp");
                tempFile1 = File.createTempFile("test", "tmp");

                // create input streams
                bmpInputStream = new FileInputStream(mediaBmpFullPath);
                jpegInputStream = new FileInputStream(mediaJpegFullPath);

                // creates output streams
                tempOutputStream = new FileOutputStream(tempFile);
                tempOutputStream1 = new FileOutputStream(tempFile1);

                // makes conversion and creates media bean with the result of
                // conversion - bmp temp file
                testInstance.process(jpegInputStream, tempOutputStream);

                tempOutputStream.close();
                jpegInputStream.close();

                testMedia = new MediaBean(tempFile, "bmp");
                tempFile.delete();

                // makes conversion and creates media bean with the result of
                // conversion - jpeg temp file
                testInstance.process(bmpInputStream, tempOutputStream1);

                tempOutputStream1.close();
                bmpInputStream.close();

                testMedia = new MediaBean(tempFile1, "jpg");
                tempFile1.delete();
            }

            testTrace("test 4 passed");
            succeed();
        } catch (IllegalArgumentException e) {
            fail("test 4 , thrown exception : " + e.toString());
        }

    }

}