0001: package com.ibm.emb.test;
0002:
0003: import java.io.ByteArrayInputStream;
0004: import java.io.File;
0005: import java.io.FileInputStream;
0006: import java.io.FileOutputStream;
0007: import java.io.IOException;
0008: import java.io.InputStream;
0009: import java.util.Arrays;
0010:
0011: import javax.emb.ContentAccessException;
0012: import javax.emb.ContentTooLargeException;
0013: import javax.emb.FormatNotFoundException;
0014: import javax.emb.FormatSyntaxException;
0015: import javax.emb.GenericMediaFormat;
0016: import javax.emb.Media;
0017: import javax.emb.MediaException;
0018: import javax.emb.MediaFormatException;
0019: import javax.emb.MediaFormatRegistry;
0020: import javax.emb.MediaHeader;
0021:
0022: import junit.framework.Test;
0023: import junit.framework.TestSuite;
0024:
0025: import org.objectweb.jonas.emb.mfb.formats.image.BmpFormat;
0026: import org.objectweb.jonas.emb.mfb.formats.image.JpegFormat;
0027:
0028: import com.ibm.emb.junit.EMBStaticHelper;
0029: import com.ibm.emb.junit.EMBTestCaseBase;
0030:
0031: /**
0032: * <pre>
0033: *
0034: *
0035: *
0036: * Line Item: MFB API
0037: * Subcategory 1: javax.emb
0038: * Subcategory 2: Media
0039: *
0040: * This abstract class provides test methods for all methods described in the interface
0041: * Media. For testing implementations of this interface one needs to create a Tester
0042: * class which extends this interface and provide create methods for the concrete
0043: * implementers of the Media interface
0044: *
0045: *
0046: *
0047: * </pre>
0048: */
0049: public abstract class MediaTest extends EMBTestCaseBase {
0050:
0051: public MediaTest(String name) throws MediaException {
0052: super (name);
0053: }
0054:
0055: /**
0056: * abstract method to create an instance of Media this method needs to be
0057: * implemented in the test subclass and will create an instance of the
0058: * concrete implementation of the class under test
0059: */
0060: public abstract Media createMedia(File inputFile, String mimeType)
0061: throws MediaException;
0062:
0063: public abstract Media createMedia(InputStream in, String mimeType,
0064: String name) throws MediaException;
0065:
0066: /**
0067: * stdSuite method of this test class instantiates all tests necesssary to
0068: * for the standard compliance tests
0069: * @param String complete class Name of the implementor of the abstract test
0070: */
0071: public static Test stdSuite(String TestCaseImplementor) {
0072: TestSuite suite = new TestSuite(TestCaseImplementor);
0073: try {
0074: Class implementorClass = Class.forName(TestCaseImplementor);
0075: java.lang.reflect.Constructor implementorConstructor = implementorClass
0076: .getDeclaredConstructor(new Class[] { String.class });
0077: suite.addTest((Test) implementorConstructor
0078: .newInstance(new String[] { "testEMB001" }));
0079: suite.addTest((Test) implementorConstructor
0080: .newInstance(new String[] { "testEMB002" }));
0081: suite.addTest((Test) implementorConstructor
0082: .newInstance(new String[] { "testEMB003" }));
0083: suite.addTest((Test) implementorConstructor
0084: .newInstance(new String[] { "testEMB004" }));
0085: suite.addTest((Test) implementorConstructor
0086: .newInstance(new String[] { "testEMB005" }));
0087: suite.addTest((Test) implementorConstructor
0088: .newInstance(new String[] { "testEMB006" }));
0089: suite.addTest((Test) implementorConstructor
0090: .newInstance(new String[] { "testEMB007" }));
0091: suite.addTest((Test) implementorConstructor
0092: .newInstance(new String[] { "testEMB008" }));
0093: suite.addTest((Test) implementorConstructor
0094: .newInstance(new String[] { "testEMB009" }));
0095: suite.addTest((Test) implementorConstructor
0096: .newInstance(new String[] { "testEMB010" }));
0097: } catch (java.lang.reflect.InvocationTargetException ex) {
0098: String msg = "error dynamically creating "
0099: + TestCaseImplementor + " " + ex.toString();
0100: EMBStaticHelper.testTrace(msg);
0101: throw new RuntimeException(msg);
0102: } catch (ClassNotFoundException ex) {
0103: String msg = "Class not found " + TestCaseImplementor + " "
0104: + ex.toString();
0105: EMBStaticHelper.testTrace(msg);
0106: throw new RuntimeException(msg);
0107: } catch (NoSuchMethodException ex) {
0108: String msg = "No such method in " + TestCaseImplementor
0109: + " " + ex.toString();
0110: EMBStaticHelper.testTrace(msg);
0111: throw new RuntimeException(msg);
0112: } catch (InstantiationException ex) {
0113: String msg = TestCaseImplementor + " " + ex.toString();
0114: EMBStaticHelper.testTrace(msg);
0115: throw new RuntimeException(msg);
0116: } catch (IllegalAccessException ex) {
0117: String msg = TestCaseImplementor + " " + ex.toString();
0118: EMBStaticHelper.testTrace(msg);
0119: throw new RuntimeException(msg);
0120: }
0121: return suite;
0122: }
0123:
0124: /**
0125: * <pre>
0126: *
0127: *
0128: *
0129: * Testcase Name: MIME_TYPE_UNKNOWN
0130: * Testcase Number: EMB001
0131: *
0132: * setup:
0133: *
0134: * test procedure:
0135: * 1.query value of constant
0136: * expected result: "www/unknown"
0137: *
0138: *
0139: *
0140: * </pre>
0141: */
0142: public void testEMB001() {
0143: assertEquals("test 1: ", Media.MIME_TYPE_UNKNOWN, "www/unknown");
0144: testTrace("test 1 passed");
0145: succeed();
0146: }
0147:
0148: /**
0149: * <pre>
0150: *
0151: *
0152: *
0153: * Testcase Name: getContent()
0154: * Testcase Number: EMB002
0155: *
0156: * setup:
0157: *
0158: * test procedure:
0159: * 1.rebind("test", new GenericMediaFormat), create file of maxMediaSize and fill with random content
0160: * create MediaBean by using file constructor, call getContent()
0161: * expected result: ContentTooLargeException
0162: *
0163: * 2.create small file, call getContent(), compare content with file content
0164: * expected result: content and file content are equal
0165: *
0166: *
0167: *
0168: * </pre>
0169: */
0170: public void testEMB002() throws IOException, MediaException {
0171:
0172: Media testInstance = null;
0173:
0174: if (embTestConfig.getIncludeLongRunningTest()) {
0175: int maxSize = Integer.parseInt(embTestConfig
0176: .getMaxMediaSize());
0177: if ((maxSize == -1) || (maxSize > Integer.MAX_VALUE)) {
0178: testTrace("test 1: running long test");
0179: File largeFile = new File(embTestConfig.getMediaDir()
0180: + File.separatorChar + "Generated"
0181: + File.separatorChar + "largeFile.test");
0182: largeFile.createNewFile();
0183: FileOutputStream largeFileOutputStream = new FileOutputStream(
0184: largeFile);
0185: // create byte array 1 byte larger than maxMediaSize
0186: byte[] testArray = EMBStaticHelper
0187: .createRandomByteArray(Integer
0188: .parseInt(embTestConfig
0189: .getMaxMediaSize()) + 1);
0190: largeFileOutputStream.write(testArray);
0191: largeFileOutputStream.close();
0192:
0193: javax.emb.MediaFormatRegistry formatRegistry = javax.emb.MediaFormatRegistry.SINGLETON;
0194: formatRegistry.rebind("test", new GenericMediaFormat());
0195:
0196: //
0197: // test1
0198: //
0199: testInstance = createMedia(new FileInputStream(
0200: largeFile), null, "largeFile.test");
0201: try {
0202: testInstance.getContent();
0203: fail("test 1: Should throw a ContentTooLargeException");
0204: } catch (ContentTooLargeException e) {
0205: testTrace("test 1 passed");
0206: } catch (Exception e) {
0207: testTrace("test 1: Should throw a ContentTooLargeException but threw "
0208: + e.toString());
0209: }
0210: largeFile.delete();
0211: }
0212: }
0213: //
0214: // test2
0215: //
0216: // random test file
0217: File file1024Byte = new File(embTestConfig.getMediaDir()
0218: + File.separatorChar + "Generated" + File.separatorChar
0219: + "testMediaBeanFile.test");
0220: file1024Byte.createNewFile();
0221: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0222: file1024Byte);
0223: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0224: file1024ByteOutputStream.write(testArray);
0225: file1024ByteOutputStream.close();
0226:
0227: testInstance = createMedia(file1024Byte, "test");
0228: byte[] contentArray = testInstance.getContent();
0229: assertTrue("test 2: ", Arrays.equals(testArray, contentArray));
0230: testTrace("test 2 passed");
0231:
0232: succeed();
0233: }
0234:
0235: /**
0236: * <pre>
0237: *
0238: *
0239: *
0240: * Testcase Name: getFormat()
0241: * Testcase Number: EMB003
0242: *
0243: * setup:
0244: *
0245: * test procedure:
0246: * 1.rebind("jpg", new JpegFormat()), create random content file with name test.jpg
0247: * create MediaBean from file, run getFormat()
0248: * expected result: not null and instance of JpegFormat
0249: *
0250: * 2.unbind("xxx") catch FormatNotFoundException
0251: * create random content file with name test.xxx, create MediaBean from file, run getFormat()
0252: * expected result: FormatNotFoundException
0253: *
0254: *
0255: *
0256: * </pre>
0257: */
0258: public void testEMB003() throws IOException, MediaException {
0259:
0260: Media testInstance = null;
0261: // register media format
0262: MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
0263: formatRegistry.rebind("jpg", new JpegFormat());
0264: formatRegistry.rebind("xxx", new GenericMediaFormat());
0265:
0266: //
0267: // test1
0268: //
0269: File file1024Byte = new File(embTestConfig.getMediaDir()
0270: + File.separatorChar + "Generated" + File.separatorChar
0271: + "test.jpg");
0272: file1024Byte.createNewFile();
0273: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0274: file1024Byte);
0275: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0276: file1024ByteOutputStream.write(testArray);
0277: file1024ByteOutputStream.close();
0278:
0279: testInstance = createMedia(file1024Byte, null);
0280: assertTrue("test 1", testInstance.getFormat() != null);
0281: assertTrue("test 1",
0282: testInstance.getFormat() instanceof JpegFormat);
0283: testTrace("test 1 passed");
0284: //
0285: // test2
0286: //
0287: file1024Byte = new File(embTestConfig.getMediaDir()
0288: + File.separatorChar + "Generated" + File.separatorChar
0289: + "test.xxx");
0290: file1024Byte.createNewFile();
0291: file1024ByteOutputStream = new FileOutputStream(file1024Byte);
0292: file1024ByteOutputStream.write(testArray);
0293: file1024ByteOutputStream.close();
0294: testInstance = createMedia(file1024Byte, null);
0295: try {
0296: formatRegistry.unbind("xxx");
0297: } catch (Exception e) {
0298: }
0299: try {
0300: testInstance.getFormat();
0301: fail("test 2 : Should throw a FormatNotFoundException");
0302: } catch (FormatNotFoundException e) {
0303: testTrace("test 2 passed");
0304: } catch (Exception e) {
0305: fail("test 2 : Should throw a FormatNotFoundException but threw "
0306: + e.toString());
0307: }
0308: succeed();
0309: }
0310:
0311: /**
0312: * <pre>
0313: *
0314: *
0315: *
0316: * Testcase Name: getHeader()
0317: * Testcase Number: EMB004
0318: *
0319: * setup:
0320: *
0321: * test procedure:
0322: * 1.rebind("jpg",new JpegFormat()), create file from JPEG image file with name test.jpg
0323: * create MediaBean from file, run getHeader()
0324: * expected result: return value not null and equals getFormat().extractHeader()
0325: *
0326: * 2.unbind("xxx") catch FormatNotFoundException
0327: * create random content file with name test.xxx, create MediaBean from file, run getHeader()
0328: * expected result: FormatNotFoundException is thrown
0329: *
0330: * 3.create file from random content, run getHeader()
0331: * expected result: FormatSyntaxException
0332: *
0333: * 4.use static byte array to create BMP file (contains unsupported version) and run getHeader()
0334: * expected result: MediaFormatException
0335: *
0336: *
0337: *
0338: * </pre>
0339: */
0340: public void testEMB004() throws IOException, MediaException {
0341: // name of a jpg sample
0342: String mediaJpg = embTestConfig.getJpgPictureName2();
0343: // full path of the jpg sample
0344: String mediaJpgFullPath = embTestConfig.getMediaDir()
0345: + File.separatorChar + mediaJpg;
0346: File mediaJpegFile = new File(mediaJpgFullPath);
0347:
0348: File file1024Byte = new File(embTestConfig.getMediaDir()
0349: + File.separatorChar + "Generated" + File.separatorChar
0350: + "test.xxx");
0351: file1024Byte.createNewFile();
0352: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0353: file1024Byte);
0354: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0355: file1024ByteOutputStream.write(testArray);
0356: file1024ByteOutputStream.close();
0357:
0358: // register media format
0359: MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
0360: formatRegistry.rebind("jpg", new JpegFormat());
0361: formatRegistry.rebind("bmp", new BmpFormat());
0362: try {
0363: formatRegistry.unbind("xxx");
0364: } catch (Exception e) {
0365: // do nothing
0366: }
0367: Media testInstance = null;
0368: //
0369: // test1
0370: //
0371: testInstance = createMedia(mediaJpegFile, "jpg");
0372: InputStream in = new FileInputStream(mediaJpegFile);
0373: MediaHeader testHeader = testInstance.getHeader();
0374: assertTrue("test 1: ", testHeader.equals(testInstance
0375: .getFormat().extractHeader(in)));
0376: testTrace("test 1 passed");
0377: in.close();
0378: //
0379: // test2
0380: //
0381: try {
0382: testInstance = createMedia(file1024Byte, null);
0383: testInstance.getHeader();
0384: fail("test 2: Should throw a FormatNotFoundException");
0385: } catch (FormatNotFoundException e) {
0386: testTrace("test 2 passed");
0387: } catch (Exception e) {
0388: fail("test 2: Should throw a FormatNotFoundException but threw "
0389: + e.toString());
0390: }
0391: //
0392: // test 3
0393: //
0394: File file512Byte = new File(embTestConfig.getMediaDir()
0395: + File.separatorChar + "Generated" + File.separatorChar
0396: + "test.jpg");
0397: file512Byte.createNewFile();
0398: FileOutputStream file512ByteOutputStream = new FileOutputStream(
0399: file512Byte);
0400: byte[] randomArray = EMBStaticHelper.createRandomByteArray(512);
0401: file512ByteOutputStream.write(randomArray);
0402: file512ByteOutputStream.close();
0403: try {
0404: testInstance = createMedia(file512Byte, null);
0405: testInstance.getHeader();
0406: fail("test 3: Should throw a FormatSyntaxException");
0407: } catch (FormatSyntaxException e) {
0408: testTrace("test 3 passed");
0409: } catch (Exception e) {
0410: fail("test 3: Should throw a FormatSyntaxException but threw "
0411: + e.toString());
0412: }
0413: //
0414: // test 4
0415: //
0416: byte[] corruptedBmpByteArray = { 76, 86, 70, 43, 0, 0, 0, 0, 0,
0417: 0, 54, 4, 0, 0, 40, 0, 0, 0, 100, 0, 0, 0, 100, 0, 0,
0418: 0, 1, 0, 8, 0, 0, 0, 0, 0, 16, 39, 0, 0, 0, 0, 0, 0, 0,
0419: 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0420: 1, 0, 2, 2, 2, 0, 3, 3, 3, 0, 4, 4, 4, 0, 5, 5, 5, 0,
0421: 6, 6, 6, 7, 7, 7, 0, 8, 8, 8, 0, 9, 9, 9, 0, 1, 10, 10,
0422: 0, 11, 11 };
0423: testInstance = createMedia(new ByteArrayInputStream(
0424: corruptedBmpByteArray), null, "test.bmp");
0425: try {
0426: testInstance.getHeader();
0427: fail("test 4: Should throw a MediaFormatException");
0428: } catch (MediaFormatException e) {
0429: testTrace("test 4 passed");
0430: } catch (Exception e) {
0431: fail("test 4: Should throw a MediaFormatException but threw "
0432: + e.toString());
0433: }
0434:
0435: succeed();
0436: }
0437:
0438: /**
0439: * <pre>
0440: *
0441: *
0442: *
0443: * Testcase Name: getMimeType()
0444: * Testcase Number: EMB005
0445: *
0446: * setup:
0447: *
0448: * test procedure:
0449: * 1.rebind("jpg", new JpegFormat())
0450: * create random content file with name test.jpg, create MediaBean from file with mimetype null, run getMimeType()
0451: * expected result: result equals getFormat().getDefaultMimeType()
0452: *
0453: * 2.create random content file with name test.jpg, create MediaBean from file with mimetype "test", run getMimeType()
0454: * expected result: result is "test"
0455: *
0456: * 3.create random content file with name test.xxx and create MediaBean from file with null mimetype, run getMimeType()
0457: * expected result: FormatNotFoundException
0458: *
0459: *
0460: *
0461: * </pre>
0462: */
0463: public void testEMB005() throws IOException, MediaException {
0464: // random file
0465: File file1024Byte = new File(embTestConfig.getMediaDir()
0466: + File.separatorChar + "Generated" + File.separatorChar
0467: + "test.jpg");
0468: file1024Byte.createNewFile();
0469: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0470: file1024Byte);
0471: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0472: file1024ByteOutputStream.write(testArray);
0473: file1024ByteOutputStream.close();
0474:
0475: // register media format
0476: javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
0477: formatRegistry.rebind("jpg", new JpegFormat());
0478:
0479: // test instance
0480: Media testInstance = null;
0481: //
0482: // test 1
0483: //
0484: testInstance = createMedia(file1024Byte, null);
0485: assertEquals("test1: ", testInstance.getMimeType(),
0486: testInstance.getFormat().getDefaultMimeType());
0487: testTrace("test 1 passed");
0488: //
0489: // test 2
0490: //
0491: testInstance = createMedia(file1024Byte, "test");
0492: assertEquals("test 2: ", "test", testInstance.getMimeType());
0493: testTrace("test 2 passed");
0494: //
0495: // test 3
0496: //
0497: File testFile = new File(embTestConfig.getMediaDir()
0498: + File.separatorChar + "Generated" + File.separatorChar
0499: + "test.xxx");
0500: testFile.createNewFile();
0501: FileOutputStream testFileOutputStream = new FileOutputStream(
0502: testFile);
0503: testFileOutputStream.write(testArray);
0504: testFileOutputStream.close();
0505: try {
0506: testInstance = createMedia(testFile, null);
0507: fail("test 3: Should throw a FormatNotFoundException");
0508: } catch (FormatNotFoundException e) {
0509: testTrace("test 3 passed");
0510: } catch (Exception e) {
0511: fail("test 3: Should throw a FormatNotFoundException but threw "
0512: + e.toString());
0513: }
0514:
0515: succeed();
0516: }
0517:
0518: /**
0519: * <pre>
0520: *
0521: *
0522: *
0523: * Testcase Name: getName()
0524: * Testcase Number: EMB006
0525: *
0526: * setup:
0527: *
0528: * test procedure:
0529: * 1.rebind("jpg", new JpegFormat()), create random content file with name test.jpg, create MediaBean from file with mimetype null, run getName()
0530: * expected result: result is not null and contains a '.'
0531: *
0532: * 2.create MediaBean from InputStream with null mimetype and random name, call getName
0533: * expected result: name matches original name
0534: *
0535: *
0536: *
0537: * </pre>
0538: */
0539: public void testEMB006() throws IOException, MediaException {
0540: // random file
0541: File file1024Byte = new File(embTestConfig.getMediaDir()
0542: + File.separatorChar + "Generated" + File.separatorChar
0543: + "test.jpg");
0544: file1024Byte.createNewFile();
0545: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0546: file1024Byte);
0547: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0548: file1024ByteOutputStream.write(testArray);
0549: file1024ByteOutputStream.close();
0550:
0551: // register media format
0552: MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
0553: formatRegistry.rebind("jpg", new JpegFormat());
0554: // test instance
0555: Media testInstance = null;
0556: //
0557: // test 1
0558: //
0559: testInstance = createMedia(file1024Byte, null);
0560: assertNotNull(testInstance.getName());
0561: assertTrue("test 1: name does not have a file extension",
0562: testInstance.getName().indexOf(".") != -1);
0563: testTrace("test 1 passed");
0564: //
0565: // test 2
0566: //
0567: String name = "testMedia.jpg";
0568: testInstance = createMedia(new FileInputStream(file1024Byte),
0569: null, name);
0570: assertEquals("test 2: name does not match", name, testInstance
0571: .getName());
0572: testTrace("test 2 passed");
0573:
0574: succeed();
0575: }
0576:
0577: /**
0578: * <pre>
0579: *
0580: *
0581: *
0582: * Testcase Name: getProxy()
0583: * Testcase Number: EMB007
0584: *
0585: * setup:
0586: *
0587: * test procedure:
0588: * 1.create random content file with name test.jpg, create MediaBean from file, call getProxy()
0589: * expected result: not null
0590: *
0591: *
0592: *
0593: * </pre>
0594: */
0595: public void testEMB007() throws MediaException, IOException {
0596: // random file
0597: String jpgFilePath = embTestConfig.getMediaDir()
0598: + File.separatorChar
0599: + embTestConfig.getJpgPictureName1();
0600: File jpgFile = new File(jpgFilePath);
0601: // register media format
0602: javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
0603: formatRegistry.rebind("jpg", new JpegFormat());
0604: // test instance
0605: Media testInstance = null;
0606: //
0607: // test 1
0608: //
0609: testInstance = createMedia(jpgFile, "image/jpeg");
0610: assertNotNull("test 1: proxy is null", testInstance.getProxy());
0611: testTrace("test 1 passed");
0612:
0613: succeed();
0614: }
0615:
0616: /**
0617: * <pre>
0618: *
0619: *
0620: *
0621: * Testcase Name: getSize()
0622: * Testcase Number: EMB008
0623: *
0624: * setup:
0625: * test procedure:
0626: * 1.create random content file with name test.jpg and content size 1024, create MediaBean from file, run getSize()
0627: * expected result: result is 1024
0628: *
0629: *
0630: *
0631: * </pre>
0632: */
0633: public void testEMB008() throws IOException, MediaException,
0634: Exception {
0635: // random file
0636: File file1024Byte = new File(embTestConfig.getMediaDir()
0637: + File.separatorChar + "Generated" + File.separatorChar
0638: + "test.jpg");
0639: file1024Byte.createNewFile();
0640: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0641: file1024Byte);
0642: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0643: file1024ByteOutputStream.write(testArray);
0644: file1024ByteOutputStream.close();
0645: // register media format
0646: javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
0647: formatRegistry.rebind("jpg", new JpegFormat());
0648: // test instance
0649: Media testInstance;
0650: //
0651: // test1
0652: //
0653: testInstance = createMedia(file1024Byte, null);
0654: assertEquals("test1 : ", 1024, testInstance.getSize());
0655: testTrace("test 1 passed");
0656: succeed();
0657: }
0658:
0659: /**
0660: * <pre>
0661: *
0662: *
0663: *
0664: * Testcase Name: readContent(long position, byte[] buffer)
0665: * Testcase Number: EMB009
0666: *
0667: * setup: create file (1024 byte), rebind("test", new GenericMediaFormat()), create MediaBean
0668: * create byte array buffer of size 1024 bytes
0669: *
0670: * test procedure:
0671: * 1.call readContent(-1,testBuffer)
0672: * expected result: IndexOutOfBoundsException
0673: *
0674: * 2.call readContent(1025,testBuffer)
0675: * expected result: IndexOutOfBoundsException
0676: *
0677: * 3.call readContent(0,null)
0678: * expected result: NullPointerException
0679: *
0680: * 4.call readContent(10,testBuffer) with buffer size 100 bytes
0681: * compare to media content offset 10, length of return value
0682: * expected result: same content
0683: *
0684: * 5.call readContent(1024,testBuffer)
0685: * expected result: -1 returned with empty buffer
0686: *
0687: * 6.create testInstance from nonexistent file and call readContent(0,buffer)
0688: * expected result: ContentAccessException
0689: *
0690: * 7.create testInstance of size 100 bytes and buffer of size 1024 bytes
0691: * call readContent(0,testBuffer) and compare content of media and buffer
0692: * expected result: buffer partially filled (return value is the length)
0693: *
0694: *
0695: *
0696: * </pre>
0697: */
0698: public void testEMB009() throws IOException, MediaException,
0699: Exception {
0700: //
0701: // Initialize the testcase
0702: //
0703: File file1024Byte = new File(embTestConfig.getMediaDir()
0704: + File.separatorChar + "Generated" + File.separatorChar
0705: + "testFile1024Byte.test");
0706: file1024Byte.createNewFile();
0707: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0708: file1024Byte);
0709: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0710: file1024ByteOutputStream.write(testArray);
0711: file1024ByteOutputStream.close();
0712: // register media format
0713: javax.emb.MediaFormatRegistry formatRegistry = javax.emb.MediaFormatRegistry.SINGLETON;
0714: formatRegistry.rebind("test", new GenericMediaFormat());
0715: Media testInstance = createMedia(file1024Byte, "test");
0716: byte[] testBuffer = new byte[1024];
0717: //
0718: // test 1
0719: //
0720: try {
0721: testInstance.readContent(-1, testBuffer);
0722: fail("test 1: Should throw an IndexOutOfBoundsException");
0723: } catch (IndexOutOfBoundsException e) {
0724: testTrace("test 1 passed");
0725: } catch (Exception e) {
0726: fail("test 1: Should throw an IndexOutOfBoundsException but threw "
0727: + e.toString());
0728: }
0729: //
0730: // test 2
0731: //
0732: try {
0733: testInstance.readContent(1025, testBuffer);
0734: fail("test 2: Should throw an IndexOutOfBoundsException");
0735: } catch (IndexOutOfBoundsException e) {
0736: testTrace("test 2 passed");
0737: } catch (Exception e) {
0738: fail("test 2: Should throw an IndexOutOfBoundsException but threw "
0739: + e.toString());
0740: }
0741: //
0742: // test 3
0743: //
0744: try {
0745: testInstance.readContent(0, null);
0746: fail("test 3: Should throw an NullPointerException");
0747: } catch (NullPointerException e) {
0748: testTrace("test 3 passed");
0749: } catch (Exception e) {
0750: fail("test 3: Should throw an NullPointerException but threw "
0751: + e.toString());
0752: }
0753: //
0754: // test 4
0755: //
0756: byte[] buffer100Byte = new byte[100];
0757: int amountCopied = testInstance.readContent(10, buffer100Byte);
0758: FileInputStream file1024ByteInputStream = new FileInputStream(
0759: file1024Byte);
0760: byte[] fileArray = new byte[amountCopied];
0761: file1024ByteInputStream.skip(10);
0762: int amountRead = file1024ByteInputStream.read(fileArray, 0,
0763: amountCopied);
0764: if (amountRead != amountCopied)
0765: throw new ContentAccessException();
0766: file1024ByteInputStream.close();
0767: for (int i = 0; i < amountCopied; i++) {
0768: assertEquals("test 4: ", buffer100Byte[i], fileArray[i]);
0769: }
0770: testTrace("test 4 passed");
0771: //
0772: // test 5
0773: //
0774: amountCopied = testInstance.readContent(1024, testBuffer);
0775: assertEquals("test 5: ", -1, amountCopied);
0776: testTrace("test 5 passed");
0777: //
0778: // test 6
0779: //
0780: try {
0781: testInstance = createMedia(
0782: new File("nonexistentFile.test"), "test");
0783: testInstance.readContent(0, testBuffer);
0784: fail("test 6: Should throw a ContentAccessException");
0785: } catch (ContentAccessException e) {
0786: testTrace("test 6 passed");
0787: } catch (Exception e) {
0788: fail("test 6: Should throw a ContentAccessException but threw "
0789: + e.toString());
0790: }
0791: //
0792: // test 7
0793: //
0794: File file100Byte = new File(embTestConfig.getMediaDir()
0795: + File.separatorChar + "Generated" + File.separatorChar
0796: + "testFile100Byte.test");
0797: file100Byte.createNewFile();
0798: FileOutputStream file100ByteOutputStream = new FileOutputStream(
0799: file100Byte);
0800: byte[] randomTestArray = EMBStaticHelper
0801: .createRandomByteArray(100);
0802: file100ByteOutputStream.write(randomTestArray);
0803: file100ByteOutputStream.close();
0804: testInstance = createMedia(file100Byte, "test");
0805: amountCopied = testInstance.readContent(0, testBuffer);
0806: for (int j = 0; j < amountCopied; j++) {
0807: assertEquals("test 7: ", testBuffer[j], randomTestArray[j]);
0808: }
0809: testTrace("test 7 passed");
0810:
0811: succeed();
0812: }
0813:
0814: /**
0815: * <pre>
0816: *
0817: *
0818: *
0819: * Testcase Name: readContent(long position, byte[] buffer, int offset, int length)
0820: * Testcase Number: EMB010
0821: *
0822: * setup: create file (1024 byte), rebind("test", new GenericMediaFormat()), create MediaBean
0823: * create byte array buffer of size 1024 bytes
0824: *
0825: * test procedure:
0826: * 1.call readContent(-1,testBuffer,0,1024)
0827: * expected result: IndexOutOfBoundsException
0828: *
0829: * 2.call readContent(1025,testBuffer,0,1024)
0830: * expected result: IndexOutOfBoundsException
0831: *
0832: * 3.call readContent(0,testBuffer,-1,1024)
0833: * expected result: IndexOutOfBoundsException
0834: *
0835: * 4.call readContent(0,testBuffer,1025,1024)
0836: * expected result: IndexOutOfBoundsException
0837: *
0838: * 5.call readContent(0,testBuffer,0,-1)
0839: * expected result: NegativeIndexArrayException
0840: *
0841: * 6.call readContent(0,null,0,1024)
0842: * expected result: NullPointerException
0843: *
0844: * 7.create testInstance from nonexistent file and call readContent(0,testBuffer,0,1024)
0845: * expected result: ContentAccessException
0846: *
0847: * 8.call readContent(0,testBuffer,0,1024) (loop to fill buffer)
0848: * compare with file content
0849: * expected result: same content
0850: *
0851: * 9.call readContent(512,testBuffer,0,1024)
0852: * expected result: buffer partially filled with 512 bytes matching last 512 bytes
0853: * of media content (could be less if nonblocking)
0854: *
0855: * 10.call readContent(1024,testBuffer,0,1024)
0856: * expected result: return -1 with empty buffer
0857: *
0858: * 11.call readContent(0,testBuffer,100,924), compare buffer offset 100 with media content
0859: * (use return value as length)
0860: * expected result: same content
0861: *
0862: * 12.call readContent(200,testBuffer,100,100), compare buffer offset 100 with file offset 200
0863: * (use return value as length)
0864: * expected result: same content
0865: *
0866: * 13.call readContent(0,testBuffer,0,1048), compare buffer with file content
0867: * expected result: same content and no exceptions
0868: *
0869: *
0870: *
0871: * </pre>
0872: */
0873: public void testEMB010() throws IOException, MediaException,
0874: Exception {
0875: //
0876: // Initialize the testcase
0877: //
0878: File file1024Byte = new File(embTestConfig.getMediaDir()
0879: + File.separatorChar + "Generated" + File.separatorChar
0880: + "testFile1024Byte.test");
0881: file1024Byte.createNewFile();
0882: FileOutputStream file1024ByteOutputStream = new FileOutputStream(
0883: file1024Byte);
0884: byte[] testArray = EMBStaticHelper.createRandomByteArray(1024);
0885: file1024ByteOutputStream.write(testArray);
0886: file1024ByteOutputStream.close();
0887: // register media format
0888: javax.emb.MediaFormatRegistry formatRegistry = javax.emb.MediaFormatRegistry.SINGLETON;
0889: formatRegistry.rebind("test", new GenericMediaFormat());
0890: Media testInstance = createMedia(file1024Byte, "test");
0891: byte[] testBuffer = new byte[1024];
0892: //
0893: // test 1
0894: //
0895: try {
0896: testInstance.readContent(-1, testBuffer, 0, 1024);
0897: fail("test 1: Should throw an IndexOutOfBoundsException");
0898: } catch (IndexOutOfBoundsException e) {
0899: testTrace("test 1 passed");
0900: } catch (Exception e) {
0901: fail("test 1: Should throw an IndexOutOfBoundsException but threw "
0902: + e.toString());
0903: }
0904: //
0905: // test 2
0906: //
0907: try {
0908: testInstance.readContent(1025, testBuffer, 0, 1024);
0909: fail("test 2: Should throw an IndexOutOfBoundsException");
0910: } catch (IndexOutOfBoundsException e) {
0911: testTrace("test 2 passed");
0912: } catch (Exception e) {
0913: fail("test 2: Should throw an IndexOutOfBoundsException but threw "
0914: + e.toString());
0915: }
0916: //
0917: // test 3
0918: //
0919: try {
0920: testInstance.readContent(0, testBuffer, -1, 1024);
0921: fail("test 3: Should throw an IndexOutOfBoundsException");
0922: } catch (IndexOutOfBoundsException e) {
0923: testTrace("test 3 passed");
0924: } catch (Exception e) {
0925: fail("test 3: Should throw an IndexOutOfBoundsException but threw "
0926: + e.toString());
0927: }
0928: //
0929: // test 4
0930: //
0931: try {
0932: testInstance.readContent(0, testBuffer, 1025, 1024);
0933: fail("test 4: Should throw an IndexOutOfBoundsException");
0934: } catch (IndexOutOfBoundsException e) {
0935: testTrace("test 4 passed");
0936: } catch (Exception e) {
0937: fail("test 4: Should throw an IndexOutOfBoundsException but threw "
0938: + e.toString());
0939: }
0940: //
0941: // test 5
0942: //
0943: try {
0944: testInstance.readContent(0, testBuffer, 0, -1);
0945: fail("test 5: Should throw a NegativeIndexArrayException");
0946: } catch (NegativeArraySizeException e) {
0947: testTrace("test 5 passed");
0948: } catch (Exception e) {
0949: fail("test 5: Should throw a NegativeIndexArrayExcepton but threw "
0950: + e.toString());
0951: }
0952: //
0953: // test 6
0954: //
0955: try {
0956: testInstance.readContent(0, null, 0, 1024);
0957: fail("test 6: Should throw a NullPointerException");
0958: } catch (NullPointerException e) {
0959: testTrace("test 6 passed");
0960: } catch (Exception e) {
0961: fail("test 6: Should throw a NullPointerException but threw "
0962: + e.toString());
0963: }
0964: //
0965: // test 7
0966: //
0967: try {
0968: testInstance = createMedia(
0969: new File("nonexistentFile.test"), "test");
0970: testInstance.readContent(0, testBuffer, 0, 1024);
0971: fail("test 7: Should throw a ContentAccessException");
0972: } catch (ContentAccessException e) {
0973: testTrace("test 7 passed");
0974: } catch (Exception e) {
0975: fail("test 7: Should throw a NullPointerException but threw "
0976: + e.toString());
0977: }
0978: //
0979: // test 8
0980: //
0981: int totalCopied = 0;
0982: int amountCopied = 0;
0983: while (totalCopied != 1024) {
0984: amountCopied = testInstance.readContent(totalCopied,
0985: testBuffer, totalCopied, 1024 - totalCopied);
0986: totalCopied = totalCopied + amountCopied;
0987: }
0988: assertTrue("test 8: ", java.util.Arrays.equals(testArray,
0989: testBuffer));
0990: testTrace("test 8 passed");
0991: //
0992: // test 9
0993: //
0994: amountCopied = testInstance.readContent(512, testBuffer, 0,
0995: 1024);
0996: for (int i = 0; i < amountCopied; i++) {
0997: assertEquals("test 9: ", testArray[i + 512], testBuffer[i]);
0998: }
0999: testTrace("test 9 passed");
1000: //
1001: // test 10
1002: //
1003: amountCopied = testInstance.readContent(1024, testBuffer, 0,
1004: 1024);
1005: assertEquals("test 10: ", -1, amountCopied);
1006: testTrace("test 10 passed");
1007: //
1008: // test 11
1009: //
1010: amountCopied = testInstance
1011: .readContent(0, testBuffer, 100, 924);
1012: for (int j = 0; j < amountCopied; j++) {
1013: assertEquals("test 11: ", testArray[j], testBuffer[j + 100]);
1014: }
1015: testTrace("test 11 passed");
1016: //
1017: // test 12
1018: //
1019: amountCopied = testInstance.readContent(200, testBuffer, 100,
1020: 100);
1021: for (int k = 0; k < amountCopied; k++) {
1022: assertEquals("test 12: ", testArray[k + 200],
1023: testBuffer[k + 100]);
1024: }
1025: testTrace("test 12 passed");
1026: //
1027: // test 13
1028: //
1029: amountCopied = testInstance.readContent(0, testBuffer, 0, 1048);
1030: for (int m = 0; m < amountCopied; m++) {
1031: assertEquals("test 13: ", testArray[m], testBuffer[m]);
1032: }
1033: testTrace("test 13 passed");
1034:
1035: succeed();
1036: }
1037: }
|