package com.ibm.emb.test.mfb;

// java stuff
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.emb.LinkTranslationException;
import javax.emb.MalformedLocationException;
import javax.emb.Media;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaHeader;
import javax.emb.MediaSegment;

import org.objectweb.jonas.emb.mfb.formats.playlist.GenericPlaylistFormat;

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

public class GenericPlayListTest extends MediaFormatTest {

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

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

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

    {
    }

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

    /**
     * creates an instance of GenericPlaylistFormat with the given Parameters
     * used in super clas of GenericPlaylistFormatTest
     */
    public MediaFormat createMediaFormat() {
        return new GenericPlaylistFormat();
    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: assembleContent(URL, MediaSegment[])
     *        Testcase Number: TEMB0109SBA
     *
     *        Test 1:
     *        call disassembleContent and pass in the content of testPlaylist1.lst and &quot;file://myip/embtest/test.lst&quot; as URL
     *        call assembleContent and pass in the result from before and &quot;file://myip/embtest/test.lst&quot; as URL
     *        --&gt; check if no exception occurs
     *
     *        Test 2:
     *        call disassembleContent and pass in the content of test.lst and &quot;file://myip/embtest/test.lst&quot; as URL
     *        call assembleContent and pass in the result from before and null as URL
     *        --&gt; check if no exception occurs
     *        --&gt; check if length of the resulting byte array is larger than the length of the original content
     *
     *        Test 3:
     *        call assembleContent and pass in an empty segment array and null as URL
     *        --&gt; check if no exception occurs
     *        --&gt; check if length of the resulting byte array is 0.
     *
     *
     *
     * </pre>
     */
    public void testTEMB0109SBA() throws MediaException, MalformedURLException, IOException {

        // name of the computer where tests are performed
        String computerName = embTestConfig.getTestClient();

        // url of test playlist
        URL playlistURL = new URL("file", computerName, embTestConfig.getMediaDir() + File.separator
                + embTestConfig.getPlaylistName1());

        // reads content of test playlist (test.lst) and stores to an array
        File playlistFile = new File(embTestConfig.getMediaDir() + File.separator + embTestConfig.getPlaylistName1());
        FileInputStream playlistByteInputStream = new FileInputStream(playlistFile);

        byte[] testArray = new byte[new Long(playlistFile.length()).intValue()];
        playlistByteInputStream.read(testArray);

        // media segment
        MediaSegment mediaSegment1 = null;
        MediaSegment[] testMediaSegmentArray = null;

        // test instance
        MediaFormat testInstance = createMediaFormat();

        //
        // test 1
        //
        byte[] testArray1 = null;
        try {
            testMediaSegmentArray = testInstance.disassembleContent(playlistURL, testArray);
            testArray1 = testInstance.assembleContent(playlistURL, testMediaSegmentArray);
        } catch (Exception e) {
            fail("No exception should be thrown but this one appears : " + e.toString());
        }
        testTrace("test 1 passed");

        //
        // test 2
        //
        byte[] testArray2 = null;
        try {
            testMediaSegmentArray = testInstance.disassembleContent(playlistURL, testArray);
            testArray2 = testInstance.assembleContent(null, testMediaSegmentArray);
        } catch (Exception e) {
            fail("No exception should be thrown but this one appears : " + e.toString());
        }
        assertTrue("test 2: ", testArray2.length > testArray.length);
        testTrace("test 2 passed");

        //
        // test 3
        //
        byte[] testArray3 = null;
        try {
            MediaSegment[] testMediaSegmentArray3 = new MediaSegment[0];
            testArray3 = testInstance.assembleContent(null, testMediaSegmentArray3);
        } catch (Exception e) {
            fail("No exception should be thrown but this one appears : " + e.toString());
        }
        assertTrue("test 2: ", testArray3.length == 0);
        testTrace("test 3 passed");

        succeed();

    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: disassembleContent(URL, byte[])
     *        Testcase Number: TEMB0108SBA
     *
     *        Test 1:
     *        call disassembleContent and pass in the content of test.lst and null as URL
     *        --&gt; check for LinkTranslationException
     *
     *
     *        Test 2:
     *        call disassembleContent and pass in the content of test.lst and &quot;file://myip/embtest/test.lst&quot; as URL
     *        --&gt; check result for 3 elements
     *        --&gt; check each element for childLink &quot;file://myip/embtest/test.jpg&quot;
     *
     *
     *        Test 3:
     *        call disassembleContent and pass in &quot;file://foo&quot;.getBytes() as content and  &quot;file://myip/embtest/test.lst&quot; as URL
     *        --&gt; check for MalformedLocationException
     *
     *
     *
     *
     * </pre>
     */
    public void testTEMB0108SBA() throws MediaException, MalformedURLException, IOException {

        String computerName = embTestConfig.getTestClient();

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

        // url of test playlist
        String gplURLlocation = embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getPlaylistName1();
        String gplURLlocationPath = gplURLlocation.substring(0, gplURLlocation.lastIndexOf("/"));
        URL playlistURL = new URL("file", computerName, gplURLlocation);

        // URL playlistURL = new URL("file", computerName,
        // embTestConfig.getTestServerMediaDir() + "/" + );

        // reads content of test playlist (test.lst) and stores to an array
        File playlistFile = new File(embTestConfig.getMediaDir() + File.separator + embTestConfig.getPlaylistName1());

        byte[] testArray = getByteArrayFromFile(playlistFile);
        // playlistByteInputStream.read(testArray);

        // media segment
        MediaSegment mediaSegment1 = null;
        MediaSegment[] testMediaSegmentArray = null;

        // test instance
        MediaFormat testInstance = createMediaFormat();

        //
        // test 1
        //
        byte[] testArray1 = null;
        try {
            testMediaSegmentArray = testInstance.disassembleContent(null, testArray);
            fail("LinkTranslationException should be thrown");
        } catch (LinkTranslationException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("LinkTranslationException should be thrown but this one appears : " + e.toString());
        }

        //
        // test 2
        //
        try {
            boolean pathCheck = true;
            testMediaSegmentArray = testInstance.disassembleContent(playlistURL, testArray);

            for (int i = 0; i < testMediaSegmentArray.length - 1; i++) {
                // path check
                if (!testMediaSegmentArray[i].getChildLocation().toString().startsWith(
                        "file://" + embTestConfig.getTestClient() + gplURLlocationPath)) {
                    pathCheck = false;
                }
            }

            assertTrue("test 2: ", testMediaSegmentArray.length == 3 && pathCheck);

            testTrace("test 2 passed");

        } catch (Exception e) {
            fail("No exception should be thrown but this one appears : " + e.toString());
        }

        //
        // test 3
        //

        byte[] fooArray = new String("file://foo").getBytes();

        logProductError("reported error PEMB0058RMD");

        try {
            testMediaSegmentArray = testInstance.disassembleContent(playlistURL, fooArray);
            fail("MalformedLocationException should be thrown");
        } catch (MalformedLocationException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("MalformedLocationException should be thrown but this one appears : " + e.toString());
        }

    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: extractHeader(Media)
     *        Testcase Number: TEMB0113SBA
     *
     *        Test 1:
     *        call extractHeader passing null
     *        --&gt; check result to be an instance of GenericMediaHeader
     *
     *
     *
     * </pre>
     */
    public void testTEMB0113SBA() throws MediaException {
        //
        // test 1
        //

        // test instance
        MediaFormat testInstance = createMediaFormat();
        MediaHeader mediaHeader = testInstance.extractHeader(null);
        assertTrue("test1: ", mediaHeader instanceof MediaHeader);
        testTrace("test 1 passed");

        succeed();

    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: extractSurrogate(Media)
     *        Testcase Number: TEMB0110SBA
     *
     *        Test 1:
     *        call extractSurrogate passing null
     *        check result to be an instance of Media
     *        access the content of the resulting media ----&gt;  to verify it is accessible
     *
     *
     *
     * </pre>
     */
    public void testTEMB0110SBA() throws MediaException, RemoteException {
        //
        // test 1
        //

        // test instance
        MediaFormat testInstance = createMediaFormat();
        Media media = testInstance.extractProxy(null);

        // checks if media accessable and testinstance instance of media
        try {
            byte[] mediaArray = media.getContent();
            assertTrue("test1: ", media instanceof Media);
        } catch (Exception e) {
            fail("test 1 failed, exception : ");
        }

        testTrace("test 1 passed");

        succeed();

    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: getDefaultMimeType()
     *        Testcase Number: TEMB0112SBA
     *
     *        Test 1:
     *        call getDefaultMimeType
     *        ---&gt;check if result equals &quot;www/unknown&quot;
     *
     *
     *
     * </pre>
     */
    public void testTEMB0112SBA() throws MediaException {
        //
        // test 1
        //

        // test instance
        MediaFormat testInstance = createMediaFormat();

        assertEquals("test1: ", "www/unknown", testInstance.getDefaultMimeType());
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: isStreamingDesirable()
     *        Testcase Number: TEMB0111SBA
     *
     *        call isStreamingDesirable
     *        ---&gt;check if result is true
     *
     *
     *
     * </pre>
     */
    public void testTEMB0111SBA() {
        //
        // test 1
        //

        // test instance
        MediaFormat testInstance = createMediaFormat();

        assertTrue("test1: ", testInstance.isStreamingDesirable());
        testTrace("test 1 passed");

        succeed();
    }
}