package com.ibm.emb.test;

import java.util.Iterator;

import javax.emb.FormatAlreadyBoundException;
import javax.emb.FormatNotFoundException;
import javax.emb.GenericMediaFormat;
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.JpegFormat;

import com.ibm.emb.junit.EMBStaticHelper;
import com.ibm.emb.junit.EMBTestCaseBase;
import com.ibm.emb.junit.EMBTestRunner;

/**
 * <pre>
 * 
 *  
 *  
 *    Line Item: MFB API
 *    Subcategory 1: javax.emb
 *    Subcategory 2: MediaFormatRegistry
 *  
 *   
 *  
 * </pre>
 */
public class MediaFormatRegistryTest extends EMBTestCaseBase {

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

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

    /**
     * <pre>
     * 
     *  
     *  
     *    Testcase Name: MediaFormatRegistry.SINGLETON
     *    Testcase Number: EMB027
     *  
     *    setup:
     *  
     *    test procedure:
     *    1.access MediaFormatRegistry.SINGLETON
     *      expected result: not null
     *  
     *   
     *  
     * </pre>
     */
    public void testEMB027() throws MediaException {
        //
        // test 1
        //
        MediaFormatRegistry testInstance = MediaFormatRegistry.SINGLETON;
        assertNotNull("test 1: ", testInstance);
        testTrace("test 1 passed");
        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *  
     *    Testcase Name: bind(String fileExtension, MediaFormat mediaFormat)
     *    Testcase Number: EMB028
     *  
     *    setup:
     *  
     *    test procedure:
     *    1.access MediaFormatRegistry.SINGLETON
     *      call bind passing null as fileExtension and GenericMediaFormat instance as format
     *      expected resul: NullPointerException
     *  
     *    2.access MediaFormatRegistry.SINGLETON
     *      call bind passing random string as fileExtension and null as format
     *      expected result: NullPointerException
     *  
     *    3.access MediaFormatRegistry.SINGLETON
     *      call bind passing &quot;TEST1&quot; as fileExtension and GenericMediaFormat instance as format
     *      call bind passing &quot;TEST1&quot; as fileExtension and other GenericMediaFormat instance as format
     *      expected result: FormatAlreadyBoundException on second call
     *  
     *    4.bind passing valid file extension and GenericMediaFormat
     *      call lookup on file extenstion
     *      expected result: instance of GenericMediaFormat
     *  
     *    5.bind empty string with GenericMediaFormat
     *      expected result: no exception and lookup(&quot;&quot;) returns instance of GenericMediaFormat
     *  
     *   
     *  
     * </pre>
     */
    public void testEMB028() throws MediaException {
        //
        // test 1
        //
        MediaFormatRegistry testInstance = MediaFormatRegistry.SINGLETON;
        try {
            testInstance.bind(null, new GenericMediaFormat());
            fail("test 1: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 2
        //
        try {
            testInstance.bind(EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(1, 3)), null);
            fail("test 2: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 3
        //
        try {
            testInstance.bind("TEST1", new GenericMediaFormat());
            testInstance.bind("TEST1", new GenericMediaFormat());
            fail("test 3: Should throw FormatAlreadyBoundException");
        } catch (FormatAlreadyBoundException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3: Should throw a FormatAlreadyBoundException but threw" + e.toString());
        }
        //
        // test 4
        //
        try {
            testInstance.unbind("TEST2");
        } catch (FormatNotFoundException e) {
        }
        testInstance.bind("TEST2", new GenericMediaFormat());
        assertTrue("test 4: format does not match", testInstance.lookup("TEST2") instanceof GenericMediaFormat);
        testTrace("test 4 passed");
        //
        // test 5
        //
        try {
            testInstance.unbind("");
        } catch (FormatNotFoundException e) {
        }
        testInstance.bind("", new GenericMediaFormat());
        assertTrue("test 5: format does not match", testInstance.lookup("") instanceof GenericMediaFormat);
        testTrace("test 5 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *  
     *    Testcase Name: getFileExtensions()
     *    Testcase Number: EMB029
     *  
     *    setup:
     *  
     *    test procedure:
     *    1.access MediaFormatRegistry.SINGLETON
     *      bind fileExtensions &quot;test1&quot;, &quot;test2&quot;, and &quot;test3&quot; to GenericMediaFormat
     *      call getFileExtensions
     *    	 expected result: list contains test1, test2, test3
     *  
     *   
     *  
     * </pre>
     */
    public void testEMB029() throws MediaException {
        //
        // test 1
        //
        MediaFormatRegistry testInstance = MediaFormatRegistry.SINGLETON;
        testInstance.rebind("test1", new GenericMediaFormat());
        testInstance.rebind("test2", new GenericMediaFormat());
        testInstance.rebind("test3", new GenericMediaFormat());

        Iterator fileExtensions = testInstance.getFileExtensions();
        boolean test1 = false;
        boolean test2 = false;
        boolean test3 = false;
        while (fileExtensions.hasNext()) {
            Object ext = fileExtensions.next();
            if (ext.toString().equals("test1")) {
                test1 = true;
            } else if (ext.toString().equals("test2")) {
                test2 = true;
            } else if (ext.toString().equals("test3")) {
                test3 = true;
            }
        }
        assertTrue(test1);
        assertTrue(test2);
        assertTrue(test3);
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *  
     *    Testcase Name: lookup(String fileExtension)
     *    Testcase Number: EMB030
     *  
     *    setup:
     *  
     *    test procedure:
     *    1.access MediaFormatRegistry.SINGLETON, call lookup(null)
     *      expected result: NullPointerException
     *  
     *    2.access MediaFormatRegistry.SINGLETON
     *      call unbind(&quot;test1&quot;) (catch FormatNotFoundException)
     *      call lookup passing &quot;test1&quot;
     *      expected result: FormatNotFoundException
     *  
     *    3.access MediaFormatRegistry.SINGLETON
     *      call rebind(&quot;test2&quot;, new GenericMediaFormat())
     *      call lookup(&quot;test2&quot;)
     *      expected result: instance of GenericMediaFormat()
     *  
     *   
     *  
     * </pre>
     */
    public void testEMB030() throws MediaException {
        //
        // test 1
        //
        MediaFormatRegistry testInstance = MediaFormatRegistry.SINGLETON;
        try {
            testInstance.lookup(null);
            fail("test 1: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw a NullPointerExceptin but threw " + e.toString());
        }
        //
        // test 2
        //
        try {
            testInstance.unbind("test1");
        } catch (FormatNotFoundException e) {
        }

        try {
            MediaFormat testFormat = testInstance.lookup("test1");
            fail("test 2: Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw A FormatNotFoundException but threw " + e.toString());
        }
        //
        // test 3
        //
        testInstance = MediaFormatRegistry.SINGLETON;
        testInstance.rebind("test1", new GenericMediaFormat());
        MediaFormat testFormat = testInstance.lookup("test1");
        assertEquals("test 3: ", testFormat.getClass(), new GenericMediaFormat().getClass());
        testTrace("test 3 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *  
     *    Testcase Name: rebind(String fileExtension, MediaFormat mediaFormat)
     *    Testcase Number: EMB031
     *  
     *    setup:
     *  
     *    test procedure:
     *    1.call rebind with null fileExtension and instance of GenericMediaFormat
     *      expected result: NullPointerException
     *  
     *    2.call rebind with random string and null format
     *      expected result: NullPointerException
     *  
     *    3.call bind with fileExtension &quot;test&quot; and JpegFormat
     *      call rebind with fileExtension &quot;test&quot; and BmpFormat
     *      call lookup(&quot;test&quot;)
     *      expected result: instance of BmpFormat
     *  
     *    4.call unbind with random fileExtension
     *      call rebind with random fileExtension and GenericMediaFormat
     *      call lookup with fileExtension
     *      expected result: instance of GenericMediaFormat
     *  
     *   
     *  
     * </pre>
     */
    public void testEMB031() throws MediaException {
        //
        // test 1
        //
        MediaFormatRegistry testInstance = MediaFormatRegistry.SINGLETON;
        try {
            testInstance.rebind(null, new GenericMediaFormat());
            fail("test 1: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 2
        //
        try {
            testInstance.rebind(EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(1, 3)), null);
            fail("test 2: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 3
        //
        testInstance = MediaFormatRegistry.SINGLETON;
        testInstance.rebind("test", new JpegFormat());
        testInstance.rebind("test", new BmpFormat());

        MediaFormat testFormat = testInstance.lookup("test");
        assertTrue("test 3: ", testFormat instanceof BmpFormat);
        testTrace("test 3 passed");
        //
        // test 4
        //
        String fileExt = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(1, 3));
        testInstance = MediaFormatRegistry.SINGLETON;
        try {
            testInstance.unbind(fileExt);
        } catch (FormatNotFoundException e) {
        }

        testInstance.rebind(fileExt, new GenericMediaFormat());
        testFormat = testInstance.lookup(fileExt);
        assertTrue("test 4: ", testFormat instanceof GenericMediaFormat);
        testTrace("test 4 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *  
     *    Testcase Name: unbind(String fileExtension)
     *    Testcase Number: EMB032
     *  
     *    setup:
     *  
     *    test procedure:
     *    1.access MediaFormatRegistry.SINGLETON
     *      call unbind passing null
     *      expected result: NullPointerException
     *  
     *    2.access MediaFormatRegistry.SINGLETON
     *      call unbind(&quot;test&quot;), catch FormatNotFoundExceptin
     *      call unbind(&quot;test&quot;)
     *      expected result: FormatNotFoundException
     *  
     *    3.access MediaFormatRegistry.SINGLETON
     *      call bind passing &quot;test1&quot; and GenericMediaFormat
     *      call bind passing &quot;test2&quot; and GenericMediaFormat
     *      call unbind passing &quot;test1&quot;
     *      call unbind passing &quot;test2&quot;
     *      expected result: no exception
     *  
     *   
     *  
     * </pre>
     */
    public void testEMB032() throws MediaException {
        //
        // test 1
        //
        MediaFormatRegistry testInstance = MediaFormatRegistry.SINGLETON;
        try {
            testInstance.unbind(null);
            fail("test 1: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 2
        //
        try {
            testInstance.unbind("test");
        } catch (FormatNotFoundException e) {
        }

        try {
            testInstance.unbind("test");
            fail("test 2: Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw a FormatNotFoundException but threw " + e.toString());
        }
        //
        // test 3
        //
        testInstance = MediaFormatRegistry.SINGLETON;
        testInstance.rebind("test1", new GenericMediaFormat());
        testInstance.rebind("test2", new GenericMediaFormat());
        testInstance.unbind("test1");
        testInstance.unbind("test2");
        testTrace("test 3 passed");

        succeed("");
    }
}