1   /*
2    * $Id: AbstractNotificationTestCase.java 11413 2008-03-18 03:08:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.context.notification;
12  
13  import org.mule.api.context.notification.ServerNotification;
14  import org.mule.tck.FunctionalTestCase;
15  
16  import java.util.Iterator;
17  
18  /**
19   * Tests must define a "notificationLogger" listener
20   */
21  public abstract class AbstractNotificationTestCase extends FunctionalTestCase
22  {
23  
24      private AbstractNotificationLogger notifications;
25  
26      public AbstractNotificationTestCase()
27      {
28          super();
29          setDisposeManagerPerSuite(true);
30      }
31      
32      public final void testNotifications() throws Exception
33      {
34          doTest();
35          notifications = (AbstractNotificationLogger) muleContext.getRegistry().lookupObject("notificationLogger");
36      }
37  
38      public abstract void doTest() throws Exception;
39  
40      public abstract RestrictedNode getSpecification();
41  
42      public abstract void validateSpecification(RestrictedNode spec) throws Exception;
43  
44      protected void suitePostTearDown() throws Exception
45      {
46          // Need to explicitly dispose manager here to get disposal notifications
47          muleContext.dispose();
48          // allow shutdown to complete (or get concurrent mod errors and/or miss notifications)
49          Thread.sleep(2000L);
50          logNotifications();
51          RestrictedNode spec = getSpecification();
52          validateSpecification(spec);
53          assertExpectedNotifications(spec);
54      }
55  
56      protected void logNotifications()
57      {
58          logger.info("Number of notifications: " + notifications.getNotifications().size());
59          for (Iterator iterator = notifications.getNotifications().iterator(); iterator.hasNext();)
60          {
61              ServerNotification notification = (ServerNotification) iterator.next();
62              logger.info(notification);
63          }
64      }
65  
66      /**
67       * This is destructive - do not use spec after calling this routine
68       */
69      protected void assertExpectedNotifications(RestrictedNode spec)
70      {
71          for (Iterator iterator = notifications.getNotifications().iterator(); iterator.hasNext();)
72          {
73              ServerNotification notification = (ServerNotification) iterator.next();
74              switch (spec.match(notification))
75              {
76              case Node.SUCCESS:
77                  break;
78              case Node.FAILURE:
79                  fail("Could not match " + notification);
80                  break;
81              case Node.EMPTY:
82                  fail("Extra notification: " + notification);
83              }
84          }
85          if (!spec.isExhausted())
86          {
87              fail("Specification not exhausted: " + spec.getAnyRemaining());
88          }
89      }
90  
91      protected void verifyAllNotifications(RestrictedNode spec, Class clazz, int from, int to)
92      {
93          for (int action = from; action <= to; ++action)
94          {
95              if (!spec.contains(clazz, action))
96              {
97                  fail("Specification missed action " + action + " for class " + clazz);
98              }
99          }
100     }
101 
102 }