package com.ibm.emb.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.emb.ContentAccessException;
import javax.emb.FormatNotFoundException;
import javax.emb.GenericMediaFormat;
import javax.emb.Media;
import javax.emb.MediaBean;
import javax.emb.MediaEntityLocalHome;
import javax.emb.MediaException;
import javax.emb.NoServerFoundException;
import javax.emb.ProtocolConstraints;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import org.objectweb.jonas.emb.mfb.formats.image.BmpFormat;
import org.objectweb.jonas.emb.mfb.formats.image.JpegFormat;
import org.objectweb.jonas.emb.mfb.formats.playlist.GenericPlaylistFormat;

import com.ibm.emb.junit.ComplexMediaLocationInfo;
import com.ibm.emb.junit.EMBStaticHelper;
import com.ibm.emb.junit.EMBTestCaseBase;
import com.ibm.emb.junit.EmbeddedMediaLocationInfo;
import com.ibm.emb.meb.test.ejb.MediaEntityLocalTestDriver;
import com.ibm.emb.meb.test.ejb.MediaEntityLocalTestDriverHome;

/**
 * <pre>
 *
 *
 *
 *
 *
 *
 *       Line Item: MEB API
 *       Subcategory 1: javax.emb
 *       Subcategory 2: MediaEntityLocalHome
 *
 *
 *
 *
 *
 *
 * </pre>
 */
public class MediaEntityLocalHomeTest extends EMBTestCaseBase {

    private static String MEBTESTER_JNDI = "java:comp/env/ejb/MediaEntityLocalTestDriverHome";

    private InitialContext ic;

    private MediaEntityLocalTestDriverHome mebHome;

    private MediaEntityLocalTestDriver mebTester = null;

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

