package com.ibm.emb.test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import javax.emb.ContentAccessException;
import javax.emb.ContentTooLargeException;
import javax.emb.FormatNotFoundException;
import javax.emb.FormatSyntaxException;
import javax.emb.GenericMediaFormat;
import javax.emb.Media;
import javax.emb.MediaException;
import javax.emb.MediaFormatException;
import javax.emb.MediaFormatRegistry;
import javax.emb.MediaHeader;

import junit.framework.Test;
import junit.framework.TestSuite;

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

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

/**
 * <pre>
 *
 *
 *
 *    Line Item: MFB API
 *    Subcategory 1: javax.emb
 *    Subcategory 2: Media
 *
 *    This abstract class provides test methods for all methods described in the interface
 *    Media. For testing implementations of this interface one needs to create a Tester
 *    class which extends this interface and provide create methods for the concrete
 *    implementers of the Media interface
 *
 *
 *
 * </pre>
 */
public abstract class MediaTest extends EMBTestCaseBase {

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

    /**
     * abstract method to create an instance of Media this method needs to be
     * implemented in the test subclass and will create an instance of the
     * concrete implementation of the class under test
     */
    public abstract Media createMedia(File inputFile, String mimeType) throws MediaException;

    public abstract Media createMedia(InputStream in, String mimeType, String name) throws MediaException;

