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