KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > implementation > modelmbean > XMBeanTEST


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 test.implementation.modelmbean;
23
24 import javax.management.Attribute JavaDoc;
25 import javax.management.Descriptor JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.MBeanServerFactory JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.modelmbean.DescriptorSupport JavaDoc;
30
31 import junit.framework.TestCase;
32
33 import org.jboss.mx.modelmbean.XMBean;
34 import org.jboss.mx.modelmbean.XMBeanConstants;
35
36 import test.implementation.modelmbean.support.Trivial;
37 import test.implementation.modelmbean.support.User;
38
39 /**
40  * Here are some basic XMBean tests, mainly to demonstrate the use of the
41  * XMBean class and the MBean creation (this is the doc ;)
42  *
43  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
44  * @version $Revision: 37459 $
45  */

46 public class XMBeanTEST extends TestCase implements XMBeanConstants
47 {
48    public XMBeanTEST(String JavaDoc s)
49    {
50       super(s);
51    }
52
53    public void testCreateXMBean() throws Exception JavaDoc
54    {
55       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
56       
57       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
58       d.setField(RESOURCE_REFERENCE, new User());
59       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/UserManagementInterface.xml");
60       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
61
62       XMBean mmb = new XMBean(d, DESCRIPTOR);
63       
64       ObjectName JavaDoc name = new ObjectName JavaDoc(":test=test");
65       
66       server.registerMBean(mmb, name);
67       assertTrue(server.isRegistered(name));
68       
69       server.setAttribute(name, new Attribute JavaDoc("Name", "Juha"));
70       assertTrue(server.getAttribute(name, "Name").equals("Juha"));
71       
72       server.setAttribute(name, new Attribute JavaDoc("Address", "StrawBerry Street"));
73       assertTrue(server.getAttribute(name, "Address").equals("StrawBerry Street"));
74       
75       assertTrue(server.invoke(name, "printInfo", null, null) instanceof String JavaDoc);
76    }
77
78    public void testCreateWithJBossXMBean10DTD() throws Exception JavaDoc
79    {
80       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
81       Descriptor JavaDoc d = new DescriptorSupport JavaDoc();
82       d.setField(RESOURCE_REFERENCE, new User());
83       d.setField(RESOURCE_TYPE, "file:./src/main/test/implementation/modelmbean/support/xml/User.xml");
84       d.setField(SAX_PARSER, "org.apache.crimson.parser.XMLReaderImpl");
85
86       XMBean mmb = new XMBean(d, DESCRIPTOR);
87
88       server.registerMBean(mmb, new ObjectName JavaDoc(":test=test"));
89       
90       assertTrue(server.isRegistered(new ObjectName JavaDoc(":test=test")));
91       
92       server.setAttribute(new ObjectName JavaDoc(":test=test"), new Attribute JavaDoc("Name", "Juha"));
93       
94       assertTrue(server.getAttribute(new ObjectName JavaDoc(":test=test"), "Name").equals("Juha"));
95       
96    }
97    
98    public void testCreateWithStandardInterface() throws Exception JavaDoc
99    {
100       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
101       Trivial trivial = new Trivial();
102       ObjectName JavaDoc name = new ObjectName JavaDoc(":foo=bar");
103       
104       XMBean mmb = new XMBean(trivial, XMBeanConstants.STANDARD_INTERFACE);
105       server.registerMBean(mmb, name);
106       
107       assertTrue(server.isRegistered(new ObjectName JavaDoc(":foo=bar")));
108       
109       server.setAttribute(name, new Attribute JavaDoc("Something", "foobar"));
110       assertTrue(server.getAttribute(name, "Something").equals("foobar"));
111       
112       Boolean JavaDoc b = (Boolean JavaDoc)server.invoke(name, "doOperation", new Object JavaDoc[] { "" }, new String JavaDoc[] { "java.lang.String" });
113       assertTrue(b.booleanValue() == true);
114    }
115    
116    
117 }
118
Popular Tags