001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package test.javax.management.openmbean;
008:
009: import java.util.Collection;
010: import javax.management.openmbean.CompositeDataSupport;
011: import javax.management.openmbean.CompositeType;
012: import javax.management.openmbean.OpenDataException;
013: import javax.management.openmbean.OpenType;
014: import javax.management.openmbean.SimpleType;
015: import javax.management.openmbean.TabularDataSupport;
016: import javax.management.openmbean.TabularType;
017:
018: import junit.framework.TestCase;
019:
020: /**
021: * @version $Revision: 1.7 $
022: */
023: public class CompositeDataSupportTest extends TestCase {
024: private String[] itemNames = null;
025: private String[] itemDescriptions = null;
026: private OpenType[] itemTypes;
027: private CompositeType tShirtType;
028:
029: private String[] indexNames;
030: private TabularType allTShirtTypes;
031: private TabularDataSupport tabularSupport;
032:
033: private CompositeDataSupport compositeData;
034:
035: public CompositeDataSupportTest(String s) {
036: super (s);
037: }
038:
039: protected void setUp() throws Exception {
040: super .setUp();
041: itemNames = new String[] { "model", "color", "size", "price" };
042: itemDescriptions = new String[] { "TShirt's model name",
043: "TShirt's color", "TShirt's size", "TShirt's price" };
044: itemTypes = new OpenType[] { SimpleType.STRING,
045: SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT };
046: indexNames = new String[] { "model", "color", "size" };
047: tShirtType = new CompositeType("tShirt", "a TShirt", itemNames,
048: itemDescriptions, itemTypes);
049:
050: allTShirtTypes = new TabularType("tShirts",
051: "List of available TShirts", tShirtType, // row type
052: indexNames);
053:
054: Object[] itemValues = new Object[] { "MX4J", "red", "L",
055: new Float(15.0f) };
056:
057: compositeData = new CompositeDataSupport(tShirtType, itemNames,
058: itemValues);
059: // takes tabular type
060: tabularSupport = new TabularDataSupport(allTShirtTypes);
061: }
062:
063: protected void tearDown() throws Exception {
064: super .tearDown();
065: }
066:
067: public void testConstructor() {
068: try {
069: Object[] itemValues = new Object[] { "MX4J", "red", "L",
070: new Float(15.0f) };
071: CompositeDataSupport support = new CompositeDataSupport(
072: tShirtType, itemNames, itemValues);
073: assertTrue(support != null);
074: } catch (OpenDataException e) {
075: e.printStackTrace();
076: }
077:
078: // test for bug #769086 including a null value
079: try {
080: Object[] itemValues = new Object[] { "MX4J", "red", null,
081: new Float(15.0f) };
082: CompositeDataSupport support = new CompositeDataSupport(
083: tShirtType, itemNames, itemValues);
084: assertTrue(support != null);
085: } catch (OpenDataException e) {
086: e.printStackTrace();
087: }
088: }
089:
090: public void testGet() {
091: String expected = "MX4J";
092: String obj = (String) compositeData.get("model");
093: assertTrue("expected was stored as the value against model",
094: expected.equals(obj));
095:
096: }
097:
098: public void testGetAll() {
099: int expectedLength = 4;
100: Object[] obj = compositeData.getAll(itemNames);
101: assertEquals(expectedLength, obj.length);
102: }
103:
104: public void testValues() {
105: int expected = 4;
106: Collection result = compositeData.values();
107: assertEquals(expected, result.size());
108: }
109:
110: public void testSparseValues() throws Exception {
111: Object[] sparsevalues = new Object[] { "MX4J", null, "L",
112: new Float(15.0f) };
113: CompositeDataSupport sparsedata = new CompositeDataSupport(
114: tShirtType, itemNames, sparsevalues);
115: assertTrue("Null instance", sparsedata != null);
116: }
117:
118: public void testEquals() throws Exception {
119: Object[] testvalues = { "MX4J", "White", "XL", new Float(15.0f) };
120: CompositeDataSupport cdone = new CompositeDataSupport(
121: tShirtType, itemNames, testvalues);
122:
123: assertFalse("cdone equals 'null'", cdone.equals(null));
124: assertFalse("cdone equals Integer value", cdone
125: .equals(new Integer(42)));
126:
127: String[] items = new String[] { "model", "color", "size",
128: "price" };
129: String[] descriptions = new String[] { "TShirt's model name",
130: "TShirt's color", "TShirt's size", "TShirt's price" };
131: OpenType[] types = new OpenType[] { SimpleType.STRING,
132: SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT };
133: CompositeType shirt = new CompositeType("tShirt",
134: "A 'Tee' Shirt", items, descriptions, types);
135: CompositeDataSupport cdtwo = new CompositeDataSupport(shirt,
136: items, testvalues);
137: assertTrue("cdtwo not equal to cdone", cdtwo.equals(cdone));
138:
139: cdone = new CompositeDataSupport(tShirtType, items,
140: new Object[] { "GAP", null, "S", new Float(30.0f) });
141: cdtwo = new CompositeDataSupport(shirt, items, new Object[] {
142: "GAP", null, "S", new Float(30.0f) });
143: assertTrue("sparse cdtwo not equal to sparse cdone", cdtwo
144: .equals(cdone));
145:
146: }
147: }
|