001: package com.ibm.emb.test;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.util.StringTokenizer;
008:
009: import javax.emb.MediaException;
010: import javax.emb.MediaFormat;
011: import javax.emb.MediaFormatRegistry;
012: import javax.emb.MediaHeader;
013:
014: import junit.framework.Test;
015: import junit.framework.TestSuite;
016:
017: import com.ibm.emb.junit.EMBStaticHelper;
018: import com.ibm.emb.junit.EMBTestCaseBase;
019:
020: /**
021: * <pre>
022: *
023: *
024: *
025: * Line Item: MFB API
026: * Subcategory 1: javax.emb
027: * Subcategory 2: MediaHeader
028: *
029: *
030: *
031: * </pre>
032: */
033: public abstract class MediaHeaderTest extends EMBTestCaseBase {
034:
035: public MediaHeaderTest(String name) throws MediaException {
036: super (name);
037: }
038:
039: /**
040: * stdSuite method of this test class instantiates all tests necesssary to
041: * for the standard compliance tests
042: * @param String complete class Name of the implementor of the abstract test
043: */
044: public static Test stdSuite(String TestCaseImplementor) {
045: TestSuite suite = new TestSuite(TestCaseImplementor);
046: try {
047: Class implementorClass = Class.forName(TestCaseImplementor);
048: java.lang.reflect.Constructor implementorConstructor = implementorClass
049: .getDeclaredConstructor(new Class[] { String.class });
050: suite.addTest((Test) implementorConstructor
051: .newInstance(new String[] { "testEMB037" }));
052: suite.addTest((Test) implementorConstructor
053: .newInstance(new String[] { "testEMB038" }));
054: } catch (java.lang.reflect.InvocationTargetException ex) {
055: String msg = "error dynamically creating "
056: + TestCaseImplementor + " " + ex.toString();
057: EMBStaticHelper.testTrace(msg);
058: throw new RuntimeException(msg);
059: } catch (ClassNotFoundException ex) {
060: String msg = "Class not found " + TestCaseImplementor + " "
061: + ex.toString();
062: EMBStaticHelper.testTrace(msg);
063: throw new RuntimeException(msg);
064: } catch (NoSuchMethodException ex) {
065: String msg = "No such method in " + TestCaseImplementor
066: + " " + ex.toString();
067: EMBStaticHelper.testTrace(msg);
068: throw new RuntimeException(msg);
069: } catch (InstantiationException ex) {
070: String msg = TestCaseImplementor + " " + ex.toString();
071: EMBStaticHelper.testTrace(msg);
072: throw new RuntimeException(msg);
073: } catch (IllegalAccessException ex) {
074: String msg = TestCaseImplementor + " " + ex.toString();
075: EMBStaticHelper.testTrace(msg);
076: throw new RuntimeException(msg);
077: }
078: return suite;
079: }
080:
081: /**
082: * abstract method to create an instance of mediaHeader with no parameters
083: * this method needs to be implemented in the test subclass and will create
084: * an instance of the concrete implementation of the class under test
085: */
086: public abstract MediaHeader createMediaHeader()
087: throws MediaException, IOException;
088:
089: /**
090: * <pre>
091: *
092: *
093: *
094: * Testcase Name: getField(String fieldName)
095: * Testcase Number: EMB037
096: *
097: * setup: access file containing a list of all implemented Media formats and sample files
098: *
099: * test procedure:
100: * 1.for each format, call extractHeader().getField(null)
101: * expected result: NullPointerException
102: *
103: * 2.for each format, call extractHeader().getField("%$&//&%?%&")
104: * expected result: null
105: *
106: * 3.for each format, call extractHeader().getFieldNames()
107: * for each field, call getField()
108: * expected result: not null
109: *
110: *
111: *
112: * </pre>
113: */
114: public void testEMB037() throws MediaException, IOException {
115:
116: // retrieve Media formats
117: String allMediaFormats = embTestConfig.getAllMediaFormats();
118: allMediaFormats = allMediaFormats.trim();
119:
120: // token individual Media formats
121: StringTokenizer mediaTokenizer = new StringTokenizer(
122: allMediaFormats, ";");
123: String formatInfo = null;
124: StringTokenizer formatInfoTokenizer = null;
125:
126: while (mediaTokenizer.hasMoreTokens()) {
127: formatInfo = mediaTokenizer.nextToken();
128: formatInfoTokenizer = new StringTokenizer(formatInfo);
129:
130: assertEquals(
131: "test initialization: allMediaFormats property not properly formatted",
132: 3, formatInfoTokenizer.countTokens());
133:
134: String fullClassName = formatInfoTokenizer.nextToken();
135: String fileExtension = formatInfoTokenizer.nextToken();
136: String mediaFileName = formatInfoTokenizer.nextToken();
137:
138: String mediaFullPath = embTestConfig.getMediaDir()
139: + File.separator + mediaFileName;
140:
141: try {
142: File mediaFile = new File(mediaFullPath);
143: InputStream mediaFileInputStream = new FileInputStream(
144: mediaFile);
145: MediaFormat mediaFormat = (MediaFormat) Class.forName(
146: fullClassName).newInstance();
147: MediaHeader testInstance = mediaFormat
148: .extractHeader(mediaFileInputStream);
149: mediaFileInputStream.close();
150: MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
151: formatRegistry.rebind(fileExtension,
152: (MediaFormat) Class.forName(fullClassName)
153: .newInstance());
154:
155: //
156: // test 1
157: //
158: try {
159: testInstance.getField(null);
160: fail("[" + testInstance.getClass().getName()
161: + "] test 1: "
162: + "Should throw a NullPointerException");
163: } catch (NullPointerException e) {
164: testTrace("[" + testInstance.getClass().getName()
165: + "] test 1 passed");
166: } catch (Exception e) {
167: fail("["
168: + testInstance.getClass().getName()
169: + "] test 1: "
170: + "Should throw a NullPointerException but threw "
171: + e.toString());
172: }
173: //
174: // test 2
175: //
176: assertNull("[" + testInstance.getClass().getName()
177: + "] test 2: ", testInstance
178: .getField("%$&//&%?%&"));
179: testTrace("[" + testInstance.getClass().getName()
180: + "] test 2 passed");
181: //
182: // test 3
183: //
184: String[] fieldNames = testInstance.getFieldNames();
185: for (int i = 0; i < fieldNames.length; i++) {
186: assertNotNull("["
187: + testInstance.getClass().getName()
188: + "] test 3: ", testInstance
189: .getField(fieldNames[i]));
190: }
191: testTrace("[" + testInstance.getClass().getName()
192: + "] test 3 passed");
193:
194: } catch (ClassNotFoundException e) {
195: fail("Class not found: " + e.getMessage());
196: } catch (IllegalAccessException e) {
197: fail("Illegal access: " + e.getMessage());
198: } catch (InstantiationException e) {
199: fail("Instantiation exception; " + e.getMessage());
200: }
201: }
202: succeed();
203: }
204:
205: /**
206: * <pre>
207: *
208: *
209: *
210: * Testcase Name: getFieldNames()
211: * Testcase Number: EMB038
212: *
213: * setup: access file containing a list of all implemented Media formats and sample files
214: *
215: * test procedure:
216: * 1.for each format, call extractHeader.getFieldNames()
217: * expected result: not null
218: *
219: *
220: *
221: * </pre>
222: */
223: public void testEMB038() throws MediaException, IOException {
224: // retrieve Media formats
225: String allMediaFormats = embTestConfig.getAllMediaFormats();
226: allMediaFormats = allMediaFormats.trim();
227:
228: // token individual Media formats
229: StringTokenizer mediaTokenizer = new StringTokenizer(
230: allMediaFormats, ";");
231: String formatInfo = null;
232: StringTokenizer formatInfoTokenizer = null;
233:
234: while (mediaTokenizer.hasMoreTokens()) {
235: formatInfo = mediaTokenizer.nextToken();
236: formatInfoTokenizer = new StringTokenizer(formatInfo);
237:
238: assertEquals("test initialization: ", 3,
239: formatInfoTokenizer.countTokens());
240:
241: String fullClassName = formatInfoTokenizer.nextToken();
242: String fileExtension = formatInfoTokenizer.nextToken();
243: String mediaFileName = formatInfoTokenizer.nextToken();
244:
245: String mediaFullPath = embTestConfig.getMediaDir()
246: + File.separator + mediaFileName;
247:
248: try {
249: File mediaFile = new File(mediaFullPath);
250: InputStream mediaFileInputStream = new FileInputStream(
251: mediaFile);
252: MediaFormat mediaFormat = (MediaFormat) Class.forName(
253: fullClassName).newInstance();
254: MediaHeader testInstance = mediaFormat
255: .extractHeader(mediaFileInputStream);
256: mediaFileInputStream.close();
257: MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
258: formatRegistry.rebind(fileExtension,
259: (MediaFormat) Class.forName(fullClassName)
260: .newInstance());
261:
262: //
263: // test 1
264: //
265: assertNotNull("[" + testInstance.getClass().getName()
266: + "] test 1: ", testInstance.getFieldNames());
267: testTrace("[" + testInstance.getClass().getName()
268: + "] test 1 passed");
269:
270: } catch (ClassNotFoundException e) {
271: fail("Class not found: " + e.getMessage());
272: } catch (IllegalAccessException e) {
273: fail("Illegal access: " + e.getMessage());
274: } catch (InstantiationException e) {
275: fail("Instantiation exception; " + e.getMessage());
276: }
277: }
278:
279: succeed();
280: }
281:
282: }
|