View Javadoc

1   /*
2    * $Id: JmxStatisticsAsyncTestCase.java 22401 2011-07-13 09:10:18Z 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.management;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.management.stats.ApplicationStatistics;
15  import org.mule.management.stats.FlowConstructStatistics;
16  import org.mule.management.stats.ServiceStatistics;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.junit4.FunctionalTestCase;
19  
20  import java.util.Iterator;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  public class JmxStatisticsAsyncTestCase extends FunctionalTestCase
29  {
30      
31      @Override
32      protected String getConfigResources()
33      {
34          return "jmx-statistics-test.xml";
35      }
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          super.doSetUp();
41                  
42          MuleClient muleClient = new MuleClient(muleContext);
43          muleClient.dispatch("vm://in", "Hello world", null);
44          MuleMessage response = muleClient.request("vm://out", RECEIVE_TIMEOUT * 2);
45          assertNotNull(response);
46          assertEquals("data", response.getPayloadAsString());
47          muleClient.dispatch("vm://inflow", "Flow data", null);
48          response = muleClient.request("vm://outflow", RECEIVE_TIMEOUT * 2);
49          muleClient.dispatch("vm://inflow", "Flow data", null);
50          response = muleClient.request("vm://outflow", RECEIVE_TIMEOUT * 2);
51          assertNotNull(response);
52          assertEquals("Flow data", response.getPayloadAsString());
53      }
54  
55      @Override
56      protected void doTearDown() throws Exception
57      {
58          getServiceStatistics().clear();
59          super.doTearDown();
60      }
61  
62      @Test
63      public void testCorrectAverageQueueSize() throws Exception
64      {
65          ServiceStatistics stats = getServiceStatistics();
66          assertEquals(1, stats.getAverageQueueSize());
67      }
68  
69      @Test
70      public void testCorrectAsynchEventsReceived() throws Exception
71      {
72          ServiceStatistics stats = getServiceStatistics();
73          assertEquals(1, stats.getAsyncEventsReceived());
74          FlowConstructStatistics fstats = getFlowConstructStatistics();
75          assertEquals(2, fstats.getAsyncEventsReceived());
76          ApplicationStatistics astats = getApplicationStatistics();
77          assertEquals(3, astats.getAsyncEventsReceived());
78      }
79  
80      @Test
81      public void testCorrectMaxQueueSize() throws Exception
82      {
83          ServiceStatistics stats = getServiceStatistics();
84          assertEquals(1, stats.getMaxQueueSize());
85      }
86  
87      @Test
88      public void testCorrectAsynchEventsSent() throws Exception
89      {
90          ServiceStatistics stats = getServiceStatistics();
91          assertEquals(1, stats.getAsyncEventsSent());
92      }
93  
94      @Test
95      public void testCorrectTotalEventsSent() throws Exception
96      {
97          ServiceStatistics stats = getServiceStatistics();
98          assertEquals(1, stats.getTotalEventsSent());
99      }
100 
101     @Test
102     public void testCorrectTotalEventsReceived() throws Exception
103     {
104         ServiceStatistics stats = getServiceStatistics();
105         assertEquals(1, stats.getTotalEventsReceived());
106         FlowConstructStatistics fstats = getFlowConstructStatistics();
107         assertEquals(2, fstats.getTotalEventsReceived());
108         ApplicationStatistics astats = getApplicationStatistics();
109         assertEquals(3, astats.getTotalEventsReceived());
110     }
111 
112     private ServiceStatistics getServiceStatistics()
113     {
114         Iterator<FlowConstructStatistics> iterator = muleContext.getStatistics().getServiceStatistics().iterator();
115         FlowConstructStatistics stat1;
116         do
117         {
118             assertTrue(iterator.hasNext());
119             stat1 = iterator.next();
120         }
121         while (!(stat1 instanceof ServiceStatistics));
122         return (ServiceStatistics)stat1;
123 
124     }
125 
126     private FlowConstructStatistics getFlowConstructStatistics()
127     {
128         Iterator<FlowConstructStatistics> iterator = muleContext.getStatistics().getServiceStatistics().iterator();
129         FlowConstructStatistics stat1;
130         do
131         {
132             assertTrue(iterator.hasNext());
133             stat1 = iterator.next();
134         }
135         while (stat1.getClass() != FlowConstructStatistics.class);
136         return stat1;
137     }
138 
139     private ApplicationStatistics getApplicationStatistics()
140     {
141         Iterator<FlowConstructStatistics> iterator = muleContext.getStatistics().getServiceStatistics().iterator();
142         FlowConstructStatistics stat1;
143         do
144         {
145             assertTrue(iterator.hasNext());
146             stat1 = iterator.next();
147         }
148         while (!(stat1 instanceof ApplicationStatistics));
149         return (ApplicationStatistics)stat1;
150     }
151 
152 }