package com.ibm.emb.test;

// java stuff
import java.net.MalformedURLException;
import java.net.URL;

import javax.emb.MediaException;
import javax.emb.MediaSegment;

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: MediaSegment
 *  
 * </pre>
 */
public class MediaSegmentTest extends EMBTestCaseBase {

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

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

    /**
     * <pre>
     * 
     *  Testcase Name: getContent()
     *  Testcase Number: EMB033
     *  
     *  setup:
     *  
     *  test procedure:
     *  1.create random content byte array and pass into setContent()
     *    call getContent()
     *    expected result: content should match original byte array	
     *  
     *  2.create new media segment and call getContent
     *    expected result: empty byte array
     *  
     * </pre>
     */
    public void testEMB033() {
        //
        // test 1
        // 
        MediaSegment testInstance = new MediaSegment();
        byte[] testArray = EMBStaticHelper.createRandomByteArray(EMBStaticHelper.randomInt(0, 255));
        testInstance.setContent(testArray);
        assertSame("test 1: ", testArray, testInstance.getContent());
        testTrace("test 1 passed");
        //
        // test 2
        //
        testInstance = new MediaSegment();
        assertEquals("test 2: content is not empty", 0, testInstance.getContent().length);
        testTrace("test 2 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  Testcase Name: getChildLocation()
     *  Testcase Number: EMB034
     *  
     *  setup:
     *  
     *  test procedure:
     *  1.call setChildLoation(null), call getChildLocation()
     *    expected result: null
     *  
     *  2.create random URL with &quot;file&quot; protocol, call setChildLocation with random URL
     *    call getChildLocation()
     *    expected result: return same URL
     *      
     * </pre>
     */
    public void testEMB034() throws MediaException, MalformedURLException {
        //
        // test 1
        //
        MediaSegment testInstance = new MediaSegment();
        testInstance.setChildLocation(null);
        assertNull("test 1: ", testInstance.getChildLocation());
        testTrace("test 1 passed");
        // 
        // test 2
        //
        testInstance = new MediaSegment();
        URL testURL = new URL("file", "localhost", "/testfile");
        testInstance.setChildLocation(testURL);
        assertTrue("test 2: ", testURL.equals(testInstance.getChildLocation()));
        testTrace("test 2 passed");

        succeed();

    }

    /**
     * <pre>
     * 
     *  Testcase Name: setContent(content)
     *  Testcase Number: EMB035
     *  
     *  setup: 
     *  
     *  test procedure
     *  1.call setContent with null
     *    expected result: NullPointerException
     *  
     *  2.call setContent with random byte array
     *    call getContent() 
     *    expected result: content equal to the random one passed in
     *  
     * </pre>
     */
    public void testEMB035() throws MediaException {
        //
        // test 1
        //
        MediaSegment testInstance = new MediaSegment();
        try {
            testInstance.setContent(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 = new MediaSegment();
        byte[] randomArray = EMBStaticHelper.createRandomByteArray(EMBStaticHelper.randomInt(0, 255));
        testInstance.setContent(randomArray);
        assertSame("test 2: ", randomArray, testInstance.getContent());
        testTrace("test 2 passed");

        succeed();
    }

    /**
     * <pre>
     * 
     *  Testcase Name: setChildLocation(URL childLocation)
     *  Testcase Number: EMB036
     *  
     *  setup:
     *  
     *  test procedure:
     *  1.call setChildLocation with null
     *    expected result: NullPointerException
     *  
     *  2.call setChildLocation with URL pointing to local machine and protocol type &quot;file&quot;
     *    call getChildLocation() 
     *    expected result: content equal to the URL passed before
     *  
     * </pre>
     */
    public void testEMB038() throws MediaException, MalformedURLException {
        //
        // test 1
        //
        MediaSegment testInstance = new MediaSegment();
        try {
            testInstance.setChildLocation(null);
            testTrace("test 1 passed");
        } catch (NullPointerException e) {
            fail("test 1: Should not throw a NullPointerException");
        } catch (Exception e) {
            fail("test 1: Should not throw any exception");
        }
        //
        // test 2
        //
        testInstance = new MediaSegment();
        URL testURL = new URL("file", "localhost", "/testfile");
        testInstance.setChildLocation(testURL);
        assertEquals("test 1: ", testURL.toString(), testInstance.getChildLocation().toString());
        testTrace("test 2 passed");

        succeed();
    }
}