001: package com.ibm.emb.test;
002:
003: import java.io.IOException;
004: import java.net.MalformedURLException;
005: import java.rmi.RemoteException;
006: import java.util.Arrays;
007: import java.util.StringTokenizer;
008:
009: import javax.ejb.CreateException;
010: import javax.ejb.FinderException;
011: import javax.ejb.RemoveException;
012: import javax.emb.ListenerVetoException;
013: import javax.emb.MediaException;
014: import javax.emb.MediaListener;
015: import javax.naming.InitialContext;
016: import javax.naming.NamingException;
017: import javax.rmi.PortableRemoteObject;
018:
019: import org.objectweb.jonas.emb.mfb.formats.image.JpegFormat;
020:
021: import com.ibm.emb.junit.EMBStaticHelper;
022: import com.ibm.emb.junit.EMBTestCaseBase;
023: import com.ibm.emb.junit.EmbeddedMediaLocationInfo;
024: import com.ibm.emb.meb.test.ejb.MediaEntityLocalTestDriver;
025: import com.ibm.emb.meb.test.ejb.MediaEntityLocalTestDriverHome;
026:
027: /**
028: * <pre>
029: *
030: *
031: *
032: *
033: * Line Item: MEB API
034: * Subcategory 1: javax.emb
035: * Subcategory 2: MediaListener
036: *
037: *
038: *
039: *
040: * </pre>
041: */
042:
043: public class MediaListenerTest extends EMBTestCaseBase {
044:
045: public MediaListenerTest(String name) throws MediaException {
046: super (name);
047: }
048:
049: private static String MEBTESTER_JNDI = "java:comp/env/ejb/MediaEntityLocalTestDriverHome";
050:
051: private InitialContext ic;
052:
053: private MediaEntityLocalTestDriverHome mebHome;
054:
055: private MediaEntityLocalTestDriver mebTester = null;
056:
057: /**
058: * initialization of test case (called before each test method)
059: */
060: protected void setUp() throws RemoteException, CreateException {
061: if (mebHome == null) {
062: try {
063: ic = new InitialContext();
064: Object ojb = ic.lookup(MEBTESTER_JNDI);
065: mebHome = (MediaEntityLocalTestDriverHome) PortableRemoteObject
066: .narrow(ojb,
067: MediaEntityLocalTestDriverHome.class);
068: } catch (NamingException ne) {
069: System.out
070: .println("NamingException thrown during setup. Msg: "
071: + ne.getMessage());
072: }
073: }
074:
075: // create new instance of MediaEntityLocalTestDriver
076: mebTester = mebHome.create();
077: return;
078: }
079:
080: /**
081: * <pre>
082: *
083: *
084: *
085: *
086: * Testcase Name: handleAboutToChangeMediaEntity(String)
087: * Testcase Number: EMB142
088: *
089: * setup: create Media entity me and retrieve list of media listeners
090: * for each, call me1.addListener(listener) and run the following tests
091: *
092: * test procedure:
093: * 1.call handleAboutToChangeMediaEntity(null)
094: * expected result: NullPointerException
095: *
096: * 2.call me.setContent(valid content)
097: * expected result: no exception or ListenerVetoException
098: *
099: * 3.call me.setDescription(random string)
100: * expected result: no exception or ListenerVetoException
101: *
102: * 4.call me.setLocation(valid local location URL)
103: * expected result: no exception or ListenerVetoException
104: *
105: * 5.call me.setMimeType("www/unknown")
106: * expected result: no exception or ListenerVetoException
107: *
108: * 6.call me.setName(newName)
109: * expected result: no exception or ListenerVetoException
110: *
111: *
112: *
113: *
114: * </pre>
115: */
116: public void testEMB142() throws IOException,
117: InstantiationException, IllegalAccessException,
118: ClassNotFoundException, FinderException, RemoteException,
119: MediaException, CreateException, NamingException {
120:
121: mebTester.bindMediaFormat("jpg", new JpegFormat());
122:
123: EmbeddedMediaLocationInfo me1LocInfo = new EmbeddedMediaLocationInfo(
124: embTestConfig.getJpgPictureName1());
125: String me1 = mebTester.createMediaEntityBean();
126: mebTester.setMediaEntityName(me1, me1LocInfo.getMediaName());
127: mebTester.setMediaEntityContent(me1, me1LocInfo.getMediaFile());
128: String description = mebTester.getMediaEntityDescription(me1);
129: String mimeType = mebTester.getMediaEntityMimeType(me1);
130:
131: EmbeddedMediaLocationInfo me2LocInfo = new EmbeddedMediaLocationInfo(
132: embTestConfig.getJpgPictureName2());
133: String me2 = mebTester.createMediaEntityBean();
134: mebTester.setMediaEntityName(me2, me2LocInfo.getMediaName());
135: mebTester.setMediaEntityLocation(me2, me2LocInfo.getMediaURL());
136:
137: String mediaListeners = embTestConfig.getMediaListeners();
138: StringTokenizer tokenizer = new StringTokenizer(mediaListeners,
139: ";");
140: String listenerName = null;
141: MediaListener currentListener = null;
142:
143: while (tokenizer.hasMoreTokens()) {
144: listenerName = tokenizer.nextToken();
145: currentListener = (MediaListener) Class.forName(
146: listenerName).newInstance();
147: mebTester.addMediaEntityListener(me1, currentListener);
148:
149: int exception = -1;
150: //
151: // test 1
152: //
153: try {
154: exception = mebTester.listenerAboutToChangeMEB(
155: currentListener, me1, null);
156: } catch (Throwable e) {
157: fail("["
158: + listenerName
159: + "] test 1: Should throw a NullPointerException but threw "
160: + e.toString());
161: }
162: assertEquals("[" + listenerName
163: + "] test 1: Should throw a NullPointerException",
164: MediaEntityLocalTestDriver.NULLPOINTER, exception);
165: testTrace("[" + listenerName + "] test 1 passed");
166: //
167: // test 2
168: //
169: try {
170: mebTester.setMediaEntityContent(me1, me2LocInfo
171: .getMediaFile());
172: testTrace("[" + listenerName + "] test 2 passed");
173: } catch (ListenerVetoException e) {
174: assertTrue("[" + listenerName
175: + "] test 2: content changed", Arrays
176: .equals(getByteArrayFromFile(me1LocInfo
177: .getMediaFile()), mebTester
178: .getMediaEntityContent(me1)));
179: testTrace("["
180: + listenerName
181: + "] test 2 passed: threw ListenerVetoException");
182: } catch (Exception e) {
183: fail("["
184: + listenerName
185: + "] test 2: Should pass or throw ListenerVetoException but threw "
186: + e.toString());
187: }
188: //
189: // test 3
190: //
191: try {
192: mebTester.setMediaEntityDescription(me1,
193: EMBStaticHelper
194: .createRandomString(EMBStaticHelper
195: .randomInt(0, 10)));
196: testTrace("[" + listenerName + "] test 3 passed");
197: } catch (ListenerVetoException e) {
198: assertEquals("[" + listenerName
199: + "] test 3: description changed", description,
200: mebTester.getMediaEntityDescription(me1));
201: testTrace("["
202: + listenerName
203: + "] test 3 passed: threw ListenerVetoException");
204: } catch (Exception e) {
205: fail("["
206: + listenerName
207: + "] test 3: Should pass or throw ListenerVetoException but threw "
208: + e.toString());
209: }
210: //
211: // test 4
212: //
213: try {
214: mebTester.setMediaEntityLocation(me2, me1LocInfo
215: .getMediaURL());
216: testTrace("[" + listenerName + "] test 4 passed");
217: } catch (ListenerVetoException e) {
218: assertEquals("[" + listenerName
219: + "] test 4: location changed", me2LocInfo
220: .getMediaURL(), mebTester
221: .getMediaEntityLocation(me2));
222: testTrace("["
223: + listenerName
224: + "] test 4 passed: threw ListenerVetoException");
225: } catch (Exception e) {
226: fail("["
227: + listenerName
228: + "] test 4: Should pass or throw ListenerVetoException but threw "
229: + e.toString());
230: }
231: //
232: // test 5
233: //
234: try {
235: mebTester.setMediaEntityMimeType(me1, "www/unknown");
236: testTrace("[" + listenerName + "] test 5 passed");
237: } catch (ListenerVetoException e) {
238: assertEquals("[" + listenerName
239: + "] test 5: mimeType changed", mimeType,
240: mebTester.getMediaEntityMimeType(me1));
241: testTrace("["
242: + listenerName
243: + "] test 5 passed: threw ListenerVetoException");
244: } catch (Exception e) {
245: fail("["
246: + listenerName
247: + "] test 5: Should pass or throw ListenerVetoException but threw "
248: + e.toString());
249: }
250: //
251: // test 6
252: //
253: try {
254: mebTester.setMediaEntityName(me1, "newName.jpg");
255: testTrace("[" + listenerName + "] test 6 passed");
256: } catch (ListenerVetoException e) {
257: assertEquals("[" + listenerName
258: + "] test 6: name changed", me1LocInfo
259: .getMediaName(), mebTester
260: .getMediaEntityName(me1));
261: testTrace("["
262: + listenerName
263: + "] test 6 passed: threw ListenerVetoException");
264: } catch (Exception e) {
265: fail("["
266: + listenerName
267: + "] test 6: Should pass or throw ListenerVetoException but threw "
268: + e.toString());
269: }
270: }
271: succeed();
272: }
273:
274: /**
275: * Testcase Name: handleMediaEntityChanged(String) Testcase Number: EMB143
276: * setup: create Media entity me and retrieve list of media listeners for
277: * each, call me1.addListener(listener) and run the following tests test
278: * procedure: 1.call handleMediaEntityChanged(null) expected result:
279: * NullPointerException 2.call me.setContent(valid content) expected result:
280: * no exception or ListenerVetoException 3.call me.setDescription(random
281: * string) expected result: no exception or ListenerVetoException 4.call
282: * me.setLocation(valid local location URL) expected result: no exception or
283: * ListenerVetoException 5.call me.setMimeType("www/unknown") expected
284: * result: no exception or ListenerVetoException 6.call me.setName(newName)
285: * expected result: no exception or ListenerVetoException
286: */
287: public void testEMB143() throws IOException,
288: InstantiationException, IllegalAccessException,
289: ClassNotFoundException, FinderException, RemoteException,
290: MediaException, CreateException, NamingException {
291:
292: mebTester.bindMediaFormat("jpg", new JpegFormat());
293:
294: EmbeddedMediaLocationInfo me1LocInfo = new EmbeddedMediaLocationInfo(
295: embTestConfig.getJpgPictureName1());
296: String me1 = mebTester.createMediaEntityBean();
297: mebTester.setMediaEntityName(me1, me1LocInfo.getMediaName());
298: mebTester.setMediaEntityContent(me1, me1LocInfo.getMediaFile());
299: String description = mebTester.getMediaEntityDescription(me1);
300: String mimeType = mebTester.getMediaEntityMimeType(me1);
301:
302: EmbeddedMediaLocationInfo me2LocInfo = new EmbeddedMediaLocationInfo(
303: embTestConfig.getJpgPictureName2());
304: String me2 = mebTester.createMediaEntityBean();
305: mebTester.setMediaEntityName(me2, me2LocInfo.getMediaName());
306: mebTester.setMediaEntityLocation(me2, me2LocInfo.getMediaURL());
307:
308: String mediaListeners = embTestConfig.getMediaListeners();
309: StringTokenizer tokenizer = new StringTokenizer(mediaListeners,
310: ";");
311: String listenerName = null;
312: MediaListener currentListener = null;
313:
314: while (tokenizer.hasMoreTokens()) {
315: listenerName = tokenizer.nextToken();
316: currentListener = (MediaListener) Class.forName(
317: listenerName).newInstance();
318: mebTester.addMediaEntityListener(me1, currentListener);
319:
320: int exception = -1;
321: //
322: // test 1
323: //
324: try {
325: exception = mebTester.listenerMediaEntityChanged(
326: currentListener, me1, null);
327: } catch (Throwable e) {
328: fail("["
329: + listenerName
330: + "] test 1: Should throw a NullPointerException but threw "
331: + e.toString());
332: }
333: assertEquals("[" + listenerName
334: + "] test 1: Should throw a NullPointerException",
335: MediaEntityLocalTestDriver.NULLPOINTER, exception);
336: testTrace("[" + listenerName + "] test 1 passed");
337: //
338: // test 2
339: //
340: try {
341: mebTester.setMediaEntityContent(me1, me2LocInfo
342: .getMediaFile());
343: testTrace("[" + listenerName + "] test 2 passed");
344: } catch (ListenerVetoException e) {
345: assertTrue("[" + listenerName
346: + "] test 2: content changed", Arrays
347: .equals(getByteArrayFromFile(me1LocInfo
348: .getMediaFile()), mebTester
349: .getMediaEntityContent(me1)));
350: testTrace("["
351: + listenerName
352: + "] test 2 passed: threw ListenerVetoException");
353: } catch (Exception e) {
354: fail("["
355: + listenerName
356: + "] test 2: Should pass or throw ListenerVetoException but threw "
357: + e.toString());
358: }
359: //
360: // test 3
361: //
362: try {
363: mebTester.setMediaEntityDescription(me1,
364: EMBStaticHelper
365: .createRandomString(EMBStaticHelper
366: .randomInt(0, 10)));
367: testTrace("[" + listenerName + "] test 3 passed");
368: } catch (ListenerVetoException e) {
369: assertEquals("[" + listenerName
370: + "] test 3: description changed", description,
371: mebTester.getMediaEntityDescription(me1));
372: testTrace("["
373: + listenerName
374: + "] test 3 passed: threw ListenerVetoException");
375: } catch (Exception e) {
376: fail("["
377: + listenerName
378: + "] test 3: Should pass or throw ListenerVetoException but threw "
379: + e.toString());
380: }
381: //
382: // test 4
383: //
384: try {
385: mebTester.setMediaEntityLocation(me2, me1LocInfo
386: .getMediaURL());
387: testTrace("[" + listenerName + "] test 4 passed");
388: } catch (ListenerVetoException e) {
389: assertEquals("[" + listenerName
390: + "] test 4: location changed", me2LocInfo
391: .getMediaURL(), mebTester
392: .getMediaEntityLocation(me2));
393: testTrace("["
394: + listenerName
395: + "] test 4 passed: threw ListenerVetoException");
396: } catch (Exception e) {
397: fail("["
398: + listenerName
399: + "] test 4: Should pass or throw ListenerVetoException but threw "
400: + e.toString());
401: }
402: //
403: // test 5
404: //
405: try {
406: mebTester.setMediaEntityMimeType(me1, "www/unknown");
407: testTrace("[" + listenerName + "] test 5 passed");
408: } catch (ListenerVetoException e) {
409: assertEquals("[" + listenerName
410: + "] test 5: mimeType changed", mimeType,
411: mebTester.getMediaEntityMimeType(me1));
412: testTrace("["
413: + listenerName
414: + "] test 5 passed: threw ListenerVetoException");
415: } catch (Exception e) {
416: fail("["
417: + listenerName
418: + "] test 5: Should pass or throw ListenerVetoException but threw "
419: + e.toString());
420: }
421: //
422: // test 6
423: //
424: try {
425: mebTester.setMediaEntityName(me1, "newName.jpg");
426: testTrace("[" + listenerName + "] test 6 passed");
427: } catch (ListenerVetoException e) {
428: assertEquals("[" + listenerName
429: + "] test 6: name changed", me1LocInfo
430: .getMediaName(), mebTester
431: .getMediaEntityName(me1));
432: testTrace("["
433: + listenerName
434: + "] test 6 passed: threw ListenerVetoException");
435: } catch (Exception e) {
436: fail("["
437: + listenerName
438: + "] test 6: Should pass or throw ListenerVetoException but threw "
439: + e.toString());
440: }
441: }
442: succeed();
443: }
444:
445: /**
446: * <pre>
447: *
448: *
449: *
450: *
451: * Testcase Name: handleAboutToRemoveMediaEntity(MediaEntityLocal)
452: * Testcase Number: EMB144
453: *
454: * setup:
455: *
456: * test procedure:
457: * 1.call handleAboutToRemoveMediaEntity(null)
458: * expected result: NullPointerException
459: *
460: * 2.try to remove media entity
461: * expected result: no exception or ListenerVetoException
462: * if ListenerVetoException, check media entity was not removed
463: *
464: *
465: *
466: *
467: * </pre>
468: */
469: public void testEMB144() throws MalformedURLException, IOException,
470: MediaException, CreateException, InstantiationException,
471: IllegalAccessException, ClassNotFoundException,
472: RemoteException, NamingException, FinderException,
473: RemoveException {
474: EmbeddedMediaLocationInfo me1LocInfo = new EmbeddedMediaLocationInfo(
475: embTestConfig.getJpgPictureName1());
476: String me1 = null;
477:
478: String mediaListeners = embTestConfig.getMediaListeners();
479: StringTokenizer tokenizer = new StringTokenizer(mediaListeners,
480: ";");
481: String listenerName = null;
482: MediaListener currentListener = null;
483:
484: while (tokenizer.hasMoreTokens()) {
485: me1 = mebTester.createMediaEntityBean();
486: mebTester
487: .setMediaEntityName(me1, me1LocInfo.getMediaName());
488: mebTester.setMediaEntityContent(me1, me1LocInfo
489: .getMediaFile());
490:
491: listenerName = tokenizer.nextToken();
492: currentListener = (MediaListener) Class.forName(
493: listenerName).newInstance();
494: mebTester.addMediaEntityListener(me1, currentListener);
495:
496: int exception = -1;
497: //
498: // test 1
499: //
500: try {
501: exception = mebTester.listenerAboutToRemoveMEB(
502: currentListener, null);
503: } catch (Throwable e) {
504: fail("[" + listenerName + "] test 1 threw "
505: + e.toString());
506: }
507: assertEquals("test 1: Should throw a NullPointerException",
508: MediaEntityLocalTestDriver.NULLPOINTER, exception);
509: testTrace("[" + listenerName + "] test 1 passed");
510: //
511: // test 2
512: //
513: try {
514: mebTester.removeMEBExceptions(me1);
515: testTrace("[" + listenerName + "] test 2 passed");
516: } catch (ListenerVetoException e) {
517: assertEquals("[" + listenerName
518: + "] test 2: media entity removed", me1,
519: mebTester.findMEBByPrimaryKey(me1));
520: testTrace("[" + listenerName + "] test 2 passed");
521: mebTester.removeMediaEntityListener(me1,
522: currentListener);
523: mebTester.removeMEBByPK(me1);
524: } catch (Exception e) {
525: fail("["
526: + listenerName
527: + "] test 2: Should pass or throw ListenerVetoException but threw "
528: + e.toString());
529: }
530: }
531: succeed();
532: }
533: }
|