KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > standard > TrivialTEST


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.standard;
23
24 import javax.management.InstanceAlreadyExistsException JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.IntrospectionException JavaDoc;
27 import javax.management.MBeanAttributeInfo JavaDoc;
28 import javax.management.MBeanConstructorInfo JavaDoc;
29 import javax.management.MBeanInfo JavaDoc;
30 import javax.management.MBeanNotificationInfo JavaDoc;
31 import javax.management.MBeanOperationInfo JavaDoc;
32 import javax.management.MBeanParameterInfo JavaDoc;
33 import javax.management.MBeanRegistrationException JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.MBeanServerFactory JavaDoc;
36 import javax.management.MalformedObjectNameException JavaDoc;
37 import javax.management.NotCompliantMBeanException JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.ReflectionException JavaDoc;
40
41 import junit.framework.TestCase;
42
43 import org.jboss.test.jmx.compliance.standard.support.Trivial;
44
45 /**
46  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
47  */

48
49 public class TrivialTEST extends TestCase
50 {
51    public TrivialTEST(String JavaDoc s)
52    {
53       super(s);
54    }
55
56    public void testRegistration()
57    {
58       MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
59       Trivial trivial = new Trivial();
60
61       ObjectName JavaDoc name = null;
62       try
63       {
64          name = new ObjectName JavaDoc("trivial:key=val");
65          server.registerMBean(trivial, name);
66       }
67       catch (Exception JavaDoc e)
68       {
69          fail("registration failed: " + e.getMessage());
70       }
71       assertTrue("expected server to report it as registered", server.isRegistered(name));
72    }
73
74    public void testConstructorInfo()
75    {
76       MBeanInfo JavaDoc info = getTrivialInfo();
77
78       MBeanConstructorInfo JavaDoc[] constructors = info.getConstructors();
79       assertEquals("constructor list length", 1, constructors.length);
80
81       // I really don't feel like reflecting to get the name of the constructor,
82
// it should just be the name of the class right?
83
assertEquals("constructor name", Trivial.class.getName(), constructors[0].getName());
84
85       MBeanParameterInfo JavaDoc[] params = constructors[0].getSignature();
86       assertEquals("constructor signature length", 0, params.length);
87    }
88
89    public void testAttributeInfo()
90    {
91       MBeanInfo JavaDoc info = getTrivialInfo();
92
93       MBeanAttributeInfo JavaDoc[] attributes = info.getAttributes();
94       assertEquals("attribute list length", 1, attributes.length);
95       assertEquals("attribute name", "Something", attributes[0].getName());
96       assertEquals("attribute type", String JavaDoc.class.getName(), attributes[0].getType());
97       assertEquals("attribute readable", true, attributes[0].isReadable());
98       assertEquals("attribute writable", true, attributes[0].isWritable());
99       assertEquals("attribute isIs", false, attributes[0].isIs());
100    }
101
102    public void testOperationInfo()
103    {
104       MBeanInfo JavaDoc info = getTrivialInfo();
105
106       MBeanOperationInfo JavaDoc[] operations = info.getOperations();
107       assertEquals("operations list length", 1, operations.length);
108       assertEquals("operation name", "doOperation", operations[0].getName());
109       assertEquals("operation return type", Void.TYPE.getName(), operations[0].getReturnType());
110       assertEquals("operation impact", MBeanOperationInfo.UNKNOWN, operations[0].getImpact());
111
112       MBeanParameterInfo JavaDoc[] params = operations[0].getSignature();
113       assertEquals("signature length", 1, params.length);
114       assertEquals("parameter type", String JavaDoc.class.getName(), params[0].getType());
115    }
116
117    public void testNotificationInfo()
118    {
119       MBeanInfo JavaDoc info = getTrivialInfo();
120
121       MBeanNotificationInfo JavaDoc[] notifications = info.getNotifications();
122       assertEquals("notification list length", 0, notifications.length);
123    }
124
125
126    private MBeanInfo JavaDoc getTrivialInfo()
127    {
128       MBeanInfo JavaDoc info = null;
129
130       try
131       {
132          MBeanServer JavaDoc server = MBeanServerFactory.newMBeanServer();
133          Trivial trivial = new Trivial();
134
135          ObjectName JavaDoc name = new ObjectName JavaDoc("trivial:key=val");
136          server.registerMBean(trivial, name);
137          info = server.getMBeanInfo(name);
138       }
139       catch (MalformedObjectNameException JavaDoc e)
140       {
141          fail("got spurious MalformedObjectNameException");
142       }
143       catch (InstanceAlreadyExistsException JavaDoc e)
144       {
145          fail("got spurious InstanceAlreadyExistsException");
146       }
147       catch (MBeanRegistrationException JavaDoc e)
148       {
149          fail("got spurious MBeanRegistrationException");
150       }
151       catch (NotCompliantMBeanException JavaDoc e)
152       {
153          fail("got spurious NotCompliantMBeanException");
154       }
155       catch (InstanceNotFoundException JavaDoc e)
156       {
157          fail("got spurious InstanceNotFoundException");
158       }
159       catch (IntrospectionException JavaDoc e)
160       {
161          fail("got spurious IntrospectionException");
162       }
163       catch (ReflectionException JavaDoc e)
164       {
165          fail("got spurious ReflectionException");
166       }
167
168       return info;
169    }
170 }
171
Popular Tags