    /**
     * stdSuite method of this test class instantiates all tests necesssary to
     * for the standard compliance tests
     * @param String complete class Name of the implementor of the abstract test
     */
    public static Test stdSuite(String TestCaseImplementor) {
        TestSuite suite = new TestSuite(TestCaseImplementor);
        try {
            Class implementorClass = Class.forName(TestCaseImplementor);
            java.lang.reflect.Constructor implementorConstructor = implementorClass
                    .getDeclaredConstructor(new Class[] {String.class});
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB001"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB002"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB003"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB004"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB005"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB006"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB007"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB008"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB009"}));
            suite.addTest((Test) implementorConstructor.newInstance(new String[] {"testEMB010"}));
        } catch (java.lang.reflect.InvocationTargetException ex) {
            String msg = "error dynamically creating " + TestCaseImplementor + " " + ex.toString();
            EMBStaticHelper.testTrace(msg);
            throw new RuntimeException(msg);
        } catch (ClassNotFoundException ex) {
            String msg = "Class not found " + TestCaseImplementor + " " + ex.toString();
            EMBStaticHelper.testTrace(msg);
            throw new RuntimeException(msg);
        } catch (NoSuchMethodException ex) {
            String msg = "No such method in " + TestCaseImplementor + " " + ex.toString();
            EMBStaticHelper.testTrace(msg);
            throw new RuntimeException(msg);
        } catch (InstantiationException ex) {
            String msg = TestCaseImplementor + " " + ex.toString();
            EMBStaticHelper.testTrace(msg);
            throw new RuntimeException(msg);
        } catch (IllegalAccessException ex) {
            String msg = TestCaseImplementor + " " + ex.toString();
            EMBStaticHelper.testTrace(msg);
            throw new RuntimeException(msg);
        }
        return suite;
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: MIME_TYPE_UNKNOWN
     *    Testcase Number: EMB001
     *
     *    setup:
     *
     *    test procedure:
     *    1.query value of constant
     *      expected result: &quot;www/unknown&quot;
     *
     *
     *
     * </pre>
     */
    public void testEMB001() {
        assertEquals("test 1: ", Media.MIME_TYPE_UNKNOWN, "www/unknown");
        testTrace("test 1 passed");
        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getContent()
     *    Testcase Number: EMB002
     *
     *    setup:
     *
     *    test procedure:
     *    1.rebind(&quot;test&quot;, new GenericMediaFormat), create file of maxMediaSize and fill with random content
     *      create MediaBean by using file constructor, call getContent()
     *      expected result: ContentTooLargeException
     *
     *    2.create small file, call getContent(), compare content with file content
     *      expected result: content and file content are equal
     *
     *
     *
     * </pre>
     */
    public void testEMB002() throws IOException, MediaException {

        Media testInstance = null;

        if (embTestConfig.getIncludeLongRunningTest()) {
            int maxSize = Integer.parseInt(embTestConfig.getMaxMediaSize());
            if ((maxSize == -1) || (maxSize > Integer.MAX_VALUE)) {
                testTrace("test 1: running long test");
                File largeFile = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "largeFile.test");
                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();

                javax.emb.MediaFormatRegistry formatRegistry = javax.emb.MediaFormatRegistry.SINGLETON;
                formatRegistry.rebind("test", new GenericMediaFormat());

                //
                // test1
                //
                testInstance = createMedia(new FileInputStream(largeFile), null, "largeFile.test");
                try {
                    testInstance.getContent();
                    fail("test 1: Should throw a ContentTooLargeException");
                } catch (ContentTooLargeException e) {
                    testTrace("test 1 passed");
                } catch (Exception e) {
                    testTrace("test 1: Should throw a ContentTooLargeException but threw " + e.toString());
                }
                largeFile.delete();
            }
        }
        //
        // test2
        //
        // random test file
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "testMediaBeanFile.test");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();

        testInstance = createMedia(file1024Byte, "test");
        byte[] contentArray = testInstance.getContent();
        assertTrue("test 2: ", Arrays.equals(testArray, contentArray));
        testTrace("test 2 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getFormat()
     *    Testcase Number: EMB003
     *
     *    setup:
     *
     *    test procedure:
     *    1.rebind(&quot;jpg&quot;, new JpegFormat()), create random content file with name test.jpg
     *      create MediaBean from file, run getFormat()
     *      expected result: not null and instance of JpegFormat
     *
     *    2.unbind(&quot;xxx&quot;) catch FormatNotFoundException
     *      create random content file with name test.xxx, create MediaBean from file, run getFormat()
     *      expected result: FormatNotFoundException
     *
     *
     *
     * </pre>
     */
    public void testEMB003() throws IOException, MediaException {

        Media testInstance = null;
        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());
        formatRegistry.rebind("xxx", new GenericMediaFormat());

        //
        // test1
        //
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.jpg");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();

        testInstance = createMedia(file1024Byte, null);
        assertTrue("test 1", testInstance.getFormat() != null);
        assertTrue("test 1", testInstance.getFormat() instanceof JpegFormat);
        testTrace("test 1 passed");
        //
        // test2
        //
        file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.xxx");
        file1024Byte.createNewFile();
        file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();
        testInstance = createMedia(file1024Byte, null);
        try {
            formatRegistry.unbind("xxx");
        } catch (Exception e) {
        }
        try {
            testInstance.getFormat();
            fail("test 2 : Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2 : Should throw a FormatNotFoundException but threw " + e.toString());
        }
        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getHeader()
     *    Testcase Number: EMB004
     *
     *    setup:
     *
     *    test procedure:
     *    1.rebind(&quot;jpg&quot;,new JpegFormat()), create file from JPEG image file with name test.jpg
     *      create MediaBean from file, run getHeader()
     *      expected result: return value not null and equals getFormat().extractHeader()
     *
     *    2.unbind(&quot;xxx&quot;) catch FormatNotFoundException
     *      create random content file with name test.xxx, create MediaBean from file, run getHeader()
     *      expected result: FormatNotFoundException is thrown
     *
     *    3.create file from random content, run getHeader()
     *      expected result: FormatSyntaxException
     *
     *    4.use static byte array to create BMP file (contains unsupported version) and run getHeader()
     *      expected result: MediaFormatException
     *
     *
     *
     * </pre>
     */
    public void testEMB004() throws IOException, MediaException {
        // name of a jpg sample
        String mediaJpg = embTestConfig.getJpgPictureName2();
        // full path of the jpg sample
        String mediaJpgFullPath = embTestConfig.getMediaDir() + File.separatorChar + mediaJpg;
        File mediaJpegFile = new File(mediaJpgFullPath);

        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.xxx");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();

        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());
        formatRegistry.rebind("bmp", new BmpFormat());
        try {
            formatRegistry.unbind("xxx");
        } catch (Exception e) {
            // do nothing
        }
        Media testInstance = null;
        //
        // test1
        //
        testInstance = createMedia(mediaJpegFile, "jpg");
        InputStream in = new FileInputStream(mediaJpegFile);
        MediaHeader testHeader = testInstance.getHeader();
        assertTrue("test 1: ", testHeader.equals(testInstance.getFormat().extractHeader(in)));
        testTrace("test 1 passed");
        in.close();
        //
        // test2
        //
        try {
            testInstance = createMedia(file1024Byte, null);
            testInstance.getHeader();
            fail("test 2: Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw a FormatNotFoundException but threw " + e.toString());
        }
        //
        // test 3
        //
        File file512Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.jpg");
        file512Byte.createNewFile();
        FileOutputStream file512ByteOutputStream = new FileOutputStream(file512Byte);
        byte[] randomArray = EMBStaticHelper.createRandomByteArray(512);
        file512ByteOutputStream.write(randomArray);
        file512ByteOutputStream.close();
        try {
            testInstance = createMedia(file512Byte, null);
            testInstance.getHeader();
            fail("test 3: Should throw a FormatSyntaxException");
        } catch (FormatSyntaxException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3: Should throw a FormatSyntaxException but threw " + e.toString());
        }
        //
        // test 4
        //
        byte[] corruptedBmpByteArray = {76, 86, 70, 43, 0, 0, 0, 0, 0, 0, 54, 4, 0, 0, 40, 0, 0, 0, 100, 0, 0, 0, 100,
                0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 16, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 1, 1, 1, 0, 2, 2, 2, 0, 3, 3, 3, 0, 4, 4, 4, 0, 5, 5, 5, 0, 6, 6, 6, 7, 7, 7, 0, 8, 8, 8, 0, 9,
                9, 9, 0, 1, 10, 10, 0, 11, 11};
        testInstance = createMedia(new ByteArrayInputStream(corruptedBmpByteArray), null, "test.bmp");
        try {
            testInstance.getHeader();
            fail("test 4: Should throw a MediaFormatException");
        } catch (MediaFormatException e) {
            testTrace("test 4 passed");
        } catch (Exception e) {
            fail("test 4: Should throw a MediaFormatException but threw " + e.toString());
        }

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getMimeType()
     *    Testcase Number: EMB005
     *
     *    setup:
     *
     *    test procedure:
     *    1.rebind(&quot;jpg&quot;, new JpegFormat())
     *      create random content file with name test.jpg, create MediaBean from file with mimetype null, run getMimeType()
     *      expected result: result equals getFormat().getDefaultMimeType()
     *
     *    2.create random content file with name test.jpg, create MediaBean from file with mimetype &quot;test&quot;, run getMimeType()
     *      expected result: result is &quot;test&quot;
     *
     *    3.create random content file with name test.xxx and create MediaBean from file with null mimetype, run getMimeType()
     *      expected result: FormatNotFoundException
     *
     *
     *
     * </pre>
     */
    public void testEMB005() throws IOException, MediaException {
        // random file
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.jpg");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();

        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());

        // test instance
        Media testInstance = null;
        //
        // test 1
        //
        testInstance = createMedia(file1024Byte, null);
        assertEquals("test1: ", testInstance.getMimeType(), testInstance.getFormat().getDefaultMimeType());
        testTrace("test 1 passed");
        //
        // test 2
        //
        testInstance = createMedia(file1024Byte, "test");
        assertEquals("test 2: ", "test", testInstance.getMimeType());
        testTrace("test 2 passed");
        //
        // test 3
        //
        File testFile = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.xxx");
        testFile.createNewFile();
        FileOutputStream testFileOutputStream = new FileOutputStream(testFile);
        testFileOutputStream.write(testArray);
        testFileOutputStream.close();
        try {
            testInstance = createMedia(testFile, null);
            fail("test 3: Should throw a FormatNotFoundException");
        } catch (FormatNotFoundException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3: Should throw a FormatNotFoundException but threw " + e.toString());
        }

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getName()
     *    Testcase Number: EMB006
     *
     *    setup:
     *
     *    test procedure:
     *    1.rebind(&quot;jpg&quot;, new JpegFormat()), create random content file with name test.jpg, create MediaBean from file with mimetype null, run getName()
     *      expected result: result is not null and contains a '.'
     *
     *    2.create MediaBean from InputStream with null mimetype and random name, call getName
     *      expected result: name matches original name
     *
     *
     *
     * </pre>
     */
    public void testEMB006() throws IOException, MediaException {
        // random file
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.jpg");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();

        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());
        // test instance
        Media testInstance = null;
        //
        // test 1
        //
        testInstance = createMedia(file1024Byte, null);
        assertNotNull(testInstance.getName());
        assertTrue("test 1: name does not have a file extension", testInstance.getName().indexOf(".") != -1);
        testTrace("test 1 passed");
        //
        // test 2
        //
        String name = "testMedia.jpg";
        testInstance = createMedia(new FileInputStream(file1024Byte), null, name);
        assertEquals("test 2: name does not match", name, testInstance.getName());
        testTrace("test 2 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getProxy()
     *    Testcase Number: EMB007
     *
     *    setup:
     *
     *    test procedure:
     *    1.create random content file with name test.jpg, create MediaBean from file, call getProxy()
     *    expected result: not null
     *
     *
     *
     * </pre>
     */
    public void testEMB007() throws MediaException, IOException {
        // random file
        String jpgFilePath = embTestConfig.getMediaDir() + File.separatorChar + embTestConfig.getJpgPictureName1();
        File jpgFile = new File(jpgFilePath);
        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());
        // test instance
        Media testInstance = null;
        //
        // test 1
        //
        testInstance = createMedia(jpgFile, "image/jpeg");
        assertNotNull("test 1: proxy is null", testInstance.getProxy());
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: getSize()
     *    Testcase Number: EMB008
     *
     *    setup:
     *    test procedure:
     *    1.create random content file with name test.jpg and content size 1024, create MediaBean from file, run getSize()
     *      expected result: result is 1024
     *
     *
     *
     * </pre>
     */
    public void testEMB008() throws IOException, MediaException, Exception {
        // random file
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "test.jpg");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();
        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("jpg", new JpegFormat());
        // test instance
        Media testInstance;
        //
        // test1
        //
        testInstance = createMedia(file1024Byte, null);
        assertEquals("test1 : ", 1024, testInstance.getSize());
        testTrace("test 1 passed");
        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: readContent(long position, byte[] buffer)
     *    Testcase Number: EMB009
     *
     *    setup: create file (1024 byte), rebind(&quot;test&quot;, new GenericMediaFormat()), create MediaBean
     *    		  create byte array buffer of size 1024 bytes
     *
     *    test procedure:
     *    1.call readContent(-1,testBuffer)
     *      expected result: IndexOutOfBoundsException
     *
     *    2.call readContent(1025,testBuffer)
     *    	 expected result: IndexOutOfBoundsException
     *
     *    3.call readContent(0,null)
     *      expected result: NullPointerException
     *
     *    4.call readContent(10,testBuffer) with buffer size 100 bytes
     *      compare to media content offset 10, length of return value
     *      expected result: same content
     *
     *    5.call readContent(1024,testBuffer)
     *      expected result: -1 returned with empty buffer
     *
     *    6.create testInstance from nonexistent file and call readContent(0,buffer)
     *      expected result: ContentAccessException
     *
     *    7.create testInstance of size 100 bytes and buffer of size 1024 bytes
     *      call readContent(0,testBuffer) and compare content of media and buffer
     *      expected result: buffer partially filled (return value is the length)
     *
     *
     *
     * </pre>
     */
    public void testEMB009() throws IOException, MediaException, Exception {
        //
        // Initialize the testcase
        //
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "testFile1024Byte.test");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();
        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = javax.emb.MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("test", new GenericMediaFormat());
        Media testInstance = createMedia(file1024Byte, "test");
        byte[] testBuffer = new byte[1024];
        //
        // test 1
        //
        try {
            testInstance.readContent(-1, testBuffer);
            fail("test 1: Should throw an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw an IndexOutOfBoundsException but threw " + e.toString());
        }
        //
        // test 2
        //
        try {
            testInstance.readContent(1025, testBuffer);
            fail("test 2: Should throw an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw an IndexOutOfBoundsException but threw " + e.toString());
        }
        //
        // test 3
        //
        try {
            testInstance.readContent(0, null);
            fail("test 3: Should throw an NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3: Should throw an NullPointerException but threw " + e.toString());
        }
        //
        // test 4
        //
        byte[] buffer100Byte = new byte[100];
        int amountCopied = testInstance.readContent(10, buffer100Byte);
        FileInputStream file1024ByteInputStream = new FileInputStream(file1024Byte);
        byte[] fileArray = new byte[amountCopied];
        file1024ByteInputStream.skip(10);
        int amountRead = file1024ByteInputStream.read(fileArray, 0, amountCopied);
        if (amountRead != amountCopied) throw new ContentAccessException();
        file1024ByteInputStream.close();
        for (int i = 0; i < amountCopied; i++) {
            assertEquals("test 4: ", buffer100Byte[i], fileArray[i]);
        }
        testTrace("test 4 passed");
        //
        // test 5
        //
        amountCopied = testInstance.readContent(1024, testBuffer);
        assertEquals("test 5: ", -1, amountCopied);
        testTrace("test 5 passed");
        //
        // test 6
        //
        try {
            testInstance = createMedia(new File("nonexistentFile.test"), "test");
            testInstance.readContent(0, testBuffer);
            fail("test 6: Should throw a ContentAccessException");
        } catch (ContentAccessException e) {
            testTrace("test 6 passed");
        } catch (Exception e) {
            fail("test 6: Should throw a ContentAccessException but threw " + e.toString());
        }
        //
        // test 7
        //
        File file100Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "testFile100Byte.test");
        file100Byte.createNewFile();
        FileOutputStream file100ByteOutputStream = new FileOutputStream(file100Byte);
        byte[] randomTestArray = EMBStaticHelper.createRandomByteArray(100);
        file100ByteOutputStream.write(randomTestArray);
        file100ByteOutputStream.close();
        testInstance = createMedia(file100Byte, "test");
        amountCopied = testInstance.readContent(0, testBuffer);
        for (int j = 0; j < amountCopied; j++) {
            assertEquals("test 7: ", testBuffer[j], randomTestArray[j]);
        }
        testTrace("test 7 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *    Testcase Name: readContent(long position, byte[] buffer, int offset, int length)
     *    Testcase Number: EMB010
     *
     *    setup: create file (1024 byte), rebind(&quot;test&quot;, new GenericMediaFormat()), create MediaBean
     *    		  create byte array buffer of size 1024 bytes
     *
     *    test procedure:
     *    1.call readContent(-1,testBuffer,0,1024)
     *      expected result: IndexOutOfBoundsException
     *
     *    2.call readContent(1025,testBuffer,0,1024)
     *      expected result: IndexOutOfBoundsException
     *
     *    3.call readContent(0,testBuffer,-1,1024)
     *      expected result: IndexOutOfBoundsException
     *
     *    4.call readContent(0,testBuffer,1025,1024)
     *      expected result: IndexOutOfBoundsException
     *
     *    5.call readContent(0,testBuffer,0,-1)
     *      expected result: NegativeIndexArrayException
     *
     *    6.call readContent(0,null,0,1024)
     *      expected result: NullPointerException
     *
     *    7.create testInstance from nonexistent file and call readContent(0,testBuffer,0,1024)
     *      expected result: ContentAccessException
     *
     *    8.call readContent(0,testBuffer,0,1024) (loop to fill buffer)
     *      compare with file content
     *      expected result: same content
     *
     *    9.call readContent(512,testBuffer,0,1024)
     *      expected result: buffer partially filled with 512 bytes matching last 512 bytes
     *      of media content (could be less if nonblocking)
     *
     *    10.call readContent(1024,testBuffer,0,1024)
     *       expected result: return -1 with empty buffer
     *
     *    11.call readContent(0,testBuffer,100,924), compare buffer offset 100 with media content
     *       (use return value as length)
     *       expected result: same content
     *
     *    12.call readContent(200,testBuffer,100,100), compare buffer offset 100 with file offset 200
     *      (use return value as length)
     *      expected result: same content
     *
     *    13.call readContent(0,testBuffer,0,1048), compare buffer with file content
     *       expected result: same content and no exceptions
     *
     *
     *
     * </pre>
     */
    public void testEMB010() throws IOException, MediaException, Exception {
        //
        // Initialize the testcase
        //
        File file1024Byte = new File(embTestConfig.getMediaDir() + File.separatorChar + "Generated" + File.separatorChar + "testFile1024Byte.test");
        file1024Byte.createNewFile();
        FileOutputStream file1024ByteOutputStream = new FileOutputStream(file1024Byte);
        byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
        file1024ByteOutputStream.write(testArray);
        file1024ByteOutputStream.close();
        // register media format
        javax.emb.MediaFormatRegistry formatRegistry = javax.emb.MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("test", new GenericMediaFormat());
        Media testInstance = createMedia(file1024Byte, "test");
        byte[] testBuffer = new byte[1024];
        //
        // test 1
        //
        try {
            testInstance.readContent(-1, testBuffer, 0, 1024);
            fail("test 1: Should throw an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw an IndexOutOfBoundsException but threw " + e.toString());
        }
        //
        // test 2
        //
        try {
            testInstance.readContent(1025, testBuffer, 0, 1024);
            fail("test 2: Should throw an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw an IndexOutOfBoundsException but threw " + e.toString());
        }
        //
        // test 3
        //
        try {
            testInstance.readContent(0, testBuffer, -1, 1024);
            fail("test 3: Should throw an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            testTrace("test 3 passed");
        } catch (Exception e) {
            fail("test 3: Should throw an IndexOutOfBoundsException but threw " + e.toString());
        }
        //
        // test 4
        //
        try {
            testInstance.readContent(0, testBuffer, 1025, 1024);
            fail("test 4: Should throw an IndexOutOfBoundsException");
        } catch (IndexOutOfBoundsException e) {
            testTrace("test 4 passed");
        } catch (Exception e) {
            fail("test 4: Should throw an IndexOutOfBoundsException but threw " + e.toString());
        }
        //
        // test 5
        //
        try {
            testInstance.readContent(0, testBuffer, 0, -1);
            fail("test 5: Should throw a NegativeIndexArrayException");
        } catch (NegativeArraySizeException e) {
            testTrace("test 5 passed");
        } catch (Exception e) {
            fail("test 5: Should throw a NegativeIndexArrayExcepton but threw " + e.toString());
        }
        //
        // test 6
        //
        try {
            testInstance.readContent(0, null, 0, 1024);
            fail("test 6: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 6 passed");
        } catch (Exception e) {
            fail("test 6: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 7
        //
        try {
            testInstance = createMedia(new File("nonexistentFile.test"), "test");
            testInstance.readContent(0, testBuffer, 0, 1024);
            fail("test 7: Should throw a ContentAccessException");
        } catch (ContentAccessException e) {
            testTrace("test 7 passed");
        } catch (Exception e) {
            fail("test 7: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 8
        //
        int totalCopied = 0;
        int amountCopied = 0;
        while (totalCopied != 1024) {
            amountCopied = testInstance.readContent(totalCopied, testBuffer, totalCopied, 1024 - totalCopied);
            totalCopied = totalCopied + amountCopied;
        }
        assertTrue("test 8: ", java.util.Arrays.equals(testArray, testBuffer));
        testTrace("test 8 passed");
        //
        // test 9
        //
        amountCopied = testInstance.readContent(512, testBuffer, 0, 1024);
        for (int i = 0; i < amountCopied; i++) {
            assertEquals("test 9: ", testArray[i + 512], testBuffer[i]);
        }
        testTrace("test 9 passed");
        //
        // test 10
        //
        amountCopied = testInstance.readContent(1024, testBuffer, 0, 1024);
        assertEquals("test 10: ", -1, amountCopied);
        testTrace("test 10 passed");
        //
        // test 11
        //
        amountCopied = testInstance.readContent(0, testBuffer, 100, 924);
        for (int j = 0; j < amountCopied; j++) {
            assertEquals("test 11: ", testArray[j], testBuffer[j + 100]);
        }
        testTrace("test 11 passed");
        //
        // test 12
        //
        amountCopied = testInstance.readContent(200, testBuffer, 100, 100);
        for (int k = 0; k < amountCopied; k++) {
            assertEquals("test 12: ", testArray[k + 200], testBuffer[k + 100]);
        }
        testTrace("test 12 passed");
        //
        // test 13
        //
        amountCopied = testInstance.readContent(0, testBuffer, 0, 1048);
        for (int m = 0; m < amountCopied; m++) {
            assertEquals("test 13: ", testArray[m], testBuffer[m]);
        }
        testTrace("test 13 passed");

        succeed();
    }
}
