package com.ibm.emb.test.mfb;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.emb.FormatSyntaxException;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaSegment;

import com.ibm.emb.junit.EMBStaticHelper;
import com.ibm.emb.test.MediaFormatTest;

public abstract class EmbeddedMediaFormatTest extends MediaFormatTest {

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

    /**
     * abstract method to create an instance of MediaFormat this method needs to
     * be implemented in the test subclass and will create an Instance of the
     * concrete implementation of the class under test
     */
    public abstract MediaFormat createMediaFormat();

    /**
     * <pre>
     * 
     *  
     *   
     *        Testcase Name: assembleContent(URL, MediaSegment[])
     *        Testcase Number: TEMB0054SBA  
     *        
     *        Test 1:
     *        create 2 element segment array
     *        call assembleContent with the segment array created and null as location
     *        ----&gt; check for FormatSyntaxException
     *        
     *        Test 2:
     *        create 1 element segment array, fill in random content and valid random child location
     *        call assembleContent with the segment array created and null as location
     *        ---&gt;check for FormatSyntaxException
     *        
     *   
     *  
     * </pre>
     */
    public void testTEMB0054SBA() throws MediaException, MalformedURLException {
        // media segment
        MediaSegment mediaSegment1 = null;
        MediaSegment[] testMediaSegmentArray = null;

        // test instance
        MediaFormat testInstance = createMediaFormat();

        //
        // test 1
        //
        mediaSegment1 = new MediaSegment();
        testMediaSegmentArray = new MediaSegment[] {mediaSegment1, mediaSegment1};
        try {
            testInstance.assembleContent(null, testMediaSegmentArray);
            fail("test1: should throw a FormatSyntaxException");
        } catch (FormatSyntaxException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: should throw a FormatSyntaxException but threw " + e.toString());
        }

        //
        // test 2
        //
        // name of a jpg sample
        String mediaBmp = embTestConfig.getBmpPictureName1();

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

        // test URL
        URL testURL = new URL("file", "localhost", mediaBmpFullPath);

        // media segment
        mediaSegment1 = new MediaSegment();
        mediaSegment1.setContent(EMBStaticHelper.createRandomByteArray(1024));
        mediaSegment1.setChildLocation(testURL);
        testMediaSegmentArray = new MediaSegment[] {mediaSegment1};
        try {
            testInstance.assembleContent(null, testMediaSegmentArray);
            fail("test2: should throw a FormatSyntaxException");
        } catch (FormatSyntaxException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: should throw a FormatSyntaxException but threw " + e.toString());
        }

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *        Testcase Name: disassembleContent(URL, byte[])
     *        Testcase Number: TEMB0055SBA  
     *        
     *        Test 1:
     *        call disassembleContent with random media content and null as location
     *        ---&gt;check the result to be a 1-element segment array, 
     *        and the element to contain the random content created and a childLocation of null.
     *        
     *   
     *  
     * </pre>
     */
    public void testTEMB0055SBA() throws MediaException, MalformedURLException {
        // media segment
        MediaSegment mediaSegment1 = null;
        MediaSegment[] testMediaSegmentArray = null;
        // test array
        byte[] testByteArray = EMBStaticHelper.createRandomByteArray(1024);

        // test instance
        MediaFormat testInstance = createMediaFormat();

        //
        // test 1
        //

        testMediaSegmentArray = testInstance.disassembleContent(null, testByteArray);
        assertTrue("test 1", (testMediaSegmentArray.length == 1)
                && (java.util.Arrays.equals(testMediaSegmentArray[0].getContent(), testByteArray))
                && (testMediaSegmentArray[0].getChildLocation() == null));
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *        Testcase Name: isEmbedded()
     *        Testcase Number: TEMB0059SBA  
     *        
     *        Test 1:
     *        call isEmbedded
     *        ---&gt;check if result is true
     *        
     *   
     *  
     * </pre>
     */
    public void testTEMB0059SBA() throws MediaException, MalformedURLException {
        // test instance
        MediaFormat testInstance = createMediaFormat();
        assertTrue("test 1", testInstance.isEmbedded());
        testTrace("test 1 passed");

        succeed();
    }

}