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.compliance.metadata;
023:
024: import junit.framework.TestCase;
025: import junit.framework.AssertionFailedError;
026:
027: import java.lang.reflect.Method;
028:
029: import javax.management.MBeanAttributeInfo;
030: import javax.management.IntrospectionException;
031:
032: import test.compliance.metadata.support.Trivial;
033:
034: /**
035: * Tests MBeanAttributeInfo.
036: *
037: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
038: * @version $Revision: 57200 $
039: */
040: public class MBeanAttributeInfoTEST extends TestCase {
041: public MBeanAttributeInfoTEST(String s) {
042: super (s);
043: }
044:
045: /**
046: * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> constructor.
047: */
048: public void testConstructorWithAccessorMethods() {
049: try {
050: Class c = Trivial.class;
051: Method getter = c.getMethod("getSomething", new Class[0]);
052: Method setter = c.getMethod("setSomething",
053: new Class[] { String.class });
054:
055: MBeanAttributeInfo info = new MBeanAttributeInfo(
056: "Something", "a description", getter, setter);
057:
058: assertTrue(info.getDescription().equals("a description"));
059: assertTrue(info.getName().equals("Something"));
060: assertTrue(info.getType().equals("java.lang.String"));
061: assertTrue(info.isReadable() == true);
062: assertTrue(info.isWritable() == true);
063: assertTrue(info.isIs() == false);
064: } catch (AssertionFailedError e) {
065: throw e;
066: } catch (Throwable t) {
067: t.printStackTrace();
068: fail("Unexpected error: " + t.toString());
069: }
070: }
071:
072: /**
073: * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with misplaced accessor methods.
074: */
075: public void testConstructorWithMisplacedAccessorMethods() {
076: try {
077: Class c = Trivial.class;
078: Method getter = c.getMethod("getSomething", new Class[0]);
079: Method setter = c.getMethod("setSomething",
080: new Class[] { String.class });
081:
082: MBeanAttributeInfo info = new MBeanAttributeInfo(
083: "Something", "a description", setter, getter);
084:
085: // shouldn't reach here
086: fail("Introspection exception should have been thrown.");
087: } catch (IntrospectionException e) {
088: // this is expected
089: } catch (AssertionFailedError e) {
090: throw e;
091: } catch (Throwable t) {
092: t.printStackTrace();
093: fail("Unexpected error: " + t.toString());
094: }
095: }
096:
097: /**
098: * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with invalid getter method.
099: */
100: public void testConstructorWithInvalidGetterMethod() {
101: try {
102: Class c = Trivial.class;
103: Method getter = c.getMethod("getSomethingInvalid",
104: new Class[] { Object.class });
105: Method setter = c.getMethod("setSomethingInvalid",
106: new Class[] { String.class });
107:
108: MBeanAttributeInfo info = new MBeanAttributeInfo(
109: "Something", "a description", getter, setter);
110:
111: // shouldn't reach here
112: fail("Introspection exception should have been thrown.");
113: } catch (IntrospectionException e) {
114: // this is expected
115: } catch (AssertionFailedError e) {
116: throw e;
117: } catch (Throwable t) {
118: t.printStackTrace();
119: fail("Unexpected error: " + t.toString());
120: }
121: }
122:
123: /**
124: * Tests <tt>MBeanAttributeInfo(String name, String descr, Method getter, Method setter)</tt> with invalid getter method (void return type).
125: */
126: public void testConstructorWithInvalidGetterMethod2() {
127: try {
128: Class c = Trivial.class;
129: Method getter = c.getMethod("getSomethingInvalid2",
130: new Class[] {});
131: Method setter = c.getMethod("setSomethingInvalid2",
132: new Class[] { String.class });
133:
134: MBeanAttributeInfo info = new MBeanAttributeInfo(
135: "Something", "a description", getter, setter);
136:
137: // shouldn't reach here
138: fail("Introspection exception should have been thrown.");
139: } catch (IntrospectionException e) {
140: // this is expected
141: } catch (AssertionFailedError e) {
142: throw e;
143: } catch (Throwable t) {
144: t.printStackTrace();
145: fail("Unexpected error: " + t.toString());
146: }
147: }
148:
149: public void testConstructorWithNonBooleanIsIs() throws Exception {
150: try {
151: new MBeanAttributeInfo("name", "type", "description", true,
152: true, true);
153: } catch (Exception e) {
154: return;
155: }
156: fail("isIs is only allowed for boolean types");
157: }
158:
159: public void testConstructorWithPrimitiveBooleanIsIs()
160: throws Exception {
161: new MBeanAttributeInfo("name", Boolean.TYPE.getName(),
162: "description", true, true, true);
163: }
164:
165: public void testConstructorWithObjectBooleanIsIs() throws Exception {
166: new MBeanAttributeInfo("name", Boolean.class.getName(),
167: "description", true, true, true);
168: }
169:
170: public void testInvalidJavaName() {
171: try {
172: new MBeanAttributeInfo("invalid name", "type",
173: "description", true, true, false);
174: } catch (Exception e) {
175: return;
176: }
177: fail("shouldn't allow an invalid java name");
178: }
179:
180: public void testInvalidJavaType() {
181: try {
182: new MBeanAttributeInfo("name", "invalid type",
183: "description", true, true, false);
184: } catch (Exception e) {
185: return;
186: }
187: fail("shouldn't allow an invalid java type");
188: }
189:
190: public void testHashCode() throws Exception {
191: MBeanAttributeInfo info1 = new MBeanAttributeInfo("name",
192: "type", "description", true, true, false);
193: MBeanAttributeInfo info2 = new MBeanAttributeInfo("name",
194: "type", "description", true, true, false);
195:
196: assertTrue(
197: "Different instances with the same hashcode are equal",
198: info1.hashCode() == info2.hashCode());
199: }
200:
201: public void testEquals() throws Exception {
202: MBeanAttributeInfo info = new MBeanAttributeInfo("name",
203: "type", "description", true, true, false);
204:
205: assertTrue("Null should not be equal",
206: info.equals(null) == false);
207: assertTrue("Only MBeanAttributeInfo should be equal", info
208: .equals(new Object()) == false);
209:
210: MBeanAttributeInfo info2 = new MBeanAttributeInfo("name",
211: "type", "description", true, true, false);
212:
213: assertTrue("Different instances of the same data are equal",
214: info.equals(info2));
215: assertTrue("Different instances of the same data are equal",
216: info2.equals(info));
217:
218: info2 = new MBeanAttributeInfo("name2", "type", "description",
219: true, true, false);
220:
221: assertTrue(
222: "Different instances with different names are not equal",
223: info.equals(info2) == false);
224: assertTrue(
225: "Different instances with different names are not equal",
226: info2.equals(info) == false);
227:
228: info2 = new MBeanAttributeInfo("name", "type2", "description",
229: true, true, false);
230:
231: assertTrue(
232: "Different instances with different types are not equal",
233: info.equals(info2) == false);
234: assertTrue(
235: "Different instances with different types are not equal",
236: info2.equals(info) == false);
237:
238: info2 = new MBeanAttributeInfo("name", "type", "description2",
239: true, true, false);
240:
241: assertTrue(
242: "Different instances with different descriptions are not equal",
243: info.equals(info2) == false);
244: assertTrue(
245: "Different instances with different descritpions are not equal",
246: info2.equals(info) == false);
247:
248: info2 = new MBeanAttributeInfo("name", "type", "description",
249: false, true, false);
250:
251: assertTrue(
252: "Different instances with different readables are not equal",
253: info.equals(info2) == false);
254: assertTrue(
255: "Different instances with different readables are not equal",
256: info2.equals(info) == false);
257:
258: info2 = new MBeanAttributeInfo("name", "type", "description",
259: true, false, false);
260:
261: assertTrue(
262: "Different instances with different writables are not equal",
263: info.equals(info2) == false);
264: assertTrue(
265: "Different instances with different writables are not equal",
266: info2.equals(info) == false);
267:
268: info2 = new MBeanAttributeInfo("name", Boolean.TYPE.getName(),
269: "description", true, true, true);
270:
271: assertTrue(
272: "Different instances with different isIs are not equal",
273: info.equals(info2) == false);
274: assertTrue(
275: "Different instances with different isIs are not equal",
276: info2.equals(info) == false);
277: }
278: }
|