KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmx > compliance > standard > TrivialTestCase


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

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