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.management;
8   
9   import org.mule.tck.junit4.FunctionalTestCase;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  import java.util.Set;
14  
15  import javax.management.MBeanServer;
16  import javax.management.MBeanServerFactory;
17  import javax.management.ObjectInstance;
18  import javax.management.ObjectName;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  public class JmxDuplicateEndpointNamesTestCase extends FunctionalTestCase
26  {
27  
28      private List<ObjectInstance> endpointMBeans = new ArrayList<ObjectInstance>();
29      
30      @Override
31      protected String getConfigResources()
32      {
33          return "duplicate-endpoint-addesses.xml";
34      }
35  
36      @Test
37      public void testDuplicateNames()
38      {
39          List<?> mBeanServers = MBeanServerFactory.findMBeanServer(null);
40          assertTrue("no local MBean server found", mBeanServers.size() > 0);        
41          
42          inspectMBeanServers(mBeanServers);
43          assertEquals(2, endpointMBeans.size());
44      }
45  
46      private void inspectMBeanServers(List<?> mBeanServers)
47      {
48          for (Object o : mBeanServers)
49          {
50              MBeanServer server = (MBeanServer) o;
51              
52              Set<?> mBeans = server.queryMBeans(null, null);
53              assertTrue("no registered MBeans found", mBeans.size() > 0);
54              
55              inspectMBeans(mBeans);
56          }
57      }
58  
59      private void inspectMBeans(Set<?> mBeans)
60      {
61          for (Object o : mBeans)
62          {
63              ObjectInstance instance = (ObjectInstance) o;
64              if (objectNameMatches(instance))
65              {
66                  endpointMBeans.add(instance);
67              }
68          }
69      }
70  
71      private boolean objectNameMatches(ObjectInstance instance)
72      {
73          ObjectName name = instance.getObjectName();
74          return name.getCanonicalName().contains("vmInbound");    
75      }
76  
77  }
78  
79