    /**
     * do initialisation of each individual Test method here this method is
     * called before each individual Test Method
     */
    protected void setUp() throws RemoteException, CreateException {
        if (mebHome == null) {
            try {
                ic = new InitialContext();
                Object ojb = ic.lookup(MEBTESTER_JNDI);
                mebHome = (MediaEntityLocalTestDriverHome) PortableRemoteObject.narrow(ojb,
                        MediaEntityLocalTestDriverHome.class);
            } catch (NamingException ne) {
                System.out.println("NamingException thrown during setup. Msg: " + ne.getMessage());
            }
        }
        mebTester = mebHome.create();
        return;
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: create()
     *       Testcase Number: EMB134
     *
     *       setup: create testInstance of MediaEntityLocalHome
     *
     *       test procedure:
     *       1.call testInstance.create()
     *         expected result: new Media entity created and verify primary key is not null
     *       					  verify initial values of properties
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB134() throws MediaException, NamingException, FinderException, RemoteException, CreateException {

        //
        // test 1
        //
        String mediaEntity = mebTester.createMediaEntityBean();
        assertNotNull("test 1: primary key is null", mediaEntity);
        assertTrue("test 1: timestamp is not initialized", mebTester.getMediaEntityLastModified(mediaEntity) > 0);

        try {
            mebTester.getMediaEntityContent(mediaEntity);
            fail("test 1: (content) Should throw a ContentAccessException");
        } catch (ContentAccessException e) {
            // correct exception
        } catch (Exception e) {
            fail("test 1: (content) Should throw a ContentAccessException but threw " + e.toString());
        }
        assertNull("test 1: description should be null", mebTester.getMediaEntityDescription(mediaEntity));

        try {
            mebTester.getMediaEntityFormat(mediaEntity);
            fail("test 1: (format) Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
            // correct exception
        } catch (Exception e) {
            fail("test 1: (format) Should throw a FormatNotFoundException but threw " + e.toString());
        }

        try {
            mebTester.getMediaEntityHeader(mediaEntity);
            fail("test 1: (header) Should throw a FormatNotFoundException or ContentAccessException");
        } catch (FormatNotFoundException e) {
        } catch (ContentAccessException e) {
        } catch (Exception e) {
            fail("test 1: (header) Should throw a FormatNotFoundException or ContentAccessException but threw "
                    + e.toString());
        }

        assertNull("test 1: name is not null", mebTester.getMediaEntityName(mediaEntity));

        try {
            mebTester.getMediaEntityMimeType(mediaEntity);
            fail("test 1: (mimeType) Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
        } catch (Exception e) {
            fail("test 1: (mimeType) Should throw a FormatNotFoundException but threw " + e.toString());
        }

        assertNull("test 1: location is not null", mebTester.getMediaEntityLocation(mediaEntity));

        assertNull("test 1: previousVersion is not null", mebTester.getMediaEntityPreviousVersion(mediaEntity));
        assertNull("test 1: nextVersion is not null", mebTester.getMediaEntityNextVersion(mediaEntity));
        assertEquals("test 1: should have no listeners", 0, mebTester.getMediaEntityListeners(mediaEntity).length);
        assertEquals("test 1: should have no parents", 0, mebTester.getMediaEntityParents(mediaEntity).length);
        assertEquals("test 1: should have no children", 0, mebTester.getMediaEntityChildren(mediaEntity).length);
        assertEquals("test 1: should have no meta data", 0, mebTester.getMediaEntityMetaData(mediaEntity).length);
        assertNotNull("test 1: proxy is null", mebTester.getMediaEntityProxy(mediaEntity));

        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: exportMedia(MediaEntityLocal[], URL)
     *       Testcase Number: EMB135
     *
     *       setup: create testInstance of MediaEntityLocalHome
     *              create Media entity me1 from embedded media file
     *       	   	  create Media entity me2 from nonembedded media file
     *       		  rebind corresponding formats to registry
     *
     *       test procedure:
     *       1.call exportMedia(null, valid location)
     *         expected result: NullPointerException
     *
     *       2.call exportMedia(new MediaEntityLocal[]{me1, null}, valid location)
     *       	 expected result: 2 element array with second element being null
     *
     *       3.call exportMedia(new MediaEntityLocal[0], valid location)
     *       	 expected result: empty array
     *
     *       4.call exportMedia(new MediaEntityLocal[]{me1, me2}, valid location)
     *       	 expected result: 2 element array of URL's, verify media entities in new location are accessible
     *
     *       5.repeat test 4
     *       	 expected result: files copied and files from test 4 are not overwritten
     *
     *       6.create Media entity from nonexistent file, add to entity array
     *       	 call exportMedia with array and valid location
     *       	 expected result: ContentAccessException
     *
     *       7.create Media entity from random byte array and add to array
     *       	 call exportMedia with array and valid location
     *       	 expected result: MediaFormatException
     *
     *       8.call exportMedia(new MediaEntityLocal[]{me1, me1}, valid location)
     *       	 expected result: 2 element array of URL's, verify URL's are the same
     *
     *       9.call exportMedia(new MediaEntityLocal[]{me1}, null)
     *         expected result: no exception, media copied to default location
     *
     *       10.call importMedia on two nonembedded media with a common child
     *          call exportMedia with media entities created and valid location
     *          expected result: 2 parents copied with only one copy of common child
     *       					   use URLs to access media entities and check children
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB135() throws MediaException, MalformedURLException, IOException, CreateException,
            NamingException, RemoteException, FinderException {

        mebTester.bindMediaFormat("jpg", new JpegFormat());
        mebTester.bindMediaFormat("bmp", new BmpFormat());
        mebTester.bindMediaFormat("gpl", new GenericPlaylistFormat());
        mebTester.bindMediaFormat("test", new GenericMediaFormat());

        EmbeddedMediaLocationInfo me1ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName1());
        String me1E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me1E, me1ELocInfo.getMediaName());
        mebTester.setMediaEntityContent(me1E, me1ELocInfo.getMediaFile());

        ComplexMediaLocationInfo me2NELocInfo = new ComplexMediaLocationInfo(ComplexMediaLocationInfo.MEDIAGPL1);
        String me2NE = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me2NE, me2NELocInfo.getRootFile().getName());
        mebTester.importMediaEntityContent(me2NE, me2NELocInfo.getRootURL(), me2NELocInfo.getRootFile().getName());

        int exception = -1;
        //
        // test 1
        //
        // String targetDirectory = "/C:/" + embTestConfig.getMediaDir() + "/" +
        // "export";
        String targetDirectory = embTestConfig.getMediaDir() + File.separatorChar + "export";
        try {
            exception = mebTester.exportHomeMediaExceptions(null, new URL("file", embTestConfig
                    .getTestClientIPAddress(), targetDirectory));
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MediaEntityLocalTestDriver.NULLPOINTER, exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        URL[] exportedMediaURLs = mebTester.exportHomeMediaEntityBeans(new String[] {me1E, null}, new URL("file",
                embTestConfig.getTestClientIPAddress(), targetDirectory));
        assertEquals("test 2: less than 2 URL's", 2, exportedMediaURLs.length);
        assertTrue("test 2: content does not match", java.util.Arrays.equals(mebTester.getMediaEntityContent(me1E),
                getByteArrayFromFile(new File(exportedMediaURLs[0].getFile()))));
        assertNull("test 2: second URL not null", exportedMediaURLs[1]);
        testTrace("test 2 passed");
        //
        // test 3
        //
        exportedMediaURLs = mebTester.exportHomeMediaEntityBeans(new String[0], new URL("file", embTestConfig
                .getTestClientIPAddress(), targetDirectory));
        assertEquals("test 3: array not empty", 0, exportedMediaURLs.length);
        testTrace("test 3 passed");

        //
        // test 4
        //
        exportedMediaURLs = mebTester.exportHomeMediaEntityBeans(new String[] {me1E, me2NE}, new URL("file",
                embTestConfig.getTestClientIPAddress(), targetDirectory));
        assertEquals("test 4: number of URLs incorrect", 2, exportedMediaURLs.length);

        String[] names = new String[] {"test.jpg", "test.gpl"};
        String[] exportedMEBs = null;
        try {
            exportedMEBs = mebTester.importHomeMediaEntityBeans(exportedMediaURLs, names);
        } catch (Exception e) {
            fail("test 4: could not reimport media");
        }
        assertTrue("test 4: content of me1E does not match", java.util.Arrays.equals(mebTester
                .getMediaEntityContent(me1E), mebTester.getMediaEntityContent(exportedMEBs[0])));

        // check children of nonembedded media copied correctly
        String[] exportedChildren = mebTester.getMediaEntityChildren(exportedMEBs[1]);
        assertEquals("test 4: number of children does not match", mebTester.getMediaEntityChildren(me2NE).length,
                mebTester.getMediaEntityChildren(exportedMEBs[1]).length);
        testTrace("test 4 passed");

        //
        // test 5
        //
        URL[] secondMediaURLArray = mebTester.exportHomeMediaEntityBeans(new String[] {me1E, me2NE}, new URL("file",
                embTestConfig.getTestClientIPAddress(), targetDirectory));
        assertEquals("test 5: does not contain 2 elements", secondMediaURLArray.length, 2);
        for (int j = 0; j < secondMediaURLArray.length; j++) {
            assertFalse("test 5: media overwritten", secondMediaURLArray[j].equals(exportedMediaURLs[j]));
        }
        testTrace("test 5 passed");
        //
        // test 6
        //
        String me3E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me3E, "test.jpg");
        try {
            exception = mebTester.exportHomeMediaExceptions(new String[] {me3E}, new URL("file", embTestConfig
                    .getTestClientIPAddress(), targetDirectory));
        } catch (Throwable e) {
            fail("test 6 threw " + e.toString());
        }
        assertEquals("test 6: Should throw a ContentAccessException", MediaEntityLocalTestDriver.CONTENTACCESS,
                exception);
        testTrace("test 6 passed");
        //
        // test 7
        //
        String mediaFullPath = embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar
                + "test.gpl";
        FileOutputStream mediaBmpOutputStream = new FileOutputStream(mediaFullPath);
        mediaBmpOutputStream.write(EMBStaticHelper.createRandomByteArray(1024));
        mediaBmpOutputStream.close();

        String me4E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me4E, "testBean.test");
        mebTester.setMediaEntityContent(me4E, new File(mediaFullPath));
        mebTester.setMediaEntityName(me4E, "testBean.gpl");

        try {
            exception = mebTester.exportHomeMediaExceptions(new String[] {me4E}, new URL("file", embTestConfig
                    .getTestClientIPAddress(), targetDirectory));
        } catch (Throwable e) {
            fail("test 7 threw " + e.toString());
        }
        assertEquals("test 7: Should throw a MediaFormatException", MediaEntityLocalTestDriver.MEDIAFORMAT, exception);
        testTrace("test 7 passed");
        //
        // test 8
        //
        exportedMediaURLs = mebTester.exportHomeMediaEntityBeans(new String[] {me1E, me1E}, new URL("file",
                embTestConfig.getTestClientIPAddress(), targetDirectory));
        assertTrue("test 8: URL's not equal", exportedMediaURLs[0].equals(exportedMediaURLs[1]));
        testTrace("test 8 passed");

        //
        // test 9
        //
        try {
            exportedMediaURLs = mebTester.exportHomeMediaEntityBeans(new String[] {me1E}, null);
            assertEquals("test 9: media not copied", 1, exportedMediaURLs.length);
            testTrace("test 9 passed");
        } catch (Exception e) {
            fail("test 9 threw " + e.toString());
        }

        //
        // test 10
        //
        // ahrrrr C:/ !!!!
        // URL playlist2URL = new URL("file",
        // embTestConfig.getTestClientIPAddress(), "/C:" +
        // embTestConfig.getMediaDir() + "/" +
        // embTestConfig.getPlaylistName2());
        URL playlist2URL = new URL("file", embTestConfig.getTestClientIPAddress(), embTestConfig.getMediaDir()
                + File.separatorChar + embTestConfig.getPlaylistName2());
        String[] neMediaArray = mebTester.importHomeMediaEntityBeans(
                new URL[] {me2NELocInfo.getRootURL(), playlist2URL}, null);
        assertEquals("test 10: neMediaArray does not contain 2 elements", neMediaArray.length, 2);
        assertEquals("test 10: testPlaylist1 does not have 3 children", mebTester
                .getMediaEntityChildren(neMediaArray[0]).length, 3);
        assertEquals("test 10: testPlaylist2 does not have 1 child",
                mebTester.getMediaEntityChildren(neMediaArray[1]).length, 1);

        // verify only one media entity created for common child
        boolean commonChild = false;
        String[] meChildren = mebTester.getMediaEntityChildren(neMediaArray[0]);
        for (int i = 0; i < meChildren.length; i++) {
            if (mebTester.getMediaEntityChildren(neMediaArray[1])[0].equals(meChildren[i])) {
                commonChild = true;
                break;
            }
        }
        assertTrue("test 10: more than 1 entity imported for common child", commonChild);

        // exportMedia and verify only one copy of common child created
        exportedMediaURLs = mebTester.exportHomeMediaEntityBeans(neMediaArray, new URL("file", embTestConfig
                .getTestClientIPAddress(), targetDirectory));
        neMediaArray = mebTester.importHomeMediaEntityBeans(exportedMediaURLs, null);
        meChildren = mebTester.getMediaEntityChildren(neMediaArray[0]);
        for (int i = 0; i < meChildren.length; i++) {
            if (mebTester.getMediaEntityChildren(neMediaArray[1])[0].equals(meChildren[i])) {
                commonChild = true;
                break;
            }
        }
        assertTrue("test 10: more than 1 entity exported for common child", commonChild);
        testTrace("test 10 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: findByPartialDescription(String)
     *       Testcase Number: EMB136
     *
     *       setup: create Media entities me1, me2, and me3
     *       	      call me1.setDescription(&quot;foo1&quot;)
     *       		  call me2.setDescription(&quot;foo2&quot;)
     *       		  leave me3's description as null
     *
     *       test procedure:
     *       1.call findByPartialDescription(null)
     *         expected result: empty array?
     *
     *       2.call findByPartialDescription(&quot;foo&quot;)
     *       	 expected result: 2 element array
     *
     *       3.call findByPartialDescription(&quot;FOO&quot;)
     *       	 expected result: empty array
     *
     *       4.call findByPartialDescription(&quot;oO1&quot;)
     *       	 expected result: empty array
     *
     *       5.call findByPartialDescription(&quot;oO2&quot;)
     *         expected result: empty array
     *
     *       6.call findByPartialDescription(&quot;fooo&quot;)
     *         expected result: empty array
     *
     *       7.call findByPartialDescription(&quot;&quot;)
     *         expected result: 2 element array
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB136() throws NamingException, MalformedURLException, IOException, FinderException,
            RemoveException, CreateException, MediaException {

        mebTester.bindMediaFormat("jpg", new JpegFormat());

        EmbeddedMediaLocationInfo me1ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName1());
        String me1E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me1E, "test1.jpg");
        mebTester.setMediaEntityContent(me1E, me1ELocInfo.getMediaFile());
        mebTester.setMediaEntityDescription(me1E, "foo1");

        EmbeddedMediaLocationInfo me2ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName2());
        String me2E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me2E, "test2.jpg");
        mebTester.setMediaEntityContent(me2E, me2ELocInfo.getMediaFile());
        mebTester.setMediaEntityDescription(me2E, "foo2");

        String[] foundMEBs = null;
        //
        // test 1
        //
        foundMEBs = mebTester.findMEBByPartialDescription(null);
        assertEquals("test 1: Should return empty array", 0, foundMEBs.length);
        testTrace("test 1 passed");
        //
        // test 2
        //
        foundMEBs = mebTester.findMEBByPartialDescription("foo");
        assertTrue("test 2: (foo) Should return at least 2 elements", foundMEBs.length >= 2);
        List alist = Arrays.asList(foundMEBs);
        assertTrue("test 2: me1 is not in array", alist.contains(me1E));
        assertTrue("test 2: me2 is not in array", alist.contains(me2E));
        testTrace("test 2 passed");
        //
        // test 3
        //
        foundMEBs = mebTester.findMEBByPartialDescription("FOO");
        assertTrue("test 3: (FOO) Should return empty array", foundMEBs.length >= 0);
        alist = Arrays.asList(foundMEBs);
        assertFalse("test 3: me1 is in array", alist.contains(me1E));
        assertFalse("test 3: me2 is in array", alist.contains(me2E));
        testTrace("test 3 passed");
        //
        // test 4
        //
        foundMEBs = mebTester.findMEBByPartialDescription("oO1");
        assertTrue("test 4: (oO1) Should return empty array", foundMEBs.length >= 0);
        alist = Arrays.asList(foundMEBs);
        assertFalse("test 4: me1 is in array", alist.contains(me1E));
        testTrace("test 4 passed");
        //
        // test 5
        //
        foundMEBs = mebTester.findMEBByPartialDescription("oO2");
        assertTrue("test 5: (oO2) Should return empty array", foundMEBs.length >= 0);
        alist = Arrays.asList(foundMEBs);
        assertFalse("test 5: me2 is in array", alist.contains(me2E));
        testTrace("test 5 passed");
        //
        // test 6
        //
        foundMEBs = mebTester.findMEBByPartialDescription("fooo");
        assertTrue("test 6: (fooo) Should return empty array", foundMEBs.length >= 0);
        alist = Arrays.asList(foundMEBs);
        assertFalse("test 6: me1 is in array", alist.contains(me1E));
        assertFalse("test 6: me2 is in array", alist.contains(me2E));
        testTrace("test 6 passed");
        //
        // test 7
        //
        foundMEBs = mebTester.findMEBByPartialDescription("");
        assertTrue("test 7: (\"\") Should return at least 2 elements", foundMEBs.length >= 2);
        alist = Arrays.asList(foundMEBs);
        assertTrue("test 7: me1 is in array", alist.contains(me1E));
        assertTrue("test 7: me2 is in array", alist.contains(me2E));
        testTrace("test 7 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: findByPartialLocation(String)
     *       Testcase Number: EMB137
     *
     *       setup: create Media entities me1, me2, and me3
     *       		  call me1.setName and me1.setLocation
     *       		  call me2.setName and me2.setLocation
     *        	  call me3.setName and me3.setContent
     *
     *       test procedure:
     *       1.call findByPartialLocation(null)
     *       	 expected result: empty array
     *
     *       2.call findByPartialLocation(&quot;file&quot;)
     *       	 expected result: 1 element array
     *
     *       3.call findByPartialLocation(location1)
     *         expected result: 1 element array
     *
     *       4.call findByPartialLocation(location2)
     *       	 expected result: 1 element array
     *
     *       5.call findByPartialLocation(test client IP address)
     *         expected result: 2 element array
     *
     *       6.call findByPartialLocation(&quot;//////&quot;)
     *       	 expected result: empty array
     *
     *       7.call findByPartialLocation(&quot;&quot;)
     *         expected result: empty array
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB137() throws NamingException, MalformedURLException, IOException, FinderException,
            RemoveException, CreateException, MediaException {

        mebTester.bindMediaFormat("jpg", new JpegFormat());
        mebTester.bindMediaFormat("bmp", new BmpFormat());

        EmbeddedMediaLocationInfo me1ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName1());
        String me1E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me1E, "test1.jpg");
        mebTester.setMediaEntityLocation(me1E, me1ELocInfo.getMediaURL());

        EmbeddedMediaLocationInfo me2ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName2());
        String me2E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me2E, "test2.jpg");
        mebTester.setMediaEntityLocation(me2E, me2ELocInfo.getMediaURL());

        EmbeddedMediaLocationInfo me3ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getBmpPictureName1());
        String me3E = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me3E, "test3.bmp");
        mebTester.setMediaEntityContent(me3E, me3ELocInfo.getMediaFile());

        String[] foundMEBs = null;
        //
        // test 1
        //
        foundMEBs = mebTester.findMEBByPartialLocation(null);
        assertEquals("test 1: (null) Should return empty array", 0, foundMEBs.length);
        testTrace("test 1 passed");
        //
        // test 2
        //
        foundMEBs = mebTester.findMEBByPartialLocation("file");
        assertTrue("test 2: (file) Should return at least 2 elements", foundMEBs.length >= 2);
        List alist = Arrays.asList(foundMEBs);
        assertTrue("test 2: me1 is not in array", alist.contains(me1E));
        assertTrue("test 2: me2 is not in array", alist.contains(me2E));
        testTrace("test 2 passed");
        //
        // test 3
        //
        foundMEBs = mebTester.findMEBByPartialLocation(mebTester.getMediaEntityLocation(me1E).toExternalForm());
        assertTrue("test 3: (location1) Should return at least 1 element", foundMEBs.length >= 1);
        alist = Arrays.asList(foundMEBs);
        assertTrue("test 3: me1 is not in array", alist.contains(me1E));
        testTrace("test 3 passed");
        //
        // test 4
        //
        foundMEBs = mebTester.findMEBByPartialLocation(mebTester.getMediaEntityLocation(me2E).toExternalForm());
        assertTrue("test 4: (location2) Should return at least 1 element", foundMEBs.length >= 1);
        alist = Arrays.asList(foundMEBs);
        assertTrue("test 4: me2 is not in array", alist.contains(me2E));
        testTrace("test 4 passed");
        //
        // test 5
        //
        foundMEBs = mebTester.findMEBByPartialLocation(embTestConfig.getTestClientIPAddress());
        assertTrue("test 5: (testClientIP) Should return at least 2 elements", foundMEBs.length >= 2);
        alist = Arrays.asList(foundMEBs);
        assertTrue("test 5: me1 is not in array", alist.contains(me1E));
        assertTrue("test 5: me2 is not in array", alist.contains(me2E));
        testTrace("test 5 passed");
        //
        // test 6
        //
        foundMEBs = mebTester.findMEBByPartialLocation("//////");
        assertTrue("test 6: (/////) Should return empty array", foundMEBs.length >= 0);
        alist = Arrays.asList(foundMEBs);
        assertFalse("test 6: me1 is not in array", alist.contains(me1E));
        assertFalse("test 6: me2 is not in array", alist.contains(me2E));
        testTrace("test 6 passed");
        //
        // test 7
        //
        foundMEBs = mebTester.findMEBByPartialLocation("");
        assertTrue("test 7: (\"\") Should return at least 2 elements", foundMEBs.length >= 2);
        alist = Arrays.asList(foundMEBs);
        assertTrue("test 7: me1 is not in array", alist.contains(me1E));
        assertTrue("test 7: me2 is not in array", alist.contains(me2E));
        testTrace("test 7 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: findByPrimaryKey(String)
     *       Testcase Number: EMB138
     *
     *       setup: create testInstance of MediaEntityLocalHome
     *       		  create Media entity me1
     *       test procedure:
     *       1.call findByPrimaryKey(null)
     *       	 expected result: NullPointerException
     *
     *       2.call findByPrimaryKey(random String)
     *         expected result: javax.ejb.FinderException
     *
     *       3.call findByPrimaryKey(pk of me1)
     *       	 expected result: me1
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB138() throws CreateException, MediaException, RemoteException, NamingException, FinderException {

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mebTester.findByPKExceptions(null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        // TODO : EMB 138 : JOnAS finbyprimarykey(null) not throw a NPE
        // assertEquals("test 1: Should throw a NullPointerException",
        // MediaEntityLocalTestDriver.NULLPOINTER, exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        try {
            exception = mebTester.findByPKExceptions(EMBStaticHelper.createRandomString(EMBStaticHelper
                    .randomInt(0, 10)));
        } catch (Throwable e) {
            fail("test 2 threw " + e.toString());
        }
        assertEquals("test 2: Should throw a FinderException", MediaEntityLocalTestDriver.FINDER, exception);
        testTrace("test 2 passed");
        //
        // test 3
        //
        String testMedia = mebTester.createMediaEntityBean();
        assertEquals("test 3: incorrect media entity found", testMedia, mebTester.findMEBByPrimaryKey(testMedia));
        testTrace("test 3 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: importMedia(URL[], String[])
     *       Testcase Number: EMB139
     *
     *       setup: create Media entity me1 from embedded media file
     *       		  create Media entity me2 from nonembedded media file
     *       		  location1 = me1.getLocation()
     *       		  location2 = me2.getLocation()
     *
     *       test procedure:
     *       1.call importMedia(null, valid names)
     *      	 expected result: NullPointerException
     *
     *       2.call importMedia(new URL[]{location1, null}, valid names)
     *       	 expected result: 2 element array with second being null
     *
     *       3.call importMedia(new URL[]{location1, location2}, valid names)
     *       	 expected result: new entities created
     *       	 verify children of me2 also created
     *
     *       4.call importMedia(new URL[]{location1, location1}, valid names)
     *         expected result: 2 element array with both being same entity
     *
     *       5.call importMedia(new URL[0], new String[0])
     *         expected result: empty array
     *
     *       6.call importMedia(new URL[]{location1}, null)
     *         expected result: no exception, verify getName is not null
     *
     *       7.create Media entity me3 from corrupted bmp file
     *       	 call importMedia(new URL[]{me3.getLocation}, valid name)
     *       	 expected result: MediaFormatException
     *
     *       8.create URL pointing to nonexistent file
     *         call importMedia(new URL[]{urlToNonexistentFile}, valid name)
     *         expected result: ContentAccessException
     *
     *       9.call importMedia(new URL[]{location1}, invalid name)
     *         expected result: IllegalArgumentException
     *
     *       10.call importMedia(new URL[] {location1}, new String[] {name1, name2})
     *         expected result: IllegalArgumentException
     *
     *       11.call importMedia with URL pointing to file larger than maxMediaSize
     *          expected result: ContentTooLargeException
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB139() throws MediaException, CreateException, IOException, RemoteException, NamingException,
            FinderException, RemoveException {

        mebTester.bindMediaFormat("jpg", new JpegFormat());
        mebTester.bindMediaFormat("gpl", new GenericPlaylistFormat());
        mebTester.bindMediaFormat("test", new GenericMediaFormat());

        EmbeddedMediaLocationInfo me1ELocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName1());
        ComplexMediaLocationInfo me2NELocInfo = new ComplexMediaLocationInfo(ComplexMediaLocationInfo.MEDIAGPL1);

        String me1E = mebTester.createMediaEntityBean();
        String me2NE = mebTester.createMediaEntityBean();

        URL location1 = me1ELocInfo.getMediaURL();
        URL location2 = me2NELocInfo.getRootURL();

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mebTester.importHomeMediaExceptions(null, new String[] {"test.jpg"});
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MediaEntityLocalTestDriver.NULLPOINTER, exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        String[] importedMedia = mebTester.importHomeMediaEntityBeans(new URL[] {location1, null}, new String[] {
                "testPicture.jpg", "nullFile.jpg"});
        assertEquals("test 2: does not contain 2 elements", importedMedia.length, 2);
        assertTrue("test 2: media content does not match", java.util.Arrays.equals(mebTester
                .getMediaEntityContent(importedMedia[0]), getByteArrayFromFile(me1ELocInfo.getMediaFile())));
        assertTrue("test 2: format does not match",
                mebTester.getMediaEntityFormat(importedMedia[0]) instanceof JpegFormat);
        assertNull("test 2: second media entity is not null", importedMedia[1]);
        testTrace("test 2 passed");

        //
        // test 3
        //
        importedMedia = mebTester.importHomeMediaEntityBeans(new URL[] {location1, location2}, new String[] {
                "testPicture.jpg", "testPlaylist.gpl"});
        assertEquals("test 3: does not contain 2 elements", importedMedia.length, 2);
        assertTrue("test 3: embedded media does not match", java.util.Arrays.equals(mebTester
                .getMediaEntityContent(importedMedia[0]), getByteArrayFromFile(me1ELocInfo.getMediaFile())));
        assertTrue("test 3: format does not match",
                mebTester.getMediaEntityFormat(importedMedia[0]) instanceof JpegFormat);
        assertTrue("test 3: nonembedded media format is incorrect",
                mebTester.getMediaEntityFormat(importedMedia[1]) instanceof GenericPlaylistFormat);
        String[] importedChildren = mebTester.getMediaEntityChildren(importedMedia[1]);
        assertEquals("test 3: playlist does not have 3 children", 3, importedChildren.length);
        for (int i = 0; i < importedChildren.length; i++) {
            assertEquals("test 3: parent is not testPlaylist", mebTester.getMediaEntityParents(importedChildren[i])[0],
                    importedMedia[1]);
        }
        testTrace("test 3 passed");

        //
        // test 4
        //
        importedMedia = mebTester.importHomeMediaEntityBeans(new URL[] {location1, location1}, new String[] {
                "testPicture1.jpg", "testPicture2.jpg"});
        assertEquals("test 4: does not contain 2 elements", 2, importedMedia.length);
        assertEquals("test 4: not same media entity", importedMedia[0], importedMedia[1]);
        testTrace("test 4 passed");

        //
        // test 5
        //
        importedMedia = mebTester.importHomeMediaEntityBeans(new URL[0], new String[0]);
        assertEquals("test 5: not empty array", 0, importedMedia.length);
        testTrace("test 5 passed");

        //
        // test 6
        //
        importedMedia = mebTester.importHomeMediaEntityBeans(new URL[] {location1}, null);
        assertEquals("test 6: does not contain 1 element", 1, importedMedia.length);
        assertNotNull("test 6: name is null", mebTester.getMediaEntityName(importedMedia[0]));
        testTrace("test 6 passed");

        //
        // test 7
        //
        String mediaJpgFullPath = embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar
                + "testMedia.jpg";
        FileOutputStream mediaJpgOutputStream = new FileOutputStream(mediaJpgFullPath);
        mediaJpgOutputStream.write(EMBStaticHelper.createRandomByteArray(1024));
        mediaJpgOutputStream.close();

        try {
            // remove c:
            // exception = mebTester.importHomeMediaExceptions(new URL[] {new
            // URL("file", embTestConfig.getTestClientIPAddress(), "/C:" +
            // mediaJpgFullPath)}, new String[] {"test.jpg"});
            exception = mebTester.importHomeMediaExceptions(new URL[] {new URL("file", embTestConfig
                    .getTestClientIPAddress(), mediaJpgFullPath)}, new String[] {"test.jpg"});
        } catch (Throwable e) {
            fail("test 7 threw " + e.toString());
        }
        assertEquals("test 7: Should throw a MediaFormatException", MediaEntityLocalTestDriver.MEDIAFORMAT, exception);
        testTrace("test 7 passed");

        //
        // test 8
        //
        EmbeddedMediaLocationInfo me4ELocInfo = new EmbeddedMediaLocationInfo("nonexistentFile.jpg");

        try {
            exception = mebTester.importHomeMediaExceptions(new URL[] {me4ELocInfo.getMediaURL()},
                    new String[] {"nonexistentFile.jpg"});
        } catch (Throwable e) {
            fail("test 8 threw " + e.toString());
        }
        assertEquals("test 8: Should throw a ContentAccessException", MediaEntityLocalTestDriver.CONTENTACCESS,
                exception);
        testTrace("test 8 passed");
        //
        // test 9
        //
        try {
            exception = mebTester.importHomeMediaExceptions(new URL[] {me1ELocInfo.getMediaURL()},
                    new String[] {"?test?.jpg"});
        } catch (Throwable e) {
            fail("test 9 threw " + e.toString());
        }
        assertEquals("test 9: Should throw an IllegalArgumentException", MediaEntityLocalTestDriver.ILLEGALARGUMENT,
                exception);
        testTrace("test 9 passed");
        //
        // test 10
        //
        try {
            exception = mebTester.importHomeMediaExceptions(new URL[] {me1ELocInfo.getMediaURL()}, new String[] {
                    "testName1.jpg", "testName2.jpg"});
        } catch (Throwable e) {
            fail("test 10 threw " + e.toString());
        }
        assertEquals("test 10: Should throw an IllegalArgumentException", MediaEntityLocalTestDriver.ILLEGALARGUMENT,
                exception);
        testTrace("test 10 passed");
        //
        // test 11
        //
        if (embTestConfig.getIncludeLongRunningTest()) {
            String largeFilePath = embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar
                    + "largeFile.test";
            File largeFile = new File(largeFilePath);
            largeFile.createNewFile();
            FileOutputStream largeFileOutputStream = new FileOutputStream(largeFile);
            // create byte array 1 byte larger than maxMediaSize
            byte[] testArray = EMBStaticHelper
                    .createRandomByteArray(Integer.parseInt(embTestConfig.getMaxMediaSize()) + 1);
            largeFileOutputStream.write(testArray);
            largeFileOutputStream.close();
            URL location = new URL("file", embTestConfig.getTestClientIPAddress(), largeFilePath);
            try {
                exception = mebTester.importHomeMediaExceptions(new URL[] {location},
                        new String[] {"largeFileMeda.test"});
            } catch (Throwable e) {
                fail("test 11 threw " + e.toString());
            }
            assertEquals("test 11: Should throw a ContentTooLargeException",
                    MediaEntityLocalTestDriver.CONTENTTOOLARGE, exception);
            testTrace("test 11 passed");
            largeFile.delete();
        }

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: publishContent(Media, String, ProtocolConstraints)
     *       Testcase Number: EMB140
     *
     *       setup: create Media object from embedded media file
     *       		  create Media entity from embedded media file
     *       	      create constraints with &quot;SERVER_TYPE&quot; set to array of Strings
     *
     *       test procedure:
     *       1.a.call publishContent(null, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *       	   expected result: if web server present, NullPointerException
     *          					else NoServerFoundException
     *         b.call publishContent(null, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *       	   expected result: if stream server present, NullPointerException
     *       						else NoServerFoundException
     *
     *       2.call publishContent(mediaObject, invalidTransferType, constraints)
     *         expected result: IllegalArgumentException
     *
     *       3.a.call publishContent(mediaObject, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, URL pointing to media object on server
     *       						else NoServerFoundException
     *         b.call publishContent(mediaObject, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *           expected result: if stream server present, URL pointing to media object on server
     *       					    else NoServerFoundException
     *
     *       4.a.call publishContent(mediaObject, &quot;TRANSFER_TYPE_BURST&quot;, null)
     *           expected result: if web server present, URL pointing to media object on server
     *       						else NoServerFoundException
     *         b.call publishContent(mediaObject, &quot;TRANSFER_TYPE_STREAM&quot;, null)
     *           expected result: if stream server present, URL pointing to media object on server
     *       					    else NoServerFoundException
     *
     *       5.create Media entity from nonexistent file
     *         a.call publishContent(nonexistentMediaEntity, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, ContentAccessException
     *       						else NoServerFoundException
     *         b.call publishContent(nonexistentMediaEntity, &quot;TRANSFER_TYPE_STREAM&quot;, constraints
     *           expected result: if stream server present, ContentAccessException
     *       					    else NoServerFoundException
     *
     *       6.call publishContent(mediaEntity, invalidTransferType, constraints)
     *         expected result: IllegalArgumentException
     *
     *       7.a.call publishContent(mediaEntity, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, URL pointing to media object on server
     *       						else NoServerFoundException
     *         b.call publishContent(mediaEntity, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *           expected result: if stream server present, URL pointing to media object on server
     *       					    else NoServerFoundException
     *
     *       8.a.call publishContent(mediaEntity, &quot;TRANSFER_TYPE_BURST&quot;, null)
     *           expected result: if web server present, URL pointing to media object on server
     *       						else NoServerFoundException
     *         b.call publishContent(mediaEntity, &quot;TRANSFER_TYPE_STREAM&quot;, null)
     *           expected result: if stream server present, URL pointing to media object on server
     *       					    else NoServerFoundException
     *
     *       9.a.call publishContent(nonembeddedMediaBean, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, ContentAccessException
     *       						else NoServerFoundException
     *         b.call publishContent(nonembeddedMediaBean, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *           expected result: if web server present, ContentAccessException
     *       						else NoServerFoundException
     *
     *       10.a.call publishContent(nonembeddedMediaEntityBean, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *            expected result: if web server present, ContentAccessException
     *       						 else NoServerFoundException
     *          b.call publishContent(nonembeddedMediaEntityBean, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *            expected result: if web server present, ContentAccessException
     *       						 else NoServerFoundException
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB140() throws MediaException, NamingException, IOException, CreateException, FinderException {

        mebTester.bindMediaFormat("jpg", new JpegFormat());
        mebTester.bindMediaFormat("test", new GenericMediaFormat());
        mebTester.bindMediaFormat("gpl", new GenericPlaylistFormat());

        String mediaJpgFullPath = embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getJpgPictureName1();

        MediaBean mediaBean = new MediaBean(new File(mediaJpgFullPath), "image/jpg");
        String mediaEntity = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(mediaEntity, "test.jpg");
        mebTester.setMediaEntityContent(mediaEntity, new File(mediaJpgFullPath));

        String streamServerTypes = embTestConfig.getStreamServerTypes();
        StringTokenizer st = new StringTokenizer(streamServerTypes, ";");
        String[] serverTypesArray = new String[st.countTokens()];
        for (int i = 0; i < st.countTokens(); i++) {
            serverTypesArray[i] = st.nextToken();
        }

        ProtocolConstraints constraints = new ProtocolConstraints();
        constraints.setConstraint("SERVER_TYPE", serverTypesArray);

        byte burstTransfer = MediaEntityLocalHome.TRANSFER_TYPE_BURST;
        byte streamTransfer = MediaEntityLocalHome.TRANSFER_TYPE_STREAM;
        URL mediaLoc = null;
        int exception = -1;
        //
        // test 1a
        //
        MediaBean nullMediaBean = null;
        try {
            exception = mebTester.publishContentExceptions(nullMediaBean, burstTransfer, constraints);
        } catch (Throwable e) {
            fail("test 1a threw " + e.toString());
        }
        assertTrue("test 1a: Should throw a NullPointerException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.NULLPOINTER)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 1a passed");
        //
        // test 1b
        //
        try {
            exception = mebTester.publishContentExceptions(nullMediaBean, streamTransfer, constraints);
        } catch (Throwable e) {
            fail("test 1b threw " + e.toString());
        }
        assertTrue("test 1b: Should throw a NullPointerException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.NULLPOINTER)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 1b passed");
        //
        // test 2
        //
        byte invalidTransferType = -1;
        try {
            exception = mebTester.publishContentExceptions(mediaBean, invalidTransferType, constraints);
        } catch (Throwable e) {
            fail("test 2 threw " + e.toString());
        }
        assertTrue("test 2: Should throw an IllegalArgumentException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.ILLEGALARGUMENT)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 2 passed");
        //
        // test 3a
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaBean, burstTransfer, constraints);
            System.out.println("media location: " + mediaLoc.toExternalForm());
            assertNotNull("test 3a: media location is null", mediaLoc);
            testTrace("test 3a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 3a skipped: no server found");
        } catch (Exception e) {
            fail("test 3a threw " + e.toString());
        }
        //
        // test 3b
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaBean, streamTransfer, constraints);
            System.out.println("media location: " + mediaLoc);
            assertNotNull("test 3b: media location is null", mediaLoc);
            testTrace("test 3b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 3b skipped: no server found");
        } catch (Exception e) {
            fail("test 3b threw " + e.toString());
        }
        //
        // test 4a
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaBean, burstTransfer, null);
            System.out.println("media location: " + mediaLoc.toExternalForm());
            assertNotNull("test 4a: media location is null", mediaLoc);
            testTrace("test 4a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 4a skipped: no server found");
        } catch (Exception e) {
            fail("test 4a threw " + e.toString());
        }
        //
        // test 4b
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaBean, streamTransfer, null);
            System.out.println("media location: " + mediaLoc.toExternalForm());
            assertNotNull("test 4b: media location is null", mediaLoc);
            testTrace("test 4b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 4b skipped: no server found");
        } catch (Exception e) {
            fail("test 4b threw " + e.toString());
        }
        //
        // test 5a
        //
        String tmpMediaEntity = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(tmpMediaEntity, "testFile.test");
        try {
            exception = mebTester.publishContentExceptions(tmpMediaEntity, burstTransfer, constraints);
        } catch (Throwable e) {
            fail("test 5a threw " + e.toString());
        }
        assertTrue("test 5a: Should throw a ContentAccessException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.CONTENTACCESS)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 5a passed");
        //
        // test 5b
        //
        try {
            exception = mebTester.publishContentExceptions(tmpMediaEntity, streamTransfer, constraints);
        } catch (Throwable e) {
            fail("test 5b threw " + e.toString());
        }
        assertTrue("test 5b: Should throw a ContentAccessException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.CONTENTACCESS)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 5b passed");
        //
        // test 6
        //
        try {
            exception = mebTester.publishContentExceptions(mediaEntity, invalidTransferType, constraints);
        } catch (Throwable e) {
            fail("test 6 threw " + e.toString());
        }
        assertTrue("test 6: Should throw a IllegalArgumentException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.ILLEGALARGUMENT)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 6 passed");
        //
        // test 7a
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaEntity, burstTransfer, constraints);
            assertNotNull("test 7a: media location is null", mediaLoc);
            testTrace("test 7a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 7a skipped: no server found");
        } catch (Exception e) {
            fail("test 7a threw " + e.toString());
        }
        //
        // test 7b
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaEntity, streamTransfer, constraints);
            assertNotNull("test 7b: media location is null", mediaLoc);
            testTrace("test 7b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 7b skipped: no server found");
        } catch (Exception e) {
            fail("test 7b threw " + e.toString());
        }
        //
        // test 8a
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaEntity, burstTransfer, null);
            assertNotNull("test 8a: media location is null", mediaLoc);
            testTrace("test 8a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 8a skipped: no server found");
        } catch (Exception e) {
            fail("test 8a threw " + e.toString());
        }
        //
        // test 8b
        //
        try {
            mediaLoc = mebTester.publishMediaContent(mediaEntity, streamTransfer, null);
            assertNotNull("test 8b: media location is null", mediaLoc);
            testTrace("test 8b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 8b skipped: no server found");
        } catch (Exception e) {
            fail("test 8b threw " + e.toString());
        }
        //
        // test 9a
        //
        ComplexMediaLocationInfo me1NE = new ComplexMediaLocationInfo(ComplexMediaLocationInfo.MEDIAGPL1);
        MediaBean nonEmbeddedMedia = new MediaBean(me1NE.getRootFile(), "test");
        try {
            exception = mebTester.publishContentExceptions(nonEmbeddedMedia, burstTransfer, constraints);
        } catch (Throwable e) {
            fail("test 9a threw " + e.toString());
        }
        assertTrue("test 9a: Should throw a ContentAccessException",
                MediaEntityLocalTestDriver.CONTENTACCESS == exception
                        || MediaEntityLocalTestDriver.NOSERVERFOUND == exception);
        testTrace("test 9a passed");
        //
        // test 9b
        //
        try {
            exception = mebTester.publishContentExceptions(nonEmbeddedMedia, streamTransfer, constraints);
        } catch (Throwable e) {
            fail("test 9b threw " + e.toString());
        }
        assertTrue("test 9b: Should throw a ContentAccessException",
                MediaEntityLocalTestDriver.CONTENTACCESS == exception
                        || MediaEntityLocalTestDriver.NOSERVERFOUND == exception);
        testTrace("test 9b passed");

        //
        // test 10a
        //
        String nonEmbeddedMediaEntity = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(nonEmbeddedMediaEntity, "testPlaylist.gpl");
        mebTester.importMediaEntityContent(nonEmbeddedMediaEntity, me1NE.getRootURL(), null);
        try {
            mebTester.publishMediaContent(nonEmbeddedMediaEntity, burstTransfer, constraints);
            testTrace("test 10a passed");
        } catch (Throwable e) {
            fail("test 10a threw " + e.toString());
        }
        try {
            mebTester.publishMediaContent(nonEmbeddedMediaEntity, streamTransfer, constraints);
            testTrace("test 10b passed");
        } catch (Throwable e) {
            fail("test 10b threw " + e.toString());
        }

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *
     *
     *       Testcase Name: publishMedia(MediaEntityLocal[], String, ProtocolConstraints)
     *       Testcase Number: EMB141
     *
     *       setup: create Media entity from embedded media file
     *       		  create constraints with &quot;SERVER_TYPE&quot; set to array of Strings
     *
     *       test procedure:
     *       1.a.call publishMedia(null, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *       	   expected result: if web server present, NullPointerException
     *       						else NoServerFoundException
     *         b.call publishMedia(null, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *           expected result: if stream server present, NullPointerException
     *       						else NoServerFoundException
     *
     *       2.call publishMedia(new MediaEntityLocal[]{mediaEntity}, invalidTransferType, constraints)
     *         expected result: IllegalArgumentException
     *
     *       3.a.call publishMedia(new MediaEntityLocal[]{mediaEntity}, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, media object that is not null
     *       						else NoServerFoundException
     *       	 b.call publishMedia(new MediaEntityLocal[]{mediaEntity}. &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if stream server present, media object that is not null
     *       						else NoServerFoundException
     *
     *       4.a.call publishMedia(new MediaEntityLocal[]{mediaEntity}, &quot;TRANSFER_TYPE_BURST&quot;, null)
     *           expected result: if web server present, media object that is not null
     *       						else NoServerFoundException
     *       	 b.call publishMedia(new MediaEntityLocal[]{mediaEntity}. &quot;TRANSFER_TYPE_BURST&quot;, null)
     *           expected result: if stream server present, media object that is not null
     *       						else NoServerFoundException
     *
     *       5.create Media entity from random content file
     *         a.call publishMedia(new MediaEntityLocal[]{mediaEntity}, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, MediaFormatException
     *       						else NoServerFoundException
     *       	 b.call publishMedia(new MediaEntityLocal[]{mediaEntity}. &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if stream server present, MediaFormatException
     *       						else NoServerFoundException
     *
     *       6.create Media entity from nonexistent file
     *         a.call publishMedia(new MediaEntityLocal[]{mediaEntity}, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, ContentAccessException
     *       						else NoServerFoundException
     *       	 b.call publishMedia(new MediaEntityLocal[]{mediaEntity}. &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if stream server present, ContentAccessException
     *       						else NoServerFoundException
     *
     *       7.a.call publishMedia(new MediaEntityLocal[0], &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, no exception (check media obj not null)
     *       						else NoServerFoundException
     *         b.call publishMedia(new MediaEntityLocal[0], &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *       	   expected result: if web server present, no exception (check media obj not null)
     *       						else NoServerFoundException
     *
     *       8.a.call publishMedia(new MediaEntityLocal[]{mediaEntity, mediaEntity}, &quot;TRANSFER_TYPE_BURST&quot;, constraints)
     *           expected result: if web server present, media object not null
     *       						else NoServerFoundException
     *       	 b.call publishMedia(new MediaEntityLocal[]{mediaEntity, mediaEntity}, &quot;TRANSFER_TYPE_STREAM&quot;, constraints)
     *           expected result: if web server present, media object not null
     *       						else NoServerFoundException
     *
     *
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB141() throws MediaException, NamingException, IOException, CreateException, FinderException {

        mebTester.bindMediaFormat("jpg", new JpegFormat());
        mebTester.bindMediaFormat("test", new GenericMediaFormat());

        EmbeddedMediaLocationInfo me1LocInfo = new EmbeddedMediaLocationInfo(embTestConfig.getJpgPictureName1());
        String me1 = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me1, me1LocInfo.getMediaName());
        mebTester.setMediaEntityContent(me1, me1LocInfo.getMediaFile());

        String streamServerTypes = embTestConfig.getStreamServerTypes();
        StringTokenizer st = new StringTokenizer(streamServerTypes, ";");
        String[] serverTypesArray = new String[st.countTokens()];
        for (int i = 0; i < st.countTokens(); i++) {
            serverTypesArray[i] = st.nextToken();
        }

        ProtocolConstraints constraints = new ProtocolConstraints();
        constraints.setConstraint("SERVER_TYPE", serverTypesArray);
        byte burstTransfer = MediaEntityLocalHome.TRANSFER_TYPE_BURST;
        byte streamTransfer = MediaEntityLocalHome.TRANSFER_TYPE_STREAM;
        Media mediaObject = null;

        int exception = -1;
        //
        // test 1a
        //
        try {
            exception = mebTester.publishMediaExceptions(null, burstTransfer, constraints);
        } catch (Throwable e) {
            fail("test 1a threw " + e.toString());
        }
        assertTrue("test 1a: Should throw a NullPointerException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.NULLPOINTER)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 1a passed");
        //
        // test 1b
        //
        try {
            exception = mebTester.publishMediaExceptions(null, streamTransfer, constraints);
        } catch (Throwable e) {
            fail("test 1b threw " + e.toString());
        }
        assertTrue("test 1b: Should throw a NullPointerException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.NULLPOINTER)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 1b passed");
        //
        // test 2
        //
        byte invalidTransferType = -1;
        try {
            exception = mebTester.publishMediaExceptions(new String[] {me1}, invalidTransferType, constraints);
        } catch (Throwable e) {
            fail("test 2 threw " + e.toString());
        }
        assertTrue("test 2: Should throw an IllegalArgumentException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.ILLEGALARGUMENT)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 2 passed");
        //
        // test 3a
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[] {me1}, burstTransfer, constraints);
            assertNotNull("test 3a: media object null", mediaObject);
            testTrace("test 3a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 3a skipped: no server found");
        } catch (Exception e) {
            fail("test 3a threw " + e.toString());
        }
        //
        // test 3b
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[] {me1}, streamTransfer, constraints);
            assertNotNull("test 3b: media object null", mediaObject);
            testTrace("test 3b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 3b skipped: no server found");
        } catch (Exception e) {
            fail("test 3b threw " + e.toString());
        }
        //
        // test 4a
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[] {me1}, burstTransfer, null);
            assertNotNull("test 4a: media object null", mediaObject);
            testTrace("test 4a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 4a skipped: no server found");
        } catch (Exception e) {
            fail("test 4a threw " + e.toString());
        }
        //
        // test 4b
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[] {me1}, streamTransfer, null);
            assertNotNull("test 4b: media object null", mediaObject);
            testTrace("test 4b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 4b skipped: no server found");
        } catch (Exception e) {
            fail("test 4b threw " + e.toString());
        }
        //
        // test 5a
        //
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated"
                + File.separatorChar + "testMedia.test");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();

        String me2 = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me2, "test.test");
        mebTester.setMediaEntityContent(me2, file1024Byte);
        mebTester.setMediaEntityName(me2, "test.gpl");

        try {
            exception = mebTester.publishMediaExceptions(new String[] {me2}, burstTransfer, constraints);
        } catch (Throwable e) {
            fail("test 5a threw " + e.toString());
        }
        assertTrue("test 5a: Should throw a MediaFormatException or NoServerFoundException",
                (exception == MediaEntityLocalTestDriver.MEDIAFORMAT)
                        || (exception == MediaEntityLocalTestDriver.NOSERVERFOUND));
        testTrace("test 5a passed");
        //
        // test 5b
        //
        try {
            exception = mebTester.publishMediaExceptions(new String[] {me2}, streamTransfer, constraints);
        } catch (Throwable e) {
            fail("test 5b threw " + e.toString());
        }
        assertTrue("test 5b: Should throw a MediaFormatException or NoServerFoundException",
                exception == MediaEntityLocalTestDriver.MEDIAFORMAT
                        || exception == MediaEntityLocalTestDriver.NOSERVERFOUND);
        testTrace("test 5b passed");
        //
        // test 6a
        //
        String me3 = mebTester.createMediaEntityBean();
        mebTester.setMediaEntityName(me3, "testFile.test");
        try {
            exception = mebTester.publishMediaExceptions(new String[] {me3}, burstTransfer, constraints);
        } catch (Throwable e) {
            fail("test 6a threw " + e.toString());
        }
        assertTrue("test 6a: Should throw a ContentAccessException or NoServerFoundException",
                exception == MediaEntityLocalTestDriver.CONTENTACCESS
                        || exception == MediaEntityLocalTestDriver.NOSERVERFOUND);
        testTrace("test 6a passed");
        //
        // test 6b
        //
        try {
            exception = mebTester.publishMediaExceptions(new String[] {me3}, streamTransfer, constraints);
        } catch (Throwable e) {
            fail("test 6b threw " + e.toString());
        }
        assertTrue("test 6b: Should throw a ContentAccessException or NoServerFoundException",
                exception == MediaEntityLocalTestDriver.CONTENTACCESS
                        || exception == MediaEntityLocalTestDriver.NOSERVERFOUND);
        testTrace("test 6b passed");
        //
        // test 7a
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[0], burstTransfer, constraints);
            assertNotNull("test 7a: media object null", mediaObject);
            testTrace("test 7a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 7a skipped: no server found");
        } catch (Exception e) {
            fail("test 7a threw " + e.toString());
        }
        //
        // test 7b
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[0], streamTransfer, constraints);
            assertNotNull("test 7b: media object null", mediaObject);
            testTrace("test 7b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 7b skipped: no server found");
        } catch (Exception e) {
            fail("test 7b threw " + e.toString());
        }
        //
        // test 8a
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[] {me1, me1}, burstTransfer, constraints);
            assertNotNull("test 8a: media object null", mediaObject);
            testTrace("test 8a passed");
        } catch (NoServerFoundException e) {
            testTrace("test 8a skipped: no server found");
        } catch (Exception e) {
            fail("test 8a threw " + e.toString());
        }
        //
        // test 8b
        //
        try {
            mediaObject = mebTester.publishMediaEntityBeans(new String[] {me1, me1}, streamTransfer, constraints);
            assertNotNull("test 8b: media object null", mediaObject);
            testTrace("test 8b passed");
        } catch (NoServerFoundException e) {
            testTrace("test 8b skipped: no server found");
        } catch (Exception e) {
            fail("test 8b threw " + e.toString());
        }

        succeed();
    }
}
