KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > support > MBeanServerFactoryBeanTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.springframework.jmx.support;
18
19 import java.util.List JavaDoc;
20
21 import javax.management.MBeanServer JavaDoc;
22 import javax.management.MBeanServerFactory JavaDoc;
23
24 import junit.framework.TestCase;
25
26 /**
27  * @author Rob Harrop
28  */

29 public class MBeanServerFactoryBeanTests extends TestCase {
30
31     public void testGetObject() throws Exception JavaDoc {
32         MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
33         bean.afterPropertiesSet();
34         try {
35             MBeanServer JavaDoc server = (MBeanServer JavaDoc) bean.getObject();
36             assertNotNull("The MBeanServer should not be null", server);
37         }
38         finally {
39             bean.destroy();
40         }
41     }
42
43     public void testDefaultDomain() throws Exception JavaDoc {
44         MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
45         bean.setDefaultDomain("foo");
46         bean.afterPropertiesSet();
47         try {
48             MBeanServer JavaDoc server = (MBeanServer JavaDoc) bean.getObject();
49             assertEquals("The default domain should be foo", "foo", server.getDefaultDomain());
50         }
51         finally {
52             bean.destroy();
53         }
54     }
55
56     public void testCreateMBeanServer() throws Exception JavaDoc {
57         testCreation(true, "The server should be available in the list");
58     }
59
60     public void testNewMBeanServer() throws Exception JavaDoc {
61         testCreation(false, "The server should not be available in the list");
62     }
63
64     private void testCreation(boolean referenceShouldExist, String JavaDoc failMsg) throws Exception JavaDoc {
65         MBeanServerFactoryBean bean = new MBeanServerFactoryBean();
66         bean.setRegisterWithFactory(referenceShouldExist);
67         bean.afterPropertiesSet();
68
69         try {
70             MBeanServer JavaDoc server = (MBeanServer JavaDoc) bean.getObject();
71             List JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
72
73             boolean found = false;
74             for (int x = 0; x < servers.size(); x++) {
75                 if (servers.get(x) == server) {
76                     found = true;
77                     break;
78                 }
79             }
80
81             if (!(found == referenceShouldExist)) {
82                 fail(failMsg);
83             }
84         }
85         finally {
86             bean.destroy();
87         }
88     }
89
90 }
91
Popular Tags