package com.ibm.emb.test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.emb.FormatSyntaxException;
import javax.emb.GenericMediaFormat;
import javax.emb.Media;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaHeader;
import javax.emb.MediaSegment;

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

/**
 * <pre>
 * 
 *  
 *   
 *     Line Item: MFB API 
 *     Subcategory 1: javax.emb
 *     Subcategory 2: GenericMediaFormat
 *    
 *     Test class for javax.emb.GenericMediaFormat which extends the
 *     super class MediaFormatTest.  These tests are more specific
 *     to the GenericMediaFormat
 *    
 *   
 *  
 * </pre>
 */
public class GenericMediaFormatTest extends MediaFormatTest {

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

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

    public MediaFormat createMediaFormat() {
        return new GenericMediaFormat();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: assembleContent(URL mediaLocation, MediaSegment[] mediaSegments)
     *    Testcase Number: EMB020
     *    
     *    setup:
     *    
     *    test procedure:
     *    1.create 2 element segment array
     *      call assembleContent with the segment array created and null as location
     *      expected result: FormatSyntaxException
     *    
     *    2.create 0 element segment array
     *      call assembleContent with the segment array created and null as location
     *      expected result: empty byte array
     *    
     *    3.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
     *      expected result: FormatSyntaxException
     *    
     *    4.call assembleContent with null as segment array and null as location
     *      expected result: NullPointerException
     *    
     *    5.create 1 element segment array with null as element
     *      call assembleContent with the segment array created and null as location
     *      expected result: NullPointerException
     *    
     *    6.create 1 element segment array, fill in random content null as child location
     *      call assembleContent with the segment array created and null as location
     *      expected result: content equal to the random content passed with the segment array
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB020() throws MediaException, MalformedURLException {
        byte[] testByteArray = null;
        //
        // test 1
        //
        MediaSegment mediaSegment1 = new MediaSegment();
        mediaSegment1.setContent(EMBStaticHelper.createRandomByteArray(2048));
        MediaSegment mediaSegment2 = new MediaSegment();
        mediaSegment2.setContent(EMBStaticHelper.createRandomByteArray(2048));
        MediaSegment[] testMediaSegmentArray = new MediaSegment[] {mediaSegment1, mediaSegment2};

        MediaFormat testInstance = createMediaFormat();
        try {
            testInstance.assembleContent(null, testMediaSegmentArray);
            fail("test 1: 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
        //
        testMediaSegmentArray = new MediaSegment[0];
        testByteArray = testInstance.assembleContent(null, testMediaSegmentArray);
        assertEquals("test 2", 0, testByteArray.length);
        testTrace("test 2 passed");
        //
        // test 3
        //
        mediaSegment1.setChildLocation(new URL("file", embTestConfig.getTestClient(), "/testURL"));
        testMediaSegmentArray = new MediaSegment[] {mediaSegment1};
        testInstance = createMediaFormat();

        try {
            testInstance.assembleContent(null, testMediaSegmentArray);
            fail("test 3: Should throw a FormatSyntaxException");
        } catch (FormatSyntaxException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3: Should throw a FormatSyntaxException but threw " + e.toString());
        }
        //
        // test 4
        //
        testInstance = createMediaFormat();
        try {
            testInstance.assembleContent(null, null);
            fail("test 4: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 4 passed");
        } catch (Exception e) {
            fail("test 4: Should throw a NullPointerExceptin but threw " + e.toString());
        }
        //
        // test 5
        //
        testMediaSegmentArray = new MediaSegment[1];
        testMediaSegmentArray[0] = null;
        testInstance = createMediaFormat();
        try {
            testInstance.assembleContent(null, testMediaSegmentArray);
            fail("test 5: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 5 passed");
        } catch (Exception e) {
            fail("test 5: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 6
        //
        mediaSegment1 = new MediaSegment();
        mediaSegment1.setContent(EMBStaticHelper.createRandomByteArray(1024));
        testMediaSegmentArray = new MediaSegment[] {mediaSegment1};
        testByteArray = testInstance.assembleContent(null, testMediaSegmentArray);
        assertTrue("test 6: ", java.util.Arrays.equals(testByteArray, mediaSegment1.getContent()));

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: disassembleContent(URL mediaLocation, byte[] mediaContent)
     *    Testcase Number: EMB021
     *    
     *    setup:
     *    
     *    test procedure:
     *    1.call disassembleContent with null as mediaContent and null as location
     *      expected result: NullPointerException
     *    
     *    2.create random content byte array
     *      call disassembleContent with the random media content created and null as location
     *      expected result: 1 element segment array containing content equal to random content and null childLocation
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB021() throws MediaException {
        //
        // test 1
        //
        MediaFormat testInstance = createMediaFormat();
        try {
            testInstance.disassembleContent(null, 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
        //
        testInstance = createMediaFormat();
        byte[] testArray = EMBStaticHelper.createRandomByteArray(2048);
        MediaSegment[] testSegment = testInstance.disassembleContent(null, testArray);
        assertEquals("test 2: ", 1, testSegment.length);
        assertSame("test 2: ", testArray, testSegment[0].getContent());
        assertEquals("test 2: ", null, testSegment[0].getChildLocation());
        testTrace("test 2 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: extractHeader(Media media)
     *    Testcase Number: EMB022
     *    
     *    setup:
     *    
     *    test procedure:
     *    1.call extractHeader(null)
     *      expected result: NullPointerException
     *   
     *    2.create random content, call extractHeader with input stream
     *      expected result: instance of GenericMediaHeader
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB022() throws MediaException, IOException {
        //
        // test 1
        //
        MediaFormat testInstance = createMediaFormat();
        try {
            MediaHeader testTemp = testInstance.extractHeader(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
        //
        testInstance = createMediaFormat();
        byte[] testByteArray = EMBStaticHelper.createRandomByteArray(1024);
        InputStream in = new ByteArrayInputStream(testByteArray);
        MediaHeader header = testInstance.extractHeader(in);
        assertTrue("test 2: ", header instanceof javax.emb.GenericMediaHeader);
        testTrace("test 2 passed");
        in.close();

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: extractProxy(Media media)
     *    Testcase Number: EMB023
     *    
     *    setup:
     *    
     *    test procedure:
     *    1.call extractProxy passing null
     *      expected result: NullPointerException
     *    
     *    2.create random content, call extractProxy with input stream
     *      expected result: instance of Media
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB023() throws MediaException, IOException {
        //
        // test 1
        //
        MediaFormat testInstance = createMediaFormat();
        try {
            Media testTemp = testInstance.extractProxy(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
        //
        testInstance = createMediaFormat();
        byte[] testByteArray = EMBStaticHelper.createRandomByteArray(1024);
        InputStream in = new ByteArrayInputStream(testByteArray);
        Media mediaProxy = testInstance.extractProxy(in);
        assertNotNull("test 2: proxy is null", mediaProxy);
        assertTrue("test 2: ", mediaProxy instanceof javax.emb.Media);
        testTrace("test 2 passed");
        in.close();

        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: getDefaultMimeType()
     *    Testcase Number: EMB024
     *    
     *    setup:
     *    
     *    test procedure: 
     *    1.call getDefaultMimeType
     *      expected result: Media.MIME_TYPE_UNKNOWN	
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB024() {
        //
        // test 1
        //
        MediaFormat testInstance = createMediaFormat();
        assertSame("test 1: ", Media.MIME_TYPE_UNKNOWN, testInstance.getDefaultMimeType());
        testTrace("test 1 passed");
        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: isEmbedded()
     *    Testcase Number: EMB025
     *    
     *    setup:
     *    
     *    test procedure: 
     *    1.call isEmbedded
     *      expected result: true
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB025() {
        //
        // test 1
        //
        MediaFormat testInstance = createMediaFormat();
        assertTrue("test 1: ", testInstance.isEmbedded());
        testTrace("test 1 passed");
        succeed();
    }

    /**
     * <pre>
     * 
     *  
     *   
     *    Testcase Name: isStreamingDesirable()
     *    Testcase Number: EMB026
     *    
     *    setup:
     *    
     *    test procedure:
     *    1.call isStreamingDesirable
     *      expected result: false
     *    
     *   
     *  
     * </pre>
     */
    public void testEMB026() {
        //
        // test 1
        //
        MediaFormat testInstance = createMediaFormat();
        assertTrue("test 1: ", !testInstance.isStreamingDesirable());
        testTrace("test 1 passed");
        succeed();
    }
}