KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > server > MBeanServerFactoryTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.server;
23
24 import java.util.List JavaDoc;
25
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import junit.framework.AssertionFailedError;
31 import junit.framework.TestCase;
32
33
34 public class MBeanServerFactoryTEST extends TestCase
35 {
36    public MBeanServerFactoryTEST(String JavaDoc s)
37    {
38       super(s);
39    }
40
41    public void testFindNonCreated()
42    {
43       MBeanServerFactory.newMBeanServer();
44       List JavaDoc mbsList = MBeanServerFactory.findMBeanServer(null);
45       assertEquals(0, mbsList.size());
46    }
47
48    public void testCreateFindAndRelease()
49    {
50       MBeanServer JavaDoc server = null;
51       List JavaDoc mbsList = null;
52
53       try
54       {
55          server = MBeanServerFactory.createMBeanServer();
56          mbsList = MBeanServerFactory.findMBeanServer(null);
57          assertEquals(1, mbsList.size());
58       }
59       finally
60       {
61          if (null != server)
62          {
63             MBeanServerFactory.releaseMBeanServer(server);
64          }
65       }
66
67       mbsList = MBeanServerFactory.findMBeanServer(null);
68       assertEquals(0, mbsList.size());
69    }
70
71    public void testRemoveNonCreated()
72    {
73       try
74       {
75          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
76          MBeanServerFactory.releaseMBeanServer(server);
77          fail("expected an IllegalArgumentException");
78       }
79       catch (IllegalArgumentException JavaDoc e)
80       {
81       }
82       catch (Exception JavaDoc e)
83       {
84          fail("expected an IllegalArgumentException but got: " + e.getMessage());
85       }
86    }
87
88    public void testDomainCreated()
89    {
90       String JavaDoc domain = "dOmAiN";
91       MBeanServer JavaDoc server = null;
92       try
93       {
94          server = MBeanServerFactory.createMBeanServer(domain);
95          assertEquals(domain, server.getDefaultDomain());
96          List JavaDoc mbsList = MBeanServerFactory.findMBeanServer(null);
97          assertEquals(server, mbsList.get(0));
98          assertTrue("expected server reference equality", mbsList.get(0) == server);
99       }
100       finally
101       {
102          if (null != server)
103          {
104             MBeanServerFactory.releaseMBeanServer(server);
105          }
106       }
107    }
108
109    public void testDomainNonCreated()
110    {
111       String JavaDoc domain = "dOmAiN";
112       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer(domain);
113       assertEquals(domain, server.getDefaultDomain());
114    }
115
116    public void testFindByAgentID()
117    {
118       try
119       {
120          MBeanServer JavaDoc server1 = MBeanServerFactory.createMBeanServer();
121          MBeanServer JavaDoc server2 = MBeanServerFactory.createMBeanServer();
122          MBeanServer JavaDoc server3 = MBeanServerFactory.newMBeanServer();
123          ObjectName JavaDoc delegateName = new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
124          
125          String JavaDoc agentID1 = (String JavaDoc)server1.getAttribute(delegateName, "MBeanServerId");
126          String JavaDoc agentID2 = (String JavaDoc)server2.getAttribute(delegateName, "MBeanServerId");
127          String JavaDoc agentID3 = (String JavaDoc)server3.getAttribute(delegateName, "MBeanServerId");
128          
129          assertTrue((MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(agentID1).get(0) == server1);
130          assertTrue((MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(agentID2).get(0) == server2);
131          assertTrue(MBeanServerFactory.findMBeanServer(agentID3).size() == 0);
132       }
133       catch (AssertionFailedError e)
134       {
135          throw e;
136       }
137       catch (Throwable JavaDoc t)
138       {
139          fail("Unexpected error: " + t.toString());
140       }
141    }
142
143 }
144
Popular Tags