001: package com.ibm.emb.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileNotFoundException;
008: import java.io.IOException;
009: import java.io.InputStream;
010: import java.util.StringTokenizer;
011:
012: import javax.emb.MediaBean;
013: import javax.emb.MediaConverter;
014: import javax.emb.MediaConverterSpec;
015: import javax.emb.MediaException;
016: import javax.emb.MediaFormat;
017: import javax.emb.MediaFormatRegistry;
018:
019: import junit.framework.Test;
020: import junit.framework.TestSuite;
021:
022: import com.ibm.emb.junit.EMBStaticHelper;
023: import com.ibm.emb.junit.EMBTestCaseBase;
024:
025: /**
026: * <pre>
027: *
028: *
029: *
030: * Line Item: MFB API
031: * Subcategory 1: javax.emb
032: * Subcategory 2: MediaConverter
033: *
034: * This abstract class provides test methods for all methods described in the interface
035: * MediaConverter.
036: * For testing implementations of this interface one needs to create a Tester
037: * class which extends this abstract class.
038: *
039: *
040: *
041: * </pre>
042: */
043: public abstract class MediaConverterTest extends EMBTestCaseBase {
044:
045: public MediaConverterTest(String name) throws MediaException {
046: super (name);
047: }
048:
049: /**
050: * stdSuite method of this test class instantiates all tests necesssary to
051: * for the standard compliance tests
052: * @param String complete class Name of the implementor of the abstract test
053: */
054: public static Test stdSuite(String TestCaseImplementor) {
055: TestSuite suite = new TestSuite(TestCaseImplementor);
056: try {
057: Class implementorClass = Class.forName(TestCaseImplementor);
058: java.lang.reflect.Constructor implementorConstructor = implementorClass
059: .getDeclaredConstructor(new Class[] { String.class });
060: suite.addTest((Test) implementorConstructor
061: .newInstance(new String[] { "testEMB039" }));
062: suite.addTest((Test) implementorConstructor
063: .newInstance(new String[] { "testEMB040" }));
064: } catch (java.lang.reflect.InvocationTargetException ex) {
065: String msg = "error dynamically creating "
066: + TestCaseImplementor + " " + ex.toString();
067: EMBStaticHelper.testTrace(msg);
068: throw new RuntimeException(msg);
069: } catch (ClassNotFoundException ex) {
070: String msg = "Class not found " + TestCaseImplementor + " "
071: + ex.toString();
072: EMBStaticHelper.testTrace(msg);
073: throw new RuntimeException(msg);
074: } catch (NoSuchMethodException ex) {
075: String msg = "No such method in " + TestCaseImplementor
076: + " " + ex.toString();
077: EMBStaticHelper.testTrace(msg);
078: throw new RuntimeException(msg);
079: } catch (InstantiationException ex) {
080: String msg = TestCaseImplementor + " " + ex.toString();
081: EMBStaticHelper.testTrace(msg);
082: throw new RuntimeException(msg);
083: } catch (IllegalAccessException ex) {
084: String msg = TestCaseImplementor + " " + ex.toString();
085: EMBStaticHelper.testTrace(msg);
086: throw new RuntimeException(msg);
087: }
088: return suite;
089: }
090:
091: /**
092: * abstract method to create an instance of MediaConverter this method needs
093: * to be implemented in the test subclass and will create an instance of the
094: * concrete implementation of the class under test
095: */
096: public abstract MediaConverter createMediaConverter()
097: throws MediaException;
098:
099: /**
100: * <pre>
101: *
102: *
103: *
104: * Testcase Name: process(InputStream)
105: * Testcase Number: EMB039
106: *
107: * setup: access file containing a list of all implemented MediaConverterSpecs and sample files
108: *
109: * test procedure:
110: * 1.for each converter spec, call process passing null
111: * expected result: NullPointerException
112: *
113: * 2.for each converter spec, create input stream from matching input file
114: * call process
115: * expected result: not null and no exceptions
116: *
117: *
118: *
119: * </pre>
120: */
121: public void testEMB039() throws MediaException, IOException {
122:
123: // retrieve all Media formats and bind with Media format registry
124: String allMediaFormats = embTestConfig.getAllMediaFormats();
125: allMediaFormats = allMediaFormats.trim();
126: StringTokenizer mediaTokenizer = new StringTokenizer(
127: allMediaFormats, ";");
128: String formatInfo = null;
129: StringTokenizer formatInfoTokenizer = null;
130:
131: String fullClassName = null;
132: String fileExtension = null;
133: String mediaFileName = null;
134:
135: MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
136:
137: while (mediaTokenizer.hasMoreTokens()) {
138: formatInfo = mediaTokenizer.nextToken();
139: formatInfoTokenizer = new StringTokenizer(formatInfo);
140: assertEquals("test initialization: ", 3,
141: formatInfoTokenizer.countTokens());
142: fullClassName = formatInfoTokenizer.nextToken();
143: fileExtension = formatInfoTokenizer.nextToken();
144: mediaFileName = formatInfoTokenizer.nextToken();
145:
146: try {
147: formatRegistry.rebind(fileExtension,
148: (MediaFormat) Class.forName(fullClassName)
149: .newInstance());
150: } catch (ClassNotFoundException e) {
151: fail("Class not found: " + e.getMessage());
152: } catch (IllegalAccessException e) {
153: fail("Illegal access: " + e.getMessage());
154: } catch (InstantiationException e) {
155: fail("Instantiation exception: " + e.getMessage());
156: }
157: }
158:
159: // retrieve Media converter specs
160: String allMediaConverterSpecs = embTestConfig
161: .getAllMediaConverterSpecs();
162: allMediaConverterSpecs = allMediaConverterSpecs.trim();
163:
164: // token individual Media formats
165: StringTokenizer converterSpecsTokenizer = new StringTokenizer(
166: allMediaConverterSpecs, ";");
167: String specInfo = null;
168: StringTokenizer specInfoTokenizer = null;
169:
170: while (converterSpecsTokenizer.hasMoreTokens()) {
171: specInfo = converterSpecsTokenizer.nextToken();
172: specInfoTokenizer = new StringTokenizer(specInfo);
173:
174: assertEquals("test initialization: ", 2, specInfoTokenizer
175: .countTokens());
176:
177: fullClassName = specInfoTokenizer.nextToken();
178: mediaFileName = specInfoTokenizer.nextToken();
179:
180: String mediaFullPath = embTestConfig.getMediaDir()
181: + File.separator + mediaFileName;
182:
183: try {
184: File mediaFile = new File(mediaFullPath);
185: InputStream mediaFileInputStream = new FileInputStream(
186: mediaFile);
187: MediaConverterSpec testSpec = (MediaConverterSpec) Class
188: .forName(fullClassName).newInstance();
189: MediaConverter testInstance = testSpec.getConverter();
190:
191: //
192: // test 1
193: //
194: try {
195: testInstance.process(null);
196: fail("["
197: + fullClassName
198: + "] test 1 should throw NullPointerException");
199: } catch (NullPointerException e) {
200: testTrace("[" + fullClassName + "] test 1 passed");
201: } catch (Exception e) {
202: fail("["
203: + fullClassName
204: + "] test 1: Should throw NullPointerException but threw "
205: + e.toString());
206: }
207: //
208: // test 2
209: //
210: InputStream convertedMediaInputStream = testInstance
211: .process(mediaFileInputStream);
212: MediaBean testMedia = new MediaBean(
213: convertedMediaInputStream, testSpec
214: .getTargetMimeType(), "convertedMedia."
215: + testSpec.getTargetFileExtension());
216: assertNotNull("[" + fullClassName + "] test 2: ",
217: testMedia);
218: assertEquals("[" + fullClassName + "] test 2: ",
219: testMedia.getFormat(), formatRegistry
220: .lookup(testSpec
221: .getTargetFileExtension()));
222: testTrace("[" + fullClassName + "] test 2 passed");
223:
224: } catch (ClassNotFoundException e) {
225: fail("Class not found: " + e.getMessage());
226: } catch (FileNotFoundException e) {
227: fail("File not found: " + e.getMessage());
228: } catch (IllegalAccessException e) {
229: fail("Illegal access: " + e.getMessage());
230: } catch (InstantiationException e) {
231: fail("Instantiation exception: " + e.getMessage());
232: }
233: }
234:
235: succeed();
236: }
237:
238: /**
239: * <pre>
240: *
241: *
242: *
243: * Testcase Name: process(InputStream, OutputStream)
244: * Testcase Number: EMB040
245: *
246: * setup: access file containing a list of all implemented MediaConverterSpecs and sample files
247: *
248: * test procedure:
249: * 1.for each converter spec, call process passing null as input
250: * expected result: NullPointerException
251: *
252: * 2.for each converter spec, call process passing null as output
253: * expected result: NullPointerException
254: *
255: * 3.for each converter spec, create input stream from matching file and call process
256: * expected result: not null and no exception
257: *
258: *
259: *
260: * </pre>
261: */
262: public void testEMB040() throws MediaException, IOException {
263:
264: // retrieve all Media formats and bind with Media format registry
265: String allMediaFormats = embTestConfig.getAllMediaFormats();
266: allMediaFormats = allMediaFormats.trim();
267: StringTokenizer mediaTokenizer = new StringTokenizer(
268: allMediaFormats, ";");
269: String formatInfo = null;
270: StringTokenizer formatInfoTokenizer = null;
271:
272: String fullClassName = null;
273: String fileExtension = null;
274: String mediaFileName = null;
275:
276: javax.emb.MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
277:
278: while (mediaTokenizer.hasMoreTokens()) {
279: formatInfo = mediaTokenizer.nextToken();
280: formatInfoTokenizer = new StringTokenizer(formatInfo);
281: assertEquals("test initialization: ", 3,
282: formatInfoTokenizer.countTokens());
283: fullClassName = formatInfoTokenizer.nextToken();
284: fileExtension = formatInfoTokenizer.nextToken();
285: mediaFileName = formatInfoTokenizer.nextToken();
286:
287: try {
288: formatRegistry.rebind(fileExtension,
289: (MediaFormat) Class.forName(fullClassName)
290: .newInstance());
291: } catch (ClassNotFoundException e) {
292: fail("Class not found: " + e.getMessage());
293: } catch (IllegalAccessException e) {
294: fail("Illegal access: " + e.getMessage());
295: } catch (InstantiationException e) {
296: fail("Instantiation exception: " + e.getMessage());
297: }
298: }
299:
300: // retrieve Media converter specs
301: String allMediaConverterSpecs = embTestConfig
302: .getAllMediaConverterSpecs();
303: allMediaConverterSpecs = allMediaConverterSpecs.trim();
304:
305: // token individual Media formats
306: StringTokenizer converterSpecsTokenizer = new StringTokenizer(
307: allMediaConverterSpecs, ";");
308: String specInfo = null;
309: StringTokenizer specInfoTokenizer = null;
310:
311: while (converterSpecsTokenizer.hasMoreTokens()) {
312: specInfo = converterSpecsTokenizer.nextToken();
313: specInfoTokenizer = new StringTokenizer(specInfo);
314:
315: assertEquals("test initialization: ", 2, specInfoTokenizer
316: .countTokens());
317:
318: fullClassName = specInfoTokenizer.nextToken();
319: mediaFileName = specInfoTokenizer.nextToken();
320:
321: String mediaFullPath = embTestConfig.getMediaDir()
322: + File.separator + mediaFileName;
323:
324: try {
325: File mediaFile = new File(mediaFullPath);
326: InputStream mediaFileInputStream = new FileInputStream(
327: mediaFile);
328: MediaConverterSpec testSpec = (MediaConverterSpec) Class
329: .forName(fullClassName).newInstance();
330: MediaConverter testInstance = testSpec.getConverter();
331: ByteArrayOutputStream convertedMediaOutputStream = new ByteArrayOutputStream();
332: //
333: // test 1
334: //
335: try {
336: testInstance.process(null,
337: convertedMediaOutputStream);
338: fail("["
339: + testSpec
340: + "] test 1: Should throw a NullPointerException");
341: } catch (NullPointerException e) {
342: testTrace("[" + testSpec + "] test 1 passed");
343: } catch (Exception e) {
344: fail("["
345: + testSpec
346: + "] test 1: Should throw a NullPointerException but threw "
347: + e.toString());
348: }
349: //
350: // test 2
351: //
352: try {
353: testInstance.process(mediaFileInputStream, null);
354: fail("["
355: + testSpec
356: + "] test 2: Should throw a NullPointerException");
357: } catch (NullPointerException e) {
358: testTrace("[" + testSpec + "] test 2 passed");
359: } catch (Exception e) {
360: fail("["
361: + testSpec
362: + "] test 2: Should throw NullPointerException but threw "
363: + e.toString());
364: }
365: //
366: // test 3
367: //
368: testInstance.process(mediaFileInputStream,
369: convertedMediaOutputStream);
370: ByteArrayInputStream convertedMediaInputStream = new ByteArrayInputStream(
371: convertedMediaOutputStream.toByteArray());
372: MediaBean testMedia = new MediaBean(
373: convertedMediaInputStream, testSpec
374: .getTargetMimeType(), "convertedMedia."
375: + testSpec.getTargetFileExtension());
376: assertNotNull("[" + fullClassName + "] test 3: ",
377: testMedia);
378: assertEquals("[" + fullClassName + "] test 3: ",
379: testMedia.getFormat(), formatRegistry
380: .lookup(testSpec
381: .getTargetFileExtension()));
382: testTrace("[" + testSpec + "] test 3 passed");
383:
384: } catch (ClassNotFoundException e) {
385: fail("Class not found: " + e.getMessage());
386: } catch (FileNotFoundException e) {
387: fail("File not found: " + e.getMessage());
388: } catch (IllegalAccessException e) {
389: fail("Illegal access: " + e.getMessage());
390: } catch (InstantiationException e) {
391: fail("Instantiation exception: " + e.getMessage());
392: }
393: }
394: succeed();
395: }
396:
397: }
|