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.Set;
031:
032: import javax.management.openmbean.CompositeData;
033: import javax.management.openmbean.CompositeDataSupport;
034: import javax.management.openmbean.CompositeType;
035: import javax.management.openmbean.OpenDataException;
036: import javax.management.openmbean.OpenType;
037: import javax.management.openmbean.SimpleType;
038:
039: /**
040: * Composite type tests.<p>
041: *
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
043: */
044: public class CompositeTypeTestCase extends TestCase {
045: // Static --------------------------------------------------------------------
046:
047: // Attributes ----------------------------------------------------------------
048:
049: // Constructor ---------------------------------------------------------------
050:
051: /**
052: * Construct the test
053: */
054: public CompositeTypeTestCase(String s) {
055: super (s);
056: }
057:
058: // Tests ---------------------------------------------------------------------
059:
060: public void testCompositeTypeOpenType() throws Exception {
061: String[] itemNames = new String[] { "name1", "name2" };
062: String[] itemDescriptions = new String[] { "desc1", "desc2" };
063: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
064: SimpleType.INTEGER };
065: CompositeType compositeType = new CompositeType("typeName",
066: "description", itemNames, itemDescriptions, itemTypes);
067: assertEquals(CompositeData.class.getName(), compositeType
068: .getClassName());
069: assertEquals("description", compositeType.getDescription());
070: assertEquals("typeName", compositeType.getTypeName());
071: assertTrue("Composite type should not be an array",
072: compositeType.isArray() == false);
073: }
074:
075: public void testContainsKey() throws Exception {
076: String[] itemNames = new String[] { "name1", "name2" };
077: String[] itemDescriptions = new String[] { "desc1", "desc2" };
078: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
079: SimpleType.INTEGER };
080: CompositeType compositeType = new CompositeType("typeName",
081: "description", itemNames, itemDescriptions, itemTypes);
082: assertTrue("Composite type should contain key name1",
083: compositeType.containsKey("name1") == true);
084: assertTrue("Composite type should contain key name2",
085: compositeType.containsKey("name2") == true);
086: assertTrue("Composite type should not contain key nameX",
087: compositeType.containsKey("nameX") == false);
088: assertTrue("Composite type should not contain key null",
089: compositeType.containsKey(null) == false);
090: assertTrue("Composite type should not contain key <empty>",
091: compositeType.containsKey("") == false);
092: }
093:
094: public void testGetDescriptionForItemName() throws Exception {
095: String[] itemNames = new String[] { "name1", "name2" };
096: String[] itemDescriptions = new String[] { "desc1", "desc2" };
097: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
098: SimpleType.INTEGER };
099: CompositeType compositeType = new CompositeType("typeName",
100: "description", itemNames, itemDescriptions, itemTypes);
101: assertEquals("desc1", compositeType.getDescription("name1"));
102: assertEquals("desc2", compositeType.getDescription("name2"));
103: }
104:
105: public void testGetTypeForItemName() throws Exception {
106: String[] itemNames = new String[] { "name1", "name2" };
107: String[] itemDescriptions = new String[] { "desc1", "desc2" };
108: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
109: SimpleType.INTEGER };
110: CompositeType compositeType = new CompositeType("typeName",
111: "description", itemNames, itemDescriptions, itemTypes);
112: assertEquals(SimpleType.STRING, compositeType.getType("name1"));
113: assertEquals(SimpleType.INTEGER, compositeType.getType("name2"));
114: }
115:
116: public void testKeySet() throws Exception {
117: String[] itemNames = new String[] { "name1", "name2" };
118: String[] itemDescriptions = new String[] { "desc1", "desc2" };
119: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
120: SimpleType.INTEGER };
121: CompositeType compositeType = new CompositeType("typeName",
122: "description", itemNames, itemDescriptions, itemTypes);
123: Set keys = compositeType.keySet();
124: assertTrue("Should be 2 items", keys.size() == 2);
125: assertTrue("Should contain name1", keys.contains("name1"));
126: assertTrue("Should contain name2", keys.contains("name2"));
127: }
128:
129: public void testIsValue() throws Exception {
130: String[] itemNames = new String[] { "name1", "name2" };
131: String[] itemDescriptions = new String[] { "desc1", "desc2" };
132: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
133: SimpleType.INTEGER };
134: CompositeType compositeType = new CompositeType("typeName",
135: "description", itemNames, itemDescriptions, itemTypes);
136:
137: assertTrue("null is not a value of composite type",
138: compositeType.isValue(null) == false);
139: assertTrue("object is not a value of composite type",
140: compositeType.isValue(new Object()) == false);
141:
142: Object[] itemValues = new Object[] { "string", new Integer(2) };
143: CompositeDataSupport data = new CompositeDataSupport(
144: compositeType, itemNames, itemValues);
145: assertTrue("data should be a value of composite type",
146: compositeType.isValue(data));
147:
148: CompositeType compositeType2 = new CompositeType("typeName",
149: "description", itemNames, itemDescriptions, itemTypes);
150: data = new CompositeDataSupport(compositeType2, itemNames,
151: itemValues);
152: assertTrue(
153: "data should be a value of composite type, even though not the object instance",
154: compositeType.isValue(data));
155:
156: OpenType[] itemTypes2 = new OpenType[] { SimpleType.STRING,
157: SimpleType.LONG };
158: compositeType2 = new CompositeType("typeName", "description",
159: itemNames, itemDescriptions, itemTypes2);
160: Object[] itemValues2 = new Object[] { "string", new Long(2) };
161: data = new CompositeDataSupport(compositeType2, itemNames,
162: itemValues2);
163: assertTrue(
164: "data should not be a value of composite type, it has different types",
165: compositeType.isValue(data) == false);
166:
167: compositeType2 = new CompositeType("typeName2", "description",
168: itemNames, itemDescriptions, itemTypes);
169: data = new CompositeDataSupport(compositeType2, itemNames,
170: itemValues);
171: assertTrue(
172: "data should not be a value of composite type, it has a different type name",
173: compositeType.isValue(data) == false);
174:
175: String[] itemNames2 = new String[] { "nameX", "name2" };
176: compositeType2 = new CompositeType("typeName", "description",
177: itemNames2, itemDescriptions, itemTypes);
178: data = new CompositeDataSupport(compositeType2, itemNames2,
179: itemValues);
180: assertTrue(
181: "data should not be a value of composite type, it has different item names",
182: compositeType.isValue(data) == false);
183: }
184:
185: public void testEquals() throws Exception {
186: String[] itemNames = new String[] { "name1", "name2" };
187: String[] itemDescriptions = new String[] { "desc1", "desc2" };
188: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
189: SimpleType.INTEGER };
190: CompositeType compositeType = new CompositeType("typeName",
191: "description", itemNames, itemDescriptions, itemTypes);
192:
193: assertTrue("null is not equal composite type", compositeType
194: .equals(null) == false);
195: assertTrue("object is not equal composite type", compositeType
196: .equals(new Object()) == false);
197:
198: CompositeType compositeType2 = new CompositeType("typeName",
199: "description", itemNames, itemDescriptions, itemTypes);
200: assertTrue(
201: "compositeType2 should be equal composite type, even though not the object instance",
202: compositeType.equals(compositeType2));
203: assertTrue(
204: "compositeType2 should be equal composite type, even though not the object instance",
205: compositeType2.equals(compositeType));
206:
207: OpenType[] itemTypes2 = new OpenType[] { SimpleType.STRING,
208: SimpleType.LONG };
209: compositeType2 = new CompositeType("typeName", "description",
210: itemNames, itemDescriptions, itemTypes2);
211: assertTrue(
212: "compositeType2 should not be equal composite type, it has different types",
213: compositeType.equals(compositeType2) == false);
214: assertTrue(
215: "compositeType2 should not be equal composite type, it has different types",
216: compositeType2.equals(compositeType) == false);
217:
218: compositeType2 = new CompositeType("typeName2", "description",
219: itemNames, itemDescriptions, itemTypes);
220: assertTrue(
221: "compositeType2 should not be equal composite type, it has a different type name",
222: compositeType.equals(compositeType2) == false);
223: assertTrue(
224: "compositeType2 should not be equal composite type, it has a different type name",
225: compositeType2.equals(compositeType) == false);
226:
227: String[] itemNames2 = new String[] { "nameX", "name2" };
228: compositeType2 = new CompositeType("typeName", "description",
229: itemNames2, itemDescriptions, itemTypes);
230: assertTrue(
231: "compositeType2 should not be equal composite type, it has different item names",
232: compositeType.equals(compositeType2) == false);
233: assertTrue(
234: "compositeType2 should not be equal composite type, it has different item names",
235: compositeType2.equals(compositeType) == false);
236: }
237:
238: public void testHashCode() throws Exception {
239: String[] itemNames = new String[] { "name1", "name2" };
240: String[] itemDescriptions = new String[] { "desc1", "desc2" };
241: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
242: SimpleType.INTEGER };
243: CompositeType compositeType = new CompositeType("typeName",
244: "description", itemNames, itemDescriptions, itemTypes);
245:
246: int myHashCode = "typeName".hashCode()
247: + SimpleType.STRING.hashCode()
248: + SimpleType.INTEGER.hashCode() + "name1".hashCode()
249: + "name2".hashCode();
250: assertTrue("Wrong hash code generated",
251: myHashCode == compositeType.hashCode());
252: }
253:
254: public void testToString() throws Exception {
255: String[] itemNames = new String[] { "name1", "name2" };
256: String[] itemDescriptions = new String[] { "desc1", "desc2" };
257: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
258: SimpleType.INTEGER };
259: CompositeType compositeType = new CompositeType("typeName",
260: "description", itemNames, itemDescriptions, itemTypes);
261:
262: String toString = compositeType.toString();
263:
264: assertTrue(
265: "toString() should contain the composite type class name",
266: toString.indexOf(CompositeType.class.getName()) != -1);
267: assertTrue("toString() should contain the item name name1",
268: toString.indexOf("name1") != -1);
269: assertTrue("toString() should contain the item name name2",
270: toString.indexOf("name2") != -1);
271: assertTrue("toString() should contain " + SimpleType.STRING,
272: toString.indexOf(SimpleType.STRING.toString()) != -1);
273: assertTrue("toString() should contain " + SimpleType.INTEGER,
274: toString.indexOf(SimpleType.INTEGER.toString()) != -1);
275: }
276:
277: public void testSerialization() throws Exception {
278: String[] itemNames = new String[] { "name1", "name2" };
279: String[] itemDescriptions = new String[] { "desc1", "desc2" };
280: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
281: SimpleType.INTEGER };
282: CompositeType compositeType = new CompositeType("typeName",
283: "description", itemNames, itemDescriptions, itemTypes);
284:
285: // Serialize it
286: ByteArrayOutputStream baos = new ByteArrayOutputStream();
287: ObjectOutputStream oos = new ObjectOutputStream(baos);
288: oos.writeObject(compositeType);
289:
290: // Deserialize it
291: ByteArrayInputStream bais = new ByteArrayInputStream(baos
292: .toByteArray());
293: ObjectInputStream ois = new ObjectInputStream(bais);
294: Object result = ois.readObject();
295:
296: assertEquals(compositeType, result);
297: }
298:
299: public void testErrors() throws Exception {
300: String[] itemNames = new String[] { "name1", "name2" };
301: String[] itemDescriptions = new String[] { "desc1", "desc2" };
302: OpenType[] itemTypes = new OpenType[] { SimpleType.STRING,
303: SimpleType.INTEGER };
304:
305: boolean caught = false;
306: try {
307: new CompositeType(null, "description", itemNames,
308: itemDescriptions, itemTypes);
309: } catch (IllegalArgumentException e) {
310: caught = true;
311: }
312: if (caught == false)
313: fail("Excepted IllegalArgumentException for null typeName");
314:
315: caught = false;
316: try {
317: new CompositeType("", "description", itemNames,
318: itemDescriptions, itemTypes);
319: } catch (IllegalArgumentException e) {
320: caught = true;
321: }
322: if (caught == false)
323: fail("Excepted IllegalArgumentException for empty typeName");
324:
325: caught = false;
326: try {
327: new CompositeType("typeName", null, itemNames,
328: itemDescriptions, itemTypes);
329: } catch (IllegalArgumentException e) {
330: caught = true;
331: }
332: if (caught == false)
333: fail("Excepted IllegalArgumentException for null description");
334:
335: caught = false;
336: try {
337: new CompositeType("typeName", "", itemNames,
338: itemDescriptions, itemTypes);
339: } catch (IllegalArgumentException e) {
340: caught = true;
341: }
342: if (caught == false)
343: fail("Excepted IllegalArgumentException for empty description");
344:
345: caught = false;
346: try {
347: new CompositeType("typeName", "description", null,
348: itemDescriptions, itemTypes);
349: } catch (IllegalArgumentException e) {
350: caught = true;
351: }
352: if (caught == false)
353: fail("Excepted IllegalArgumentException for null item names");
354:
355: caught = false;
356: try {
357: new CompositeType("typeName", "description", itemNames,
358: null, itemTypes);
359: } catch (IllegalArgumentException e) {
360: caught = true;
361: }
362: if (caught == false)
363: fail("Excepted IllegalArgumentException for null item descriptions");
364:
365: caught = false;
366: try {
367: new CompositeType("typeName", "description", itemNames,
368: itemDescriptions, null);
369: } catch (IllegalArgumentException e) {
370: caught = true;
371: }
372: if (caught == false)
373: fail("Excepted IllegalArgumentException for null item types");
374:
375: String[] nullItemNames = new String[] { "name1", null };
376: caught = false;
377: try {
378: new CompositeType("typeName", "description", nullItemNames,
379: itemDescriptions, itemTypes);
380: } catch (IllegalArgumentException e) {
381: caught = true;
382: }
383: if (caught == false)
384: fail("Excepted IllegalArgumentException for null element of item names");
385:
386: String[] nullItemDescriptions = new String[] { "desc1", null };
387: caught = false;
388: try {
389: new CompositeType("typeName", "description", itemNames,
390: nullItemDescriptions, itemTypes);
391: } catch (IllegalArgumentException e) {
392: caught = true;
393: }
394: if (caught == false)
395: fail("Excepted IllegalArgumentException for null element of item descriptions");
396:
397: OpenType[] nullItemTypes = new OpenType[] { SimpleType.STRING,
398: null };
399: caught = false;
400: try {
401: new CompositeType("typeName", "description", itemNames,
402: itemDescriptions, nullItemTypes);
403: } catch (IllegalArgumentException e) {
404: caught = true;
405: }
406: if (caught == false)
407: fail("Excepted IllegalArgumentException for null element of item types");
408:
409: String[] wrongItemNames = new String[] { "name1" };
410: caught = false;
411: try {
412: new CompositeType("typeName", "description",
413: wrongItemNames, itemDescriptions, itemTypes);
414: } catch (IllegalArgumentException e) {
415: caught = true;
416: }
417: if (caught == false)
418: fail("Excepted IllegalArgumentException for wrong number of elements for item names");
419:
420: String[] wrongItemDescriptions = new String[] { "desc1" };
421: caught = false;
422: try {
423: new CompositeType("typeName", "description", itemNames,
424: wrongItemDescriptions, itemTypes);
425: } catch (IllegalArgumentException e) {
426: caught = true;
427: }
428: if (caught == false)
429: fail("Excepted IllegalArgumentException for wrong number of elements for item descriptions");
430:
431: OpenType[] wrongItemTypes = new OpenType[] { SimpleType.STRING };
432: caught = false;
433: try {
434: new CompositeType("typeName", "description", itemNames,
435: itemDescriptions, wrongItemTypes);
436: } catch (IllegalArgumentException e) {
437: caught = true;
438: }
439: if (caught == false)
440: fail("Excepted IllegalArgumentException for wrong number of elements for item types");
441:
442: String[] duplicateItemNames = new String[] { "desc1", "desc1" };
443: caught = false;
444: try {
445: new CompositeType("typeName", "description",
446: duplicateItemNames, itemDescriptions, itemTypes);
447: } catch (OpenDataException e) {
448: caught = true;
449: }
450: if (caught == false)
451: fail("Excepted OpenDataException for duplicate item names");
452:
453: duplicateItemNames = new String[] { "desc1", " desc1 " };
454: caught = false;
455: try {
456: new CompositeType("typeName", "description",
457: duplicateItemNames, itemDescriptions, itemTypes);
458: } catch (OpenDataException e) {
459: caught = true;
460: }
461: if (caught == false)
462: fail("Excepted OpenDataException for duplicate item names");
463: }
464:
465: // Support -------------------------------------------------------------------
466: }
|