View Javadoc

1   /*
2    * $Id: AbstractNotificationTestCase.java 22423 2011-07-15 13:04:56Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.AbstractServiceAndFlowTestCase;
15  
16  import java.util.Iterator;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.fail;
21  
22  /**
23   * Tests must define a "notificationLogger" listener
24   */
25  public abstract class AbstractNotificationTestCase extends AbstractServiceAndFlowTestCase
26  {
27      private AbstractNotificationLogger<? extends ServerNotification> notificationLogger;
28  
29      public AbstractNotificationTestCase(ConfigVariant variant, String configResources)
30      {
31          super(variant, configResources);
32      }
33  
34      @SuppressWarnings("unchecked")
35      @Test
36      public final void testNotifications() throws Exception
37      {
38          doTest();
39          notificationLogger = (AbstractNotificationLogger<? extends ServerNotification>) muleContext.getRegistry().lookupObject(
40              "notificationLogger");
41  
42          // Need to explicitly dispose manager here to get disposal notifications
43          muleContext.dispose();
44          // allow shutdown to complete (or get concurrent mod errors and/or miss
45          // notifications)
46          Thread.sleep(2000L);
47          logNotifications();
48          RestrictedNode spec = getSpecification();
49          validateSpecification(spec);
50          assertExpectedNotifications(spec);
51      }
52  
53      public abstract void doTest() throws Exception;
54  
55      public abstract RestrictedNode getSpecification();
56  
57      public abstract void validateSpecification(RestrictedNode spec) throws Exception;
58  
59      protected void logNotifications()
60      {
61          logger.info("Number of notifications: " + notificationLogger.getNotifications().size());
62          for (Iterator<?> iterator = notificationLogger.getNotifications().iterator(); iterator.hasNext();)
63          {
64              ServerNotification notification = (ServerNotification) iterator.next();
65              logger.info(notification);
66          }
67      }
68  
69      /**
70       * This is destructive - do not use spec after calling this routine
71       */
72      protected void assertExpectedNotifications(RestrictedNode spec)
73      {
74          for (Iterator<?> iterator = notificationLogger.getNotifications().iterator(); iterator.hasNext();)
75          {
76              ServerNotification notification = (ServerNotification) iterator.next();
77              switch (spec.match(notification))
78              {
79                  case Node.SUCCESS :
80                      break;
81                  case Node.FAILURE :
82                      fail("Could not match " + notification);
83                      break;
84                  case Node.EMPTY :
85                      fail("Extra notification: " + notification);
86              }
87          }
88          if (!spec.isExhausted())
89          {
90              fail("Specification not exhausted: " + spec.getAnyRemaining());
91          }
92      }
93  
94      protected void verifyAllNotifications(RestrictedNode spec, Class<?> clazz, int from, int to)
95      {
96          for (int action = from; action <= to; ++action)
97          {
98              if (!spec.contains(clazz, action))
99              {
100                 fail("Specification missed action " + action + " for class " + clazz);
101             }
102         }
103     }
104 
105     protected void verifyNotification(RestrictedNode spec, Class<?> clazz, int action)
106     {
107         if (!spec.contains(clazz, action))
108         {
109             fail("Specification missed action " + action + " for class " + clazz);
110         }
111     }
112 }