package com.ibm.emb.test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Date;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xerces.parsers.DOMParser;
import org.objectweb.jonas.emb.mfb.formats.image.BmpFormat;
import org.objectweb.jonas.emb.mfb.formats.image.JpegFormat;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.ibm.emb.junit.EMBStaticHelper;
import com.ibm.emb.junit.EMBTestCaseBase;
import com.ibm.emb.meb.test.ejb.MetaDataEntityLocalTestDriver;
import com.ibm.emb.meb.test.ejb.MetaDataEntityLocalTestDriverHome;

/**
 * <pre>
 *
 *  Line Item: MEB API
 *  Subcategory 1: javax.emb
 *  Subcategory 2: MetaDataEntityLocal
 *
 * </pre>
 */

public class MetaDataEntityLocalTest extends EMBTestCaseBase {

    /**
     * Constructor for MetaDataEntityLocalTest.
     * @param name
     */
    public MetaDataEntityLocalTest(String name) throws MediaException {
        super(name);
    }

    private static String MDEBTESTER_JNDI = "java:comp/env/ejb/MetaDataEntityLocalTestDriverHome";

    private static String TXUSER_JNDI = "jta/usertransaction";

    private InitialContext ic;

    private MetaDataEntityLocalTestDriverHome mdebHome;

    private MetaDataEntityLocalTestDriver mdebTester = null;

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

    protected void tearDown() throws IOException {

    }

    /**
     * recursive function which compares two XML elements
     */
    protected static boolean xmlCompare(Element expected, Element actual) {
        boolean match = false;

        // check if text matches
        if ((expected.getFirstChild() != null) && (expected.getFirstChild().getNodeType() == Node.TEXT_NODE)) {
            if ((actual.getFirstChild() != null) && (actual.getFirstChild().getNodeType() == Node.TEXT_NODE)) {
                if (expected.getFirstChild().getNodeValue().trim().equals(actual.getFirstChild().getNodeValue().trim())) {
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        // check attributes for node
        if (expected.hasAttributes()) {
            boolean allAttrsFound = true;
            NamedNodeMap attrs = expected.getAttributes();
            for (int k = 0; k < attrs.getLength(); k++) {
                Attr currentAttr = (Attr) attrs.item(k);
                if (actual.hasAttribute(currentAttr.getName())) {
                    if (currentAttr.getValue().equals(actual.getAttributeNode(currentAttr.getName()).getValue())) {
                    } else {
                        return false;
                    }
                } else {
                    // actual does not have this attribute
                    return false;
                }
            }
        }

        // retrieve all child elements
        NodeList expectedChildren = expected.getChildNodes();
        NodeList actualChildren = actual.getChildNodes();
        if (expectedChildren.getLength() != actualChildren.getLength()) {
            return false;
        }

        // for each child, find matching node
        for (int i = 0; i < expectedChildren.getLength(); i++) {
            if (expectedChildren.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) expectedChildren.item(i);
                NodeList matchingElements = actual.getElementsByTagName(currentElement.getTagName());
                for (int j = 0; j < matchingElements.getLength(); j++) {
                    match = xmlCompare(currentElement, (Element) matchingElements.item(j));
                    if (match) {
                        break;
                    }
                }
                if (!match) {
                    return false;
                }
            }
        }
        return true;
    }

    private String retrieveXML(String fileName) throws IOException {
        File xmlFile = new File(embTestConfig.getMediaDir() + File.separatorChar + fileName);
        FileReader xmlFileReader = new FileReader(xmlFile);
        BufferedReader xmlBufferedReader = new BufferedReader(xmlFileReader);
        String temp = xmlBufferedReader.readLine();
        String xmlContent = "";
        while (temp != null) {
            xmlContent = xmlContent + temp;
            temp = xmlBufferedReader.readLine();
        }
        xmlBufferedReader.close();
        xmlFileReader.close();

        return xmlContent;
    }

    /**
     * <pre>
     *
     *  Testcase Name: addChild(MetaDataEntityLocal)
     *  Testcase Number: EMB148
     *
     *  setup: create meta data entities md1, md2, and md3
     *
     *  test procedure:
     *  1.call md1.addChild(null)
     *    expected result: NullPointerException
     *
     *  2.call md1.addChild(md3)
     *    expected result: no exception
     *    call md1.getChildren() and verify 1 element array containing md3
     *    call md3.getParents() and verify 1 element array containing md1
     *
     *  3.call md2.addChild(md3)
     *    expected result: no exception
     *    call md2.getChildren() and verify 1 element array containing md3
     *    call md1.getChildren() and check that md3 is still in array
     *  	 call md3.getParents() and check that md1 and md2 are listed
     *
     * </pre>
     */
    public void testEMB148() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        String md3 = mdebTester.createMetaDataEntityBean();

        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md3, xmlContent, false);

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mdebTester.addChildExceptions(md1, null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.addMetaDataChild(md1, md3);
        String[] mdChildren = mdebTester.getMetaDataEntityChildren(md1);
        String[] mdParents = mdebTester.getMetaDataEntityParents(md3);

        assertEquals("test 2: mdChildren does not contain 1 element", 1, mdChildren.length);
        assertEquals("test 2: child is not md3", mdChildren[0], md3);
        assertEquals("test 2: mdParents does not contain 1 element", 1, mdParents.length);
        assertEquals("test 2: parent is not md1", mdParents[0], md1);
        testTrace("test 2 passed");
        //
        // test 3
        //
        mdebTester.addMetaDataChild(md2, md3);
        mdChildren = mdebTester.getMetaDataEntityChildren(md1);
        assertEquals("test 3: md1.getChildren does not contain 1 element", 1, mdChildren.length);
        assertEquals("test 3: child is not md3", mdChildren[0], md3);

        mdChildren = mdebTester.getMetaDataEntityChildren(md1);
        assertEquals("test 3: md2.getChildren does not contain 1 element", 1, mdChildren.length);
        assertEquals("test 3: child is not md3", mdChildren[0], md3);

        mdParents = mdebTester.getMetaDataEntityParents(md3);
        if ((mdParents[0].equals(md1) && mdParents[1].equals(md2))
                || (mdParents[0].equals(md2) && mdParents[1].equals(md1))) {
            testTrace("test 3 passed");
        } else {
            fail("test 3: parents are incorrect");
        }

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMDEBByPK(md3);
        succeed();

    }

    /**
     * <pre>
     *
     *  Testcase Name: addMediaEntity(MediaEntityLocal)
     *  Testcase Number: EMB149
     *
     *  setup: create Media entities me1 and me2
     *  		  create meta data entities md1 and md2
     *
     *  test procedure:
     *  1.call md1.addMediaEntity(null)
     *    expected result: NullPointerException
     *
     *  2.call md1.addMediaEntity(me1)
     *    expected result: no exception
     *    call md1.getMediaEntities() and verify me1 is in array
     *
     *  3.call md1.addMediaEntity(me2)
     *    expected result: no exception
     *    call md1.getMediaEntities() and verify two elements in array
     *
     *  4.call md2.addMediaEntity(me1)
     *    expected result: no exception
     *  	 call md2.getMediaEntities() and verify one element in array
     *    call md1.getMediaEntities() and verify two elements in array
     *
     * </pre>
     */
    public void testEMB149() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);

        String me1 = mdebTester.createMediaEntityBean();
        String me2 = mdebTester.createMediaEntityBean();

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mdebTester.addMediaExceptions(md1, null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.addMetaDataMedia(md1, me1);
        String[] mediaEntities = mdebTester.getMetaDataEntityMediaEntities(md1);
        assertEquals("test 2: md1 has more than one media entity", 1, mediaEntities.length);
        assertEquals("test 2: media entity is not me1", mediaEntities[0], me1);
        testTrace("test 2 passed");
        //
        // test 3
        //
        mdebTester.addMetaDataMedia(md1, me2);
        mediaEntities = mdebTester.getMetaDataEntityMediaEntities(md1);
        assertEquals("test 3: md1 does not have two media entities", 2, mediaEntities.length);
        assertTrue("test 3: media entities do not match",
                (mediaEntities[0].equals(me1) && mediaEntities[1].equals(me2))
                        || (mediaEntities[0].equals(me2) && mediaEntities[1].equals(me1)));
        testTrace("test 3 passed");
        //
        // test 4
        //
        mdebTester.addMetaDataMedia(md2, me1);
        mediaEntities = mdebTester.getMetaDataEntityMediaEntities(md1);
        assertEquals("test 4: md1 does not have two media entities", mediaEntities.length, 2);
        assertTrue("test 4: media entities do not match",
                (mediaEntities[0].equals(me1) && mediaEntities[1].equals(me2))
                        || (mediaEntities[0].equals(me2) && mediaEntities[1].equals(me1)));
        mediaEntities = mdebTester.getMetaDataEntityMediaEntities(md2);
        assertEquals("test 4: md2 does not have one media entity", 1, mediaEntities.length);
        assertEquals("test 4: media entity is not me1", mediaEntities[0], me1);
        testTrace("test 4 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMEBByPK(me1);
        mdebTester.removeMEBByPK(me2);
        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getXML()
     *  Testcase Number: EMB159
     *
     *  setup: create meta data entity md1
     *
     *  test procedure:
     *  1.call md1.getXML
     *  	 expected result: no exception and not null
     *  					  verify resulting String matches XML content
     *
     *  2.create meta data entity w/o setting XML content
     *    expected result: ContentAccessException
     *
     * </pre>
     */
    public void testEMB159() throws IOException, FileNotFoundException, SAXException, ParserConfigurationException,
            CreateException, MediaException, RemoteException, NamingException, FinderException, RemoveException {

        File xmlFile = new File(embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getXMLFile1());
        File exportedXMLFile = new File(embTestConfig.getMediaDir() + File.separatorChar + "export" + File.separatorChar + "temp.xml");
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        //
        // test 1
        //
        String retrievedXML = mdebTester.getMetaDataEntityXML(md1);
        assertNotNull("test 1: XML content is null", retrievedXML);
        exportedXMLFile.createNewFile();
        FileWriter xmlFileWriter = new FileWriter(exportedXMLFile);
        xmlFileWriter.write(retrievedXML);
        xmlFileWriter.close();

        DOMParser parser = new DOMParser();
        parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", true);
        InputSource expectedXML = new InputSource(new ByteArrayInputStream(xmlContent.getBytes()));
        InputSource actualXML = new InputSource(new ByteArrayInputStream(retrievedXML.getBytes()));

        parser.parse(expectedXML);
        Document xmlDoc1 = parser.getDocument();
        parser.parse(actualXML);
        Document xmlDoc2 = parser.getDocument();

        Element root1 = xmlDoc1.getDocumentElement();
        Element root2 = xmlDoc2.getDocumentElement();

        assertTrue("test 1: XML content does not match", xmlCompare(root1, root2));
        testTrace("test 1 passed");
        //
        // test 2
        //
        String md2 = mdebTester.createMetaDataEntityBean();
        int exception = -1;
        try {
            exception = mdebTester.getXMLExceptions(md2);
        } catch (Throwable e) {
            fail("test 2 threw " + e.toString());
        }
        assertEquals("test 2: Should throw a ContentAccessException", MetaDataEntityLocalTestDriver.CONTENTACCESS,
                exception);
        testTrace("test 2 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getLastModified()
     *  Testcase Number: EMB151
     *
     *  setup: create meta data entity md1
     *
     *  test procedure:
     *  1.call getLastModified()
     *    expected result: no exception and not null
     *
     *  2.call getLastModified and remember timestamp
     *    wait 2 seconds and call setName(randomString)
     *    call getLastModified
     *    expected result: timestamp updated
     *
     * </pre>
     */
    public void testEMB151() throws InterruptedException, IOException, MediaException, CreateException,
            RemoteException, NamingException, FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        //
        // test 1
        //
        try {
            mdebTester.getMetaDataEntityLastModified(md1);
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1 threw " + e.toString());
        }
        //
        // test 2
        //
        Date beforeStamp = new Date(mdebTester.getMetaDataEntityLastModified(md1));
        mdebTester.setMetaDataEntityName(md1, EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10)));
        Thread.sleep(2 * 1000);
        Date afterStamp = new Date(mdebTester.getMetaDataEntityLastModified(md1));
        assertTrue("test 2: modification date", afterStamp.after(beforeStamp));
        testTrace("test 2 passed");

        mdebTester.removeMDEBByPK(md1);
        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getMediaEntities()
     *  Testcase Number: EMB152
     *
     *  setup: create Media entities me1 and me2
     *  		  create meta data entity md1 and md2
     *
     *  test procedure:
     *  1.call md1.addMediaEntity(me1)
     *    call md1.addMediaEntity(me2)
     *    call md1.getMediaEntities()
     *    expected result: 2 element array containing me1 and me2
     *
     *  2.call md2.getMediaEntities()
     *    expected result: empty array
     *
     * </pre>
     */
    public void testEMB152() throws IOException, CreateException, MediaException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);

        mdebTester.bindMediaFormat("jpg", new JpegFormat());
        String me1 = mdebTester.createMediaSetName("test1.jpg");
        String me2 = mdebTester.createMediaSetName("test2.jpg");
        //
        // test 1
        //
        mdebTester.addMetaDataMedia(md1, me1);
        mdebTester.addMetaDataMedia(md1, me2);
        String[] meArray = mdebTester.getMetaDataEntityMediaEntities(md1);
        assertEquals("test 1: does not contain 2 elements", meArray.length, 2);
        if ((meArray[0].equals(me1) && meArray[1].equals(me2)) || (meArray[0].equals(me2) && meArray[1].equals(me1))) {
            testTrace("test 1 passed");
        } else {
            fail("test 1: media entities are incorrect");
        }
        //
        // test 2
        //
        meArray = mdebTester.getMetaDataEntityMediaEntities(md2);
        assertEquals("test 2: md2 has media entities", meArray.length, 0);
        testTrace("test 2 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMEBByPK(me1);
        mdebTester.removeMEBByPK(me2);
        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getMediaEntities(MediaFormat, boolean)
     *  Testcase Number: EMB153
     *
     *  setup: create Media entities me1 and me2 from jpg files and me3 from bmp file
     *    	  create meta data entities md1 and md2
     *
     *  test procedure:
     *  1.call md1.getMediaEntities(null, false)
     *    expected result: NullPointerException
     *
     *  2.rebind &quot;jpg&quot; and &quot;bmp&quot; to format registry
     *    call md1.addChild(md2)
     *    call md1.addMediaEntity(me1)
     *    call md2.addMediaEntity(me2)
     *  	 call md2.addMediaEntity(me3)
     *  	 call md1.getMediaEntities(new JpegFormat(), false)
     *  	 expected result: 1 element array containing me1
     *
     *  3.call md1.getMediaEntities(new JpegFormat(), true)
     *    expected result: 2 element array
     *
     *  4.call md1.getMediaEntities(new BmpFormat(), false)
     *    expected result: empty array
     *
     *  5.call md1.getMediaEntities(new BmpFormat(), true)
     *    expected result: 1 element array
     *
     * </pre>
     */
    public void testEMB153() throws IOException, CreateException, MediaException, RemoteException, NamingException,
            FinderException, RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);

        mdebTester.bindMediaFormat("jpg", new JpegFormat());
        mdebTester.bindMediaFormat("bmp", new BmpFormat());
        String me1 = mdebTester.createMediaSetName("test1.jpg");
        String me2 = mdebTester.createMediaSetName("test2.jpg");
        String me3 = mdebTester.createMediaSetName("test3.bmp");

        mdebTester.addMetaDataChild(md1, md2);
        mdebTester.addMetaDataMedia(md1, me1);
        mdebTester.addMetaDataMedia(md2, me2);
        mdebTester.addMetaDataMedia(md2, me3);

        int exception = -1;
        //
        // test 1
        //
        MediaFormat nullFormat = null;
        try {
            exception = mdebTester.getMediaByFormatExceptions(md1, nullFormat, false);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        String[] mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, new JpegFormat(), false);
        assertEquals("test 2: does not contain 1 element", 1, mediaEntitiesFound.length);
        assertEquals("test 2: media does not match", mediaEntitiesFound[0], me1);
        testTrace("test 2 passed");
        //
        // test 3
        //
        mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, new JpegFormat(), true);
        assertEquals("test 3: does not contain 1 element", 2, mediaEntitiesFound.length);
        assertTrue("test 3: media entities do not match", (mediaEntitiesFound[0].equals(me1) && mediaEntitiesFound[1]
                .equals(me2))
                || (mediaEntitiesFound[0].equals(me2) && mediaEntitiesFound[1].equals(me1)));
        testTrace("test 3 passed");
        //
        // test 4
        //
        mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, new BmpFormat(), false);
        assertEquals("test 4: is not empty array", 0, mediaEntitiesFound.length);
        testTrace("test 4 passed");
        //
        // test 5
        //
        mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, new BmpFormat(), true);
        assertEquals("test 5: is not empty array", 1, mediaEntitiesFound.length);
        assertEquals("test 3: media does not match", mediaEntitiesFound[0], me3);
        testTrace("test 5 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMEBByPK(me1);
        mdebTester.removeMEBByPK(me2);
        mdebTester.removeMEBByPK(me3);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getMediaEntities(String, boolean)
     *  Testcase Number: EMB154
     *
     *  setup: create Media entities me1 and me2 from jpg files and me3 from bmp file
     *    	  create meta data entities md1 and md2
     *
     *  test procedure:
     *  1.call md1.getMediaEntities(null, false)
     *    expected result: NullPointerException
     *
     *  2.rebind &quot;jpg&quot; and &quot;bmp&quot; to format registry
     *  	 call md1.addChild(md2)
     *  	 call md1.addMediaEntity(me1)
     *    call md2.addMediaEntity(me2)
     *  	 call md2.addMediaEntity(me3)
     *  	 call md1.getMediaEntities(&quot;image/jpeg&quot;, false)
     *    expected result: 1 element array containing me1
     *
     *  3.call md1.getMediaEntities(&quot;image/jpeg&quot;, true)
     *    expected result: 2 element array
     *
     *  4.call md1.getMediaEntities(&quot;image/bmp&quot;, false)
     *    expected result: empty array
     *
     *  5.call md1.getMediaEntities(&quot;image/bmp&quot;, true)
     *    expected result: 1 element array
     *
     * </pre>
     */
    public void testEMB154() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);

        mdebTester.bindMediaFormat("jpg", new JpegFormat());
        mdebTester.bindMediaFormat("bmp", new BmpFormat());
        String me1 = mdebTester.createMediaSetName("test1.jpg");
        String me2 = mdebTester.createMediaSetName("test2.jpg");
        String me3 = mdebTester.createMediaSetName("test3.bmp");

        mdebTester.addMetaDataChild(md1, md2);
        mdebTester.addMetaDataMedia(md1, me1);
        mdebTester.addMetaDataMedia(md2, me2);
        mdebTester.addMetaDataMedia(md2, me3);

        int exception = -1;
        //
        // test 1
        //
        MediaFormat nullFormat = null;
        try {
            exception = mdebTester.getMediaByFormatExceptions(md1, nullFormat, false);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        String[] mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, "image/jpeg", false);
        assertEquals("test 2: does not contain 1 element", 1, mediaEntitiesFound.length);
        assertEquals("test 2: media does not match", mediaEntitiesFound[0], me1);
        testTrace("test 2 passed");
        //
        // test 3
        //
        mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, "image/jpeg", true);
        assertEquals("test 3: does not contain 1 element", 2, mediaEntitiesFound.length);
        assertTrue("test 3: media entities do not match", (mediaEntitiesFound[0].equals(me1) && mediaEntitiesFound[1]
                .equals(me2))
                || (mediaEntitiesFound[0].equals(me2) && mediaEntitiesFound[1].equals(me1)));
        testTrace("test 3 passed");
        //
        // test 4
        //
        mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, "image/bmp", false);
        assertEquals("test 4: is not empty array", 0, mediaEntitiesFound.length);
        testTrace("test 4 passed");
        //
        // test 5
        //
        mediaEntitiesFound = mdebTester.getMetaDataEntityMediaEntities(md1, "image/bmp", true);
        assertEquals("test 5: is not empty array", 1, mediaEntitiesFound.length);
        assertEquals("test 3: media does not match", mediaEntitiesFound[0], me3);
        testTrace("test 5 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMEBByPK(me1);
        mdebTester.removeMEBByPK(me2);
        mdebTester.removeMEBByPK(me3);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getChildren()
     *  Testcase Number: EMB150
     *
     *  setup: create meta data entities md1, md2, and md3
     *
     *  test procedure:
     *  1.call md1.addChild(md2), call md1.addChild(md3)
     *    call md1.getChildren()
     *    expected result: 2 element array containing md2 and md3
     *
     *  2.call md2.getChildren()
     *  	 expected result: empty array
     *
     * </pre>
     */
    public void testEMB150() throws IOException, CreateException, MediaException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        String md3 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md3, xmlContent, false);

        //
        // test 1
        //
        mdebTester.addMetaDataChild(md1, md2);
        mdebTester.addMetaDataChild(md1, md3);
        boolean md2Found = false;
        boolean md3Found = false;
        String[] children = mdebTester.getMetaDataEntityChildren(md1);
        assertEquals("test 1: md1 does not have 2 children", 2, children.length);
        if ((children[0].equals(md2) && children[1].equals(md3))
                || (children[0].equals(md3) && children[1].equals(md2))) {
            testTrace("test 1 passed");
        } else {
            fail("test 1: children are incorrect");
        }
        //
        // test 2
        //
        children = mdebTester.getMetaDataEntityChildren(md2);
        assertEquals("test 2: md2 has children", children.length, 0);
        testTrace("test 2 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMDEBByPK(md3);
        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getName()
     *  Testcase Number: EMB155
     *
     *  setup: create meta data entity testInstance
     *
     *  test procedure:
     *  1.call testInstance.setName(randomString)
     *    call testInstance.getName()
     *    expected result: randomString
     *
     * </pre>
     */
    public void testEMB155() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);

        //
        // test 1
        //
        String newName = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10));
        mdebTester.setMetaDataEntityName(md1, newName);
        assertEquals("test 1: name does not match", newName, mdebTester.getMetaDataEntityName(md1));
        testTrace("test 1 passed");

        mdebTester.removeMDEBByPK(md1);
        succeed();
    }

    /**
     * Testcase Name: getNextVersion() Testcase Number: EMB156 setup: create
     * meta data entities md1 and md2 test procedure: 1.call
     * md2.setPreviousVersion(md1) expected result: md1.getPreviousVersion is
     * null md1.getNextVersion is md2 2.call md2.getNextVersion() expected
     * result: null md2.getPreviousVersion is md1
     *
     * </pre>
     */
    public void testEMB156() throws IOException, CreateException, MediaException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);

        //
        // test 1
        //
        String tmp = mdebTester.getMetaDataEntityPreviousVersion(md2);
        mdebTester.setMetaDataEntityPreviousVersion(md2, md1);
        assertNull("test 1: previous version is not null", mdebTester.getMetaDataEntityPreviousVersion(md1));
        assertEquals("test 1: next version is not md2", mdebTester.getMetaDataEntityNextVersion(md1), md2);
        testTrace("test 1 passed");
        //
        // test 2
        //
        assertEquals("test 2: previous version is not md1", mdebTester.getMetaDataEntityPreviousVersion(md2), md1);
        assertNull("test 2: next version is not null", mdebTester.getMetaDataEntityNextVersion(md2));
        testTrace("test 2 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getParents()
     *  Testcase Number: EMB157
     *
     *  setup: create meta data entities md1, md2, and md3
     *
     *  test procedure:
     *  1.call md1.getParents()
     *    expected result: empty array
     *
     *  2.call md1.addChild(md3)
     *    call md2.addChild(md3)
     *    call md3.getParents()
     *    expected result: 2 element array containing md1 and md2
     *
     * </pre>
     */
    public void testEMB157() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        String md3 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md3, xmlContent, false);

        //
        // test 1
        //
        String[] parents = mdebTester.getMetaDataEntityParents(md1);
        assertEquals("test 1: does not contain 0 element", 0, parents.length);
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.addMetaDataChild(md1, md3);
        mdebTester.addMetaDataChild(md2, md3);
        parents = mdebTester.getMetaDataEntityParents(md3);
        assertEquals("test 2: does not contain 2 elements", 2, parents.length);
        if ((parents[0].equals(md1) && parents[1].equals(md2)) || (parents[0].equals(md2) && parents[1].equals(md1))) {
            testTrace("test 2 passed");
        } else {
            fail("test 2: parents are incorrect");
        }

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMDEBByPK(md3);
        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: getPreviousVersion()
     *  Testcase Number: EMB158
     *
     *  setup: create meta data entities md1 and md2
     *
     *  test procedure:
     *  1.call md2.setPreviousVersion(md1)
     *    call md2.getPreviousVersion()
     *    expected result: md1
     *
     *  2.call md1.getPreviousVersion()
     *    expected result: null
     *
     * </pre>
     */
    public void testEMB158() throws IOException, CreateException, MediaException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);

        //
        // test 1
        //
        mdebTester.setMetaDataEntityPreviousVersion(md2, md1);
        assertTrue("test 1: previous version is not md1", mdebTester.getMetaDataEntityPreviousVersion(md2).equals(md1));
        testTrace("test 1 passed");
        //
        // test 2
        //
        assertNull("test 2: previous version is not null", mdebTester.getMetaDataEntityPreviousVersion(md1));
        testTrace("test 2 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: removeChild(MetaDataEntityLocal)
     *  Testcase Number: EMB160
     *
     *  setup: create meta data entities md1, md2, and md3
     *
     *  test procedure:
     *  1.call md1.removeChild(null)
     *    expected result: NullPointerException
     *
     *  2.call md1.addChild(md3), call getChildren and verify one element
     *    call md2.addChild(md3), call getChildren and verify one element
     *    call md1.removeChild(md3)
     *    expected result: no exception
     *    call md1.getChildren and verify empty array
     *  	 call md2.getChildren and verify one element
     *
     *  3.call md1.removeChild(md3)
     *    expected result: no exception
     *
     * </pre>
     */
    public void testEMB160() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        String md3 = mdebTester.createMetaDataEntityBean();

        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md3, xmlContent, false);
        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mdebTester.removeChildExceptions(md1, null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.addMetaDataChild(md1, md3);
        assertEquals("test 2: md1 does not have 1 child", 1, mdebTester.getMetaDataEntityChildren(md1).length);
        assertEquals("test 2: child of md1 is not md3", mdebTester.getMetaDataEntityChildren(md1)[0], md3);
        mdebTester.addMetaDataChild(md2, md3);
        assertEquals("test 2: md1 does not have 1 child", 1, mdebTester.getMetaDataEntityChildren(md2).length);
        assertEquals("test 2: child of md1 is not md3", mdebTester.getMetaDataEntityChildren(md2)[0], md3);

        mdebTester.removeMetaDataChild(md1, md3);
        assertEquals("test 2: md1 should not have children", 0, mdebTester.getMetaDataEntityChildren(md1).length);
        assertEquals("test 2: md2 does not have 1 child", 1, mdebTester.getMetaDataEntityChildren(md2).length);
        assertEquals("test 2: child of md1 is not md3", mdebTester.getMetaDataEntityChildren(md2)[0], md3);
        testTrace("test 2 passed");
        //
        // test 3
        //
        try {
            mdebTester.removeMetaDataChild(md1, md3);
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3 threw " + e.toString());
        }

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMDEBByPK(md3);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: removeMediaEntity(MediaEntityLocal)
     *  Testcase Number: EMB161
     *
     *  setup: create meta data entity md1 and Media entity me1
     *
     *  test procedure:
     *  1.call md1.removeMediaEntity(null)
     *    expected result: NullPointerException
     *
     *  2.call md1.addMediaEntity(me1)
     *    call md1.getMediaEntities and verify one element
     *    call md1.removeMediaEntity(me1)
     *    expected result: no exception, call md1.getMediaEntity to verify empty array
     *
     *  3.call md1.	removeMediaEntity(me1)
     *    expected result: no exception
     *
     * </pre>
     */
    public void testEMB161() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        String me1 = mdebTester.createMediaEntityBean();

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mdebTester.removeMediaExceptions(md1, null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.addMetaDataMedia(md1, me1);
        assertEquals("test 2: media entity not added", 1, mdebTester.getMetaDataEntityMediaEntities(md1).length);
        assertEquals("test 2: wrong media entity", mdebTester.getMetaDataEntityMediaEntities(md1)[0], me1);
        mdebTester.removeMetaDataMedia(md1, me1);
        assertEquals("test 2: meta data has associated media entities", 0, mdebTester
                .getMetaDataEntityMediaEntities(md1).length);
        testTrace("test 2 passed");
        //
        // test 3
        //
        try {
            mdebTester.removeMetaDataMedia(md1, me1);
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3 threw " + e.toString());
        }

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMEBByPK(me1);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: setName(String)
     *  Testcase Number: EMB162
     *
     *  setup: create meta data entity md1
     *
     *  test procedure:
     *  1.call md1.setName(null)
     *    expected result: NullPointerException
     *
     *  2.call md1.setName(&quot;&quot;)
     *    call md1.getName()
     *    expected result: empty string
     *
     *  3.call md1.setName(&quot;test&quot;)
     *    call md1.getName()
     *    expected result: &quot;test&quot;
     *
     * </pre>
     */
    public void testEMB162() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {
        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mdebTester.setNameExceptions(md1, null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.setMetaDataEntityName(md1, "");
        assertEquals("test 2: name is not an empty string", "", mdebTester.getMetaDataEntityName(md1));
        testTrace("test 2 passed");
        //
        // test 3
        //
        mdebTester.setMetaDataEntityName(md1, "test");
        assertEquals("test 3: name does not match", "test", mdebTester.getMetaDataEntityName(md1));
        testTrace("test 3 passed");

        mdebTester.removeMDEBByPK(md1);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: setPreviousVersion(MetaDataEntityLocal)
     *  Testcase Number: EMB163
     *
     *  setup: create meta data entities md1, md2, and md3
     *
     *  test procedure:
     *  1.call md1.setPreviousVersion(null)
     *    call md1.getPreviousVersion() and md1.getNextVersion()
     *    expected result: both are null
     *
     *  2.call md2.setPreviousVersion(md1)
     *  	 expected result: md1.getPreviousVersion() is null
     *  		      		  md1.getNextVersion() is md2
     *  					  md2.getPreviousVersion() is md1
     *  					  md2.getNextVersion() is null
     *
     *  3.call md2.setPreviousVersion(md1)
     *    expected result: no exception
     *
     *  4.call md3.setPreviousVersion(md1)
     *    expected result: VersionChainIntegrityException
     *
     *  5.call md2.setPreviousVersion(md3)
     *    expected result: VersionChainIntegrityException
     *
     *  6.call md1.setPreviousVersion(md3)
     *    expected result: VersionChainIntegrityException
     *
     *  7.call md3.setPreviousVersion(null)
     *    expected result: no exception
     *
     *  8.call md3.setPreviousVersion(md2)
     *    expected result: md1.getPreviousVersion() is null
     *  					  md1.getNextVersion() is md2
     *  					  md2.getPreviousVersion() is md1
     *  					  md2.getNextVersion() is md3
     *  					  md3.getPreviousVersion() is md2
     *  					  md3.getNextVersion() is null
     *
     *  9.call md2.setPreviousVersion(null)
     *    expected result: VersionChainIntegrityException
     *
     *  10.call md3.setPreviousVersion(null)
     *    expected result: md1.getPreviousVersion() is null
     *   				  md1.getNextVersion() is md2
     *  					  md2.getPreviousVersion() is md1
     *  					  md2.getNextVersion() is null
     *  					  md3.getPreviousVersion() is null
     *  					  md3.getNextVersion() is null
     *
     *  11.call md3.setPreviousVersion(md3)
     *     expected result: VersionChainIntegrityException
     *
     * </pre>
     */
    public void testEMB163() throws IOException, MediaException, CreateException, RemoteException, NamingException,
            FinderException, RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());

        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();
        String md3 = mdebTester.createMetaDataEntityBean();
        mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md2, xmlContent, false);
        mdebTester.setMetaDataEntityXML(md3, xmlContent, false);

        int exception = -1;

        //
        // test 1
        //
        try {
            mdebTester.setMetaDataEntityPreviousVersion(md1, null);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertNull("test 1: md1.getPreviousVersion is not null", mdebTester.getMetaDataEntityPreviousVersion(md1));
        assertNull("test 1: md1.getNextVersion is not null", mdebTester.getMetaDataEntityNextVersion(md1));
        testTrace("test 1 passed");
        //
        // test 2
        //
        mdebTester.setMetaDataEntityPreviousVersion(md2, md1);
        assertNull("test 2: md1.getPreviousVersion is not null", mdebTester.getMetaDataEntityPreviousVersion(md1));
        assertEquals("test 2: md1.getNextVersion is not md2", mdebTester.getMetaDataEntityNextVersion(md1), md2);
        assertEquals("test 2: md2.getPreviousVersion is not md1", mdebTester.getMetaDataEntityPreviousVersion(md2), md1);
        assertNull("test 2: md2.getNextVersion is not null", mdebTester.getMetaDataEntityNextVersion(md2));
        testTrace("test 2 passed");
        //
        // test 3
        //
        try {
            mdebTester.setMetaDataEntityPreviousVersion(md2, md1);
            testTrace("test 3 passed");
        } catch (Throwable e) {
            fail(" test 3 threw " + e.toString());
        }
        //
        // test 4
        //
        try {
            exception = mdebTester.setPreviousVersionExceptions(md3, md1);
        } catch (Throwable e) {
            fail("test 4 threw " + e.toString());
        }
        assertEquals("test 4: Should throw a VersionChainIntegrityException",
                MetaDataEntityLocalTestDriver.VERSIONCHAININTEGRITY, exception);
        testTrace("test 4 passed");
        //
        // test 5
        //
        try {
            exception = mdebTester.setPreviousVersionExceptions(md2, md3);
        } catch (Throwable e) {
            fail("test 5 threw " + e.toString());
        }
        assertEquals("test 5: Should throw a VersionChainIntegrityException",
                MetaDataEntityLocalTestDriver.VERSIONCHAININTEGRITY, exception);
        testTrace("test 5 passed");
        //
        // test 6
        //
        try {
            exception = mdebTester.setPreviousVersionExceptions(md1, md3);
        } catch (Throwable e) {
            fail("test 6 threw " + e.toString());
        }
        assertEquals("test 6: Should throw a VersionChainIntegrityException",
                MetaDataEntityLocalTestDriver.VERSIONCHAININTEGRITY, exception);
        testTrace("test 6 passed");
        //
        // test 7
        //
        try {
            mdebTester.setMetaDataEntityPreviousVersion(md3, null);
            testTrace("test 7 passed");
        } catch (Throwable e) {
            fail(" test 7 threw " + e.toString());
        }
        //
        // test 8
        //
        mdebTester.setMetaDataEntityPreviousVersion(md3, md2);
        assertNull("test 8: md1.getPreviousVersion is not null", mdebTester.getMetaDataEntityPreviousVersion(md1));
        assertEquals("test 8: md1.getNextVersion is not md2", mdebTester.getMetaDataEntityNextVersion(md1), md2);
        assertEquals("test 8: md2.getPreviousVersion is not md1", mdebTester.getMetaDataEntityPreviousVersion(md2), md1);
        assertEquals("test 8: md2.getNextVersion is not md3", mdebTester.getMetaDataEntityNextVersion(md2), md3);
        assertEquals("test 8: md3.getPreviousVersion is not md2", mdebTester.getMetaDataEntityPreviousVersion(md3), md2);
        assertNull("test 8: md3.getNextVersion is not null", mdebTester.getMetaDataEntityNextVersion(md3));
        testTrace("test 8 passed");
        //
        // test 9
        //
        try {
            exception = mdebTester.setPreviousVersionExceptions(md2, null);
        } catch (Throwable e) {
            fail("test 9 threw " + e.toString());
        }
        assertEquals("test 9: Should throw a VersionChainIntegrityException",
                MetaDataEntityLocalTestDriver.VERSIONCHAININTEGRITY, exception);
        testTrace("test 9 passed");
        //
        // test 10
        //
        mdebTester.setMetaDataEntityPreviousVersion(md3, null);
        assertNull("test 10: md1.getPreviousVersion is not null", mdebTester.getMetaDataEntityPreviousVersion(md1));
        assertEquals("test 10: md1.getNextVersion is not md2", mdebTester.getMetaDataEntityNextVersion(md1), md2);
        assertEquals("test 10: md2.getPreviousVersion is not md1", mdebTester.getMetaDataEntityPreviousVersion(md2),
                md1);
        assertNull("test 10: md2.getNextVersion is not null", mdebTester.getMetaDataEntityNextVersion(md2));
        assertNull("test 10: md3.getPreviousVersion is not null", mdebTester.getMetaDataEntityPreviousVersion(md3));
        assertNull("test 10: md3.getNextVersion is not null", mdebTester.getMetaDataEntityNextVersion(md3));
        testTrace("test 10 passed");
        //
        // test 11
        //
        try {
            exception = mdebTester.setPreviousVersionExceptions(md3, md3);
        } catch (Throwable e) {
            fail("test 11 threw " + e.toString());
        }
        assertEquals("test 11: Should throw a VersionChainIntegrityException",
                MetaDataEntityLocalTestDriver.VERSIONCHAININTEGRITY, exception);
        testTrace("test 11 passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);
        mdebTester.removeMDEBByPK(md3);

        succeed();
    }

    /**
     * <pre>
     *
     *  Testcase Name: setXML(String, boolean)
     *  Testcase Number: EMB164
     *
     *  setup: create meta data entities md1
     *  		  create file containing valid XML schema
     *
     *  test procedure:
     *  1.call md1.setXML(null, false)
     *    expected result: NullPointerException
     *
     *  2.a.call md1.setXML(validXML, true) (reference to DTD)
     *      expected result: no exception, verify md1.getXML() matches XML content
     *    b.call md1.setXML(validXML, true) (reference to schema)
     *      expected result: no exception , verify md1.getXML() matches XML content
     *
     *  3.a.create random String and call setXML(randomString, true)
     *      expected result: MetaDataSyntaxException
     *    b.call setXML(randomString, false)
     *      expected result: MetaDataSyntaxException
     *
     *  4.a.create well formed XML content that does not match DTD
     *      call md1.setXML(xmlContent, true)
     *      expected result: MetaDataValidationException
     *  	 b.create well formed XML content that does follow schema
     *      call md1.setXML(xmlContent, true)
     *      expected result: MetaDataValidationException
     *
     *  5.create XML file with only begin and end tags
     *    call md1.setXML
     *    expected result: no exception
     *
     *  6.call md1.setXML(&quot;&quot;)
     *    expected result: no exception
     *
     *  7.a.call setXML with XML that does not reference any DTD or schema and validate set to true
     *      expected result: MetaDataValidationException
     *    b.repeat test 7a with validate set to false
     *      expected result: no exception
     *
     * </pre>
     */
    public void testEMB164() throws FileNotFoundException, IOException, ParserConfigurationException, IOException,
            SAXException, CreateException, MediaException, RemoteException, NamingException, FinderException,
            RemoveException {

        String xmlContent = retrieveXML(embTestConfig.getXMLFile1());
        String md1 = mdebTester.createMetaDataEntityBean();
        String md2 = mdebTester.createMetaDataEntityBean();

        File xmlFile = new File(embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getXMLFile1());

        int exception = -1;
        //
        // test 1
        //
        try {
            exception = mdebTester.setXMLExceptions(md1, null, false);
        } catch (Throwable e) {
            fail("test 1 threw " + e.toString());
        }
        assertEquals("test 1: Should throw a NullPointerException", MetaDataEntityLocalTestDriver.NULLPOINTER,
                exception);
        testTrace("test 1 passed");
        //
        // test 2a
        //
        mdebTester.setMetaDataEntityXML(md1, xmlContent, true);
        String retrievedXML = mdebTester.getMetaDataEntityXML(md1);

        DOMParser parser = new DOMParser();
        parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", true);
        InputSource expectedXML = new InputSource(new ByteArrayInputStream(xmlContent.getBytes()));
        InputSource actualXML = new InputSource(new ByteArrayInputStream(retrievedXML.getBytes()));

        parser.parse(expectedXML);
        Document xmlDoc1 = parser.getDocument();
        parser.parse(actualXML);
        Document xmlDoc2 = parser.getDocument();

        Element root1 = xmlDoc1.getDocumentElement();
        Element root2 = xmlDoc2.getDocumentElement();

        assertTrue("test 2a: XML content does not match", xmlCompare(root1, root2));
        testTrace("test 2a passed");
        //
        // test 2b
        xmlContent = retrieveXML(embTestConfig.getXMLFile2());
        mdebTester.setMetaDataEntityXML(md1, xmlContent, true);
        retrievedXML = mdebTester.getMetaDataEntityXML(md1);

        expectedXML = new InputSource(new ByteArrayInputStream(xmlContent.getBytes()));
        actualXML = new InputSource(new ByteArrayInputStream(retrievedXML.getBytes()));

        parser.parse(expectedXML);
        xmlDoc1 = parser.getDocument();
        parser.parse(actualXML);
        xmlDoc2 = parser.getDocument();

        root1 = xmlDoc1.getDocumentElement();
        root2 = xmlDoc2.getDocumentElement();

        assertTrue("test 2b: XML content does not match", xmlCompare(root1, root2));
        testTrace("test 2b passed");
        //
        // test 3a
        //
        String randomXMLContent = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 1024));
        try {
            exception = mdebTester.setXMLExceptions(md1, randomXMLContent, true);
        } catch (Throwable e) {
            fail("test 3a threw " + e.toString());
        }
        assertEquals("test 3a: Should throw a MetaDataSynxtaxException", MetaDataEntityLocalTestDriver.METADATASYNTAX,
                exception);
        testTrace("test 3a passed");
        //
        // test 3b
        //
        try {
            exception = mdebTester.setXMLExceptions(md1, randomXMLContent, false);
        } catch (Throwable e) {
            fail("test 3b threw " + e.toString());
        }
        assertEquals("test 3b: Should throw a MetaDataSynxtaxException", MetaDataEntityLocalTestDriver.METADATASYNTAX,
                exception);
        testTrace("test 3b passed");
        //
        // test 4a
        //
        xmlContent = retrieveXML(embTestConfig.getInvalidXMLFile1());
        try {
            exception = mdebTester.setXMLExceptions(md1, xmlContent, true);
        } catch (Throwable e) {
            fail("test 4a threw " + e.toString());
        }
        assertEquals("test 4a: Should throw a MetaDataValidationException",
                MetaDataEntityLocalTestDriver.METADATAVALIDATION, exception);
        testTrace("test 4a passed");
        //
        // test 4b
        //
        xmlContent = retrieveXML(embTestConfig.getInvalidXMLFile2());
        try {
            exception = mdebTester.setXMLExceptions(md1, xmlContent, true);
        } catch (Throwable e) {
            fail("test 4b threw " + e.toString());
        }
        assertEquals("test 4b: Should throw a MetaDataValidationException",
                MetaDataEntityLocalTestDriver.METADATAVALIDATION, exception);
        testTrace("test 4b passed");
        //
        // test 5
        //
        xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test></test>";
        try {
            mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
            testTrace("test 5 passed");
        } catch (Exception e) {
            fail("test 5 threw " + e.toString());
        }
        //
        // test 6
        //
        try {
            mdebTester.setMetaDataEntityXML(md1, "", false);
            testTrace("test 6 passed");
        } catch (Exception e) {
            fail("test 6 threw " + e.toString());
        }

        //
        // test 7a
        //
        xmlContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><name><firstName>John</firstName><lastName>Smith</lastName></name></test>";
        try {
            exception = mdebTester.setXMLExceptions(md1, xmlContent, true);
        } catch (Throwable e) {
            fail("test 7a threw " + e.toString());
        }
        assertEquals("test 7a: Should throw a MetaDataValidationException",
                MetaDataEntityLocalTestDriver.METADATAVALIDATION, exception);
        testTrace("test 7a passed");
        //
        // test 7b
        //
        try {
            mdebTester.setMetaDataEntityXML(md1, xmlContent, false);
        } catch (Throwable e) {
            fail("test 7b threw " + e.toString());
        }
        testTrace("test 7b passed");

        mdebTester.removeMDEBByPK(md1);
        mdebTester.removeMDEBByPK(md2);

        succeed();
    }

}