KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > access > MBeanProxyFactoryBeanTests


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.access;
18
19 import javax.management.MalformedObjectNameException JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import org.springframework.beans.factory.BeanCreationException;
23 import org.springframework.beans.factory.xml.XmlBeanFactory;
24 import org.springframework.core.io.ClassPathResource;
25 import org.springframework.jmx.AbstractJmxTests;
26 import org.springframework.jmx.IJmxTestBean;
27 import org.springframework.jmx.support.ObjectNameManager;
28
29 /**
30  * @author Rob Harrop
31  */

32 public class MBeanProxyFactoryBeanTests extends AbstractJmxTests {
33
34     private static final String JavaDoc OBJECT_NAME = "bean:name=testBean1";
35
36     protected ObjectName JavaDoc getObjectName() throws Exception JavaDoc {
37         return ObjectNameManager.getInstance(OBJECT_NAME);
38     }
39
40     public void testProxyFactory() throws Exception JavaDoc {
41         MBeanProxyFactoryBean fb = getProxyFactory();
42         fb.setProxyInterface(IJmxTestBean.class);
43         fb.afterPropertiesSet();
44
45         IJmxTestBean bean = (IJmxTestBean) fb.getObject();
46         assertNotNull("Proxy should not be null", bean);
47     }
48
49     public void testInvalidJdkProxy() throws Exception JavaDoc {
50         MBeanProxyFactoryBean fb = getProxyFactory();
51         try {
52             fb.afterPropertiesSet();
53             fail("Should not be able to create JDK proxy with no proxy interfaces");
54         }
55         catch (Exception JavaDoc ex) {
56             // expected
57
}
58     }
59
60     public void testWithLocatedMBeanServer() throws Exception JavaDoc {
61         MBeanProxyFactoryBean fb = new MBeanProxyFactoryBean();
62         fb.setProxyInterface(IJmxTestBean.class);
63         fb.setObjectName(OBJECT_NAME);
64         fb.afterPropertiesSet();
65         Object JavaDoc proxy = fb.getObject();
66         assertNotNull(proxy);
67     }
68
69     public void testProxyFactoryBeanWithAutodetect() throws Exception JavaDoc {
70         try {
71             XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryBean.xml", getClass()));
72             bf.preInstantiateSingletons();
73         }
74         catch (BeanCreationException ex) {
75             if (ex.getCause().getClass() == MBeanInfoRetrievalException.class) {
76                 fail("MBeanProxyFactoryBean should be ignored by MBeanExporter when running autodetect process");
77             }
78             else {
79                 throw ex;
80             }
81         }
82     }
83
84     private MBeanProxyFactoryBean getProxyFactory() throws MalformedObjectNameException JavaDoc {
85         MBeanProxyFactoryBean fb = new MBeanProxyFactoryBean();
86         fb.setServer(server);
87         fb.setObjectName(OBJECT_NAME);
88         return fb;
89     }
90
91 }
92
Popular Tags