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.openmbean;
023:
024: import junit.framework.TestCase;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030: import java.util.Arrays;
031: import java.util.Set;
032:
033: import javax.management.openmbean.OpenMBeanParameterInfo;
034: import javax.management.openmbean.OpenMBeanParameterInfoSupport;
035: import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
036: import javax.management.openmbean.OpenType;
037: import javax.management.openmbean.SimpleType;
038:
039: /**
040: * Open MBean Constructor Info tests.<p>
041: *
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
043: */
044: public class OpenMBeanConstructorInfoSupportTestCase extends TestCase {
045: // Static --------------------------------------------------------------------
046:
047: // Attributes ----------------------------------------------------------------
048:
049: // Constructor ---------------------------------------------------------------
050:
051: /**
052: * Construct the test
053: */
054: public OpenMBeanConstructorInfoSupportTestCase(String s) {
055: super (s);
056: }
057:
058: // Tests ---------------------------------------------------------------------
059:
060: public void testOpenMBeanConstructorInfoSupport() throws Exception {
061: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
062: "name", "description", null);
063: assertEquals("name", info.getName());
064: assertEquals("description", info.getDescription());
065: assertEquals(0, info.getSignature().length);
066:
067: info = new OpenMBeanConstructorInfoSupport("name",
068: "description", new OpenMBeanParameterInfoSupport[0]);
069: assertEquals("name", info.getName());
070: assertEquals("description", info.getDescription());
071: assertEquals(0, info.getSignature().length);
072:
073: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
074: "name", "description", SimpleType.STRING) };
075: info = new OpenMBeanConstructorInfoSupport("name",
076: "description", parms);
077: assertEquals("name", info.getName());
078: assertEquals("description", info.getDescription());
079: assertEquals(1, info.getSignature().length);
080: }
081:
082: public void testEquals() throws Exception {
083: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
084: "name", "description", null);
085:
086: assertTrue("Null should not be equal",
087: info.equals(null) == false);
088: assertTrue("Only OpenMBeanConstructorInfo should be equal",
089: info.equals(new Object()) == false);
090:
091: OpenMBeanConstructorInfoSupport info2 = new OpenMBeanConstructorInfoSupport(
092: "name", "description", null);
093:
094: assertTrue("Different instances of the same data are equal",
095: info.equals(info2));
096: assertTrue("Different instances of the same data are equal",
097: info2.equals(info));
098:
099: info2 = new OpenMBeanConstructorInfoSupport("name",
100: "description2", null);
101:
102: assertTrue(
103: "Different instances with different descriptions are equal",
104: info.equals(info2));
105: assertTrue(
106: "Different instances with different descritpions are equal",
107: info2.equals(info));
108:
109: info2 = new OpenMBeanConstructorInfoSupport("name2",
110: "description", null);
111:
112: assertTrue("Instances with different names are not equal", info
113: .equals(info2) == false);
114: assertTrue("Instances with different names are not equal",
115: info2.equals(info) == false);
116:
117: OpenMBeanParameterInfoSupport param1 = new OpenMBeanParameterInfoSupport(
118: "name", "description", SimpleType.STRING);
119: OpenMBeanParameterInfoSupport param2 = new OpenMBeanParameterInfoSupport(
120: "name2", "description", SimpleType.STRING);
121:
122: info = new OpenMBeanConstructorInfoSupport("name",
123: "description", new OpenMBeanParameterInfoSupport[] {
124: param1, param2 });
125: info2 = new OpenMBeanConstructorInfoSupport("name",
126: "description", new OpenMBeanParameterInfoSupport[] {
127: param1, param2 });
128:
129: assertTrue(
130: "Different instances with the same parameters are equal",
131: info.equals(info2));
132: assertTrue(
133: "Different instances with the same parameters are equal",
134: info2.equals(info));
135:
136: info = new OpenMBeanConstructorInfoSupport("name",
137: "description", new OpenMBeanParameterInfoSupport[] {
138: param1, param2 });
139: info2 = new OpenMBeanConstructorInfoSupport("name",
140: "description", new OpenMBeanParameterInfoSupport[] {
141: param2, param1 });
142:
143: assertTrue(
144: "Different instances with the same signature but different parameters are not equal",
145: info.equals(info2) == false);
146: assertTrue(
147: "Different instances with the same signature but different parameters are not equal",
148: info2.equals(info) == false);
149:
150: param2 = new OpenMBeanParameterInfoSupport("name2",
151: "description", SimpleType.INTEGER);
152: info2 = new OpenMBeanConstructorInfoSupport("name",
153: "description", new OpenMBeanParameterInfoSupport[] {
154: param1, param2 });
155:
156: assertTrue(
157: "Different instances with different signatures are not equal",
158: info.equals(info2) == false);
159: assertTrue(
160: "Different instances with different signatures are not equal",
161: info2.equals(info) == false);
162:
163: info2 = new OpenMBeanConstructorInfoSupport("name",
164: "description",
165: new OpenMBeanParameterInfoSupport[] { param1 });
166:
167: assertTrue(
168: "Different instances with different numbers of paramters are not equal",
169: info.equals(info2) == false);
170: assertTrue(
171: "Different instances with different numbers of parameters are not equal",
172: info2.equals(info) == false);
173: }
174:
175: public void testHashCode() throws Exception {
176: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
177: "name", "description", SimpleType.STRING) };
178: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
179: "name", "description", parms);
180:
181: int myHash = "name".hashCode()
182: + Arrays.asList(parms).hashCode();
183: assertEquals(myHash, info.hashCode());
184: }
185:
186: public void testToString() throws Exception {
187: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
188: "name", "description", SimpleType.STRING) };
189: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
190: "NAME", "DESCRIPTION", parms);
191:
192: String toString = info.toString();
193:
194: assertTrue("info.toString() should contain NAME", toString
195: .indexOf("NAME") != -1);
196: assertTrue("info.toString() should contain the parameters",
197: toString.indexOf(Arrays.asList(parms).toString()) != -1);
198: }
199:
200: public void testSerialization() throws Exception {
201: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
202: "name", "description", SimpleType.STRING) };
203: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
204: "name", "description", parms);
205:
206: // Serialize it
207: ByteArrayOutputStream baos = new ByteArrayOutputStream();
208: ObjectOutputStream oos = new ObjectOutputStream(baos);
209: oos.writeObject(info);
210:
211: // Deserialize it
212: ByteArrayInputStream bais = new ByteArrayInputStream(baos
213: .toByteArray());
214: ObjectInputStream ois = new ObjectInputStream(bais);
215: Object result = ois.readObject();
216:
217: assertEquals(info, result);
218: }
219:
220: public void testErrors() throws Exception {
221: boolean caught = false;
222: try {
223: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
224: "name", "description", SimpleType.STRING) };
225: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
226: null, "description", parms);
227: } catch (IllegalArgumentException e) {
228: caught = true;
229: }
230: if (caught == false)
231: fail("Expected IllegalArgumentException for null name");
232:
233: caught = false;
234: try {
235: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
236: "name", "description", SimpleType.STRING) };
237: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
238: "", "description", parms);
239: } catch (IllegalArgumentException e) {
240: caught = true;
241: }
242: if (caught == false)
243: fail("Expected IllegalArgumentException for empty name");
244:
245: caught = false;
246: try {
247: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
248: "name", "description", SimpleType.STRING) };
249: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
250: "name", null, parms);
251: } catch (IllegalArgumentException e) {
252: caught = true;
253: }
254: if (caught == false)
255: fail("Expected IllegalArgumentException for null description");
256:
257: caught = false;
258: try {
259: OpenMBeanParameterInfoSupport[] parms = new OpenMBeanParameterInfoSupport[] { new OpenMBeanParameterInfoSupport(
260: "name", "description", SimpleType.STRING) };
261: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
262: "name", "", parms);
263: } catch (IllegalArgumentException e) {
264: caught = true;
265: }
266: if (caught == false)
267: fail("Expected IllegalArgumentException for empty description");
268:
269: caught = false;
270: try {
271: OpenMBeanConstructorInfoSupport info = new OpenMBeanConstructorInfoSupport(
272: "name",
273: "description",
274: new MyOpenMBeanParameterInfo[] { new MyOpenMBeanParameterInfo() });
275: } catch (ArrayStoreException e) {
276: caught = true;
277: }
278: if (caught == false)
279: fail("Expected ArrayStoreException for non MBeanParameterInfo array");
280: }
281:
282: public static class MyOpenMBeanParameterInfo implements
283: OpenMBeanParameterInfo {
284: public boolean equals(Object o) {
285: return false;
286: }
287:
288: public Object getDefaultValue() {
289: return null;
290: }
291:
292: public String getDescription() {
293: return null;
294: }
295:
296: public Set getLegalValues() {
297: return null;
298: }
299:
300: public Comparable getMaxValue() {
301: return null;
302: }
303:
304: public Comparable getMinValue() {
305: return null;
306: }
307:
308: public String getName() {
309: return null;
310: }
311:
312: public OpenType getOpenType() {
313: return null;
314: }
315:
316: public boolean hasDefaultValue() {
317: return false;
318: }
319:
320: public boolean hasLegalValues() {
321: return false;
322: }
323:
324: public int hashCode() {
325: return 0;
326: }
327:
328: public boolean hasMaxValue() {
329: return false;
330: }
331:
332: public boolean hasMinValue() {
333: return false;
334: }
335:
336: public boolean isValue(Object o) {
337: return false;
338: }
339:
340: public String toString() {
341: return null;
342: }
343: }
344: }
|