001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.implementation.modelmbean;
023:
024: import javax.management.Attribute;
025: import javax.management.Descriptor;
026: import javax.management.MBeanServer;
027: import javax.management.MBeanServerFactory;
028: import javax.management.ObjectName;
029: import javax.management.modelmbean.DescriptorSupport;
030:
031: import junit.framework.TestCase;
032:
033: import org.jboss.mx.modelmbean.XMBean;
034: import org.jboss.mx.modelmbean.XMBeanConstants;
035:
036: import test.implementation.modelmbean.support.Trivial;
037: import test.implementation.modelmbean.support.User;
038:
039: /**
040: * Here are some basic XMBean tests, mainly to demonstrate the use of the
041: * XMBean class and the MBean creation (this is the doc ;)
042: *
043: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
044: * @version $Revision: 57200 $
045: */
046: public class XMBeanTEST extends TestCase implements XMBeanConstants {
047: public XMBeanTEST(String s) {
048: super (s);
049: }
050:
051: public void testCreateXMBean() throws Exception {
052: MBeanServer server = MBeanServerFactory.createMBeanServer();
053:
054: Descriptor d = new DescriptorSupport();
055: d.setField(RESOURCE_REFERENCE, new User());
056: d
057: .setField(
058: RESOURCE_TYPE,
059: "file:./src/main/test/implementation/modelmbean/support/xml/UserManagementInterface.xml");
060: d.setField(SAX_PARSER,
061: "org.apache.crimson.parser.XMLReaderImpl");
062:
063: XMBean mmb = new XMBean(d, DESCRIPTOR);
064:
065: ObjectName name = new ObjectName(":test=test");
066:
067: server.registerMBean(mmb, name);
068: assertTrue(server.isRegistered(name));
069:
070: server.setAttribute(name, new Attribute("Name", "Juha"));
071: assertTrue(server.getAttribute(name, "Name").equals("Juha"));
072:
073: server.setAttribute(name, new Attribute("Address",
074: "StrawBerry Street"));
075: assertTrue(server.getAttribute(name, "Address").equals(
076: "StrawBerry Street"));
077:
078: assertTrue(server.invoke(name, "printInfo", null, null) instanceof String);
079: }
080:
081: public void testCreateWithJBossXMBean10DTD() throws Exception {
082: MBeanServer server = MBeanServerFactory.createMBeanServer();
083: Descriptor d = new DescriptorSupport();
084: d.setField(RESOURCE_REFERENCE, new User());
085: d
086: .setField(RESOURCE_TYPE,
087: "file:./src/main/test/implementation/modelmbean/support/xml/User.xml");
088: d.setField(SAX_PARSER,
089: "org.apache.crimson.parser.XMLReaderImpl");
090:
091: XMBean mmb = new XMBean(d, DESCRIPTOR);
092:
093: server.registerMBean(mmb, new ObjectName(":test=test"));
094:
095: assertTrue(server.isRegistered(new ObjectName(":test=test")));
096:
097: server.setAttribute(new ObjectName(":test=test"),
098: new Attribute("Name", "Juha"));
099:
100: assertTrue(server.getAttribute(new ObjectName(":test=test"),
101: "Name").equals("Juha"));
102:
103: }
104:
105: public void testCreateWithStandardInterface() throws Exception {
106: MBeanServer server = MBeanServerFactory.createMBeanServer();
107: Trivial trivial = new Trivial();
108: ObjectName name = new ObjectName(":foo=bar");
109:
110: XMBean mmb = new XMBean(trivial,
111: XMBeanConstants.STANDARD_INTERFACE);
112: server.registerMBean(mmb, name);
113:
114: assertTrue(server.isRegistered(new ObjectName(":foo=bar")));
115:
116: server.setAttribute(name, new Attribute("Something", "foobar"));
117: assertTrue(server.getAttribute(name, "Something").equals(
118: "foobar"));
119:
120: Boolean b = (Boolean) server.invoke(name, "doOperation",
121: new Object[] { "" },
122: new String[] { "java.lang.String" });
123: assertTrue(b.booleanValue() == true);
124: }
125:
126: }
|