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 java.util.Set;
011: import javax.management.openmbean.CompositeData;
012: import javax.management.openmbean.CompositeDataSupport;
013: import javax.management.openmbean.CompositeType;
014: import javax.management.openmbean.OpenType;
015: import javax.management.openmbean.SimpleType;
016: import javax.management.openmbean.TabularData;
017: import javax.management.openmbean.TabularDataSupport;
018: import javax.management.openmbean.TabularType;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * @version $Revision: 1.10 $
024: */
025: public class TabularDataSupportTest extends TestCase {
026: private String[] itemNames = null;
027: private String[] itemDescriptions = null;
028: private OpenType[] itemTypes;
029: private String[] indexNames;
030: private CompositeType tShirtType;
031: private TabularType allTShirtTypes;
032: private TabularDataSupport tabularSupport;
033: private CompositeData compositeData;
034:
035: public TabularDataSupportTest(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 testCreation() {
068: TabularDataSupport tabularData2 = new TabularDataSupport(
069: allTShirtTypes);
070: assertTrue(tabularData2 != null);
071: }
072:
073: public void testPut() {
074: try {
075: tabularSupport.put(compositeData);
076: assertTrue("tabularSupport doesn't contain compositeData",
077: tabularSupport.containsValue(compositeData));
078: } catch (Exception e) {
079: e.printStackTrace();
080: }
081: }
082:
083: public void testPutAll() throws Exception {
084: CompositeData snmpshirt = new CompositeDataSupport(tShirtType,
085: itemNames, new Object[] { "SNMP", "green", null,
086: new Float(15.0f) });
087: CompositeData[] shirts = { compositeData, snmpshirt };
088: tabularSupport.putAll(shirts);
089: assertTrue("tabularSupport doesn't contain compositeData",
090: tabularSupport.containsValue(compositeData));
091: assertTrue("tabularSupport doesn't contain snmpshirt",
092: tabularSupport.containsValue(snmpshirt));
093: }
094:
095: public void testRemove() throws Exception {
096: CompositeData snmpshirt = new CompositeDataSupport(tShirtType,
097: itemNames, new Object[] { "SNMP", "green", null,
098: new Float(15.0f) });
099: CompositeData[] shirts = { compositeData, snmpshirt };
100: tabularSupport.putAll(shirts);
101: CompositeData oldshirt = tabularSupport.remove(new Object[] {
102: "SNMP", "green", null });
103: assertFalse("oldshirt is null", oldshirt == null);
104: assertTrue("Expecting oldshirt equals snmpshirt", snmpshirt
105: .equals(oldshirt));
106: }
107:
108: public void testGetTabularType() {
109: TabularType toVerify = tabularSupport.getTabularType();
110: assertEquals(toVerify, allTShirtTypes);
111: }
112:
113: public void testCalculateIndex() {
114: // returns an array of the indexNames as represented by the simpleTypes(itemTypes) ie returns the values for the index names which are the simpleTypes
115: Object[] object = tabularSupport.calculateIndex(compositeData);
116: assertTrue(object.length == indexNames.length);
117: }
118:
119: public void testContainsValue() {
120: OpenType[] keyTypes = new OpenType[] { SimpleType.STRING,
121: SimpleType.STRING, SimpleType.STRING };
122: tabularSupport.put(compositeData);
123: boolean expected = tabularSupport.containsValue(compositeData);
124: assertTrue(expected);
125: }
126:
127: public void testContainsValueMissingValue() throws Exception {
128: CompositeData snmpshirt = new CompositeDataSupport(tShirtType,
129: itemNames, new Object[] { "SNMP", "green", "M",
130: new Float(15.0f) });
131: tabularSupport.put(compositeData);
132: tabularSupport.put(snmpshirt);
133: assertTrue("Should contain MX4J and SNMP shirts",
134: tabularSupport.containsValue(compositeData)
135: && tabularSupport.containsValue(snmpshirt));
136: CompositeData jmxshirt = new CompositeDataSupport(tShirtType,
137: itemNames, new Object[] { "JMX", "blue", "M",
138: new Float(15.0f) });
139: assertFalse("Contains JMX shirt", tabularSupport
140: .containsValue(jmxshirt));
141: CompositeData bogusshirt = new CompositeDataSupport(tShirtType,
142: itemNames, new Object[] { "Bogus", null, "M",
143: new Float(15.0f) });
144: assertFalse("Contains Bogus shirt", tabularSupport
145: .containsValue(bogusshirt));
146: }
147:
148: public void testContainsValueNullValue() throws Exception {
149: assertFalse("Contains 'null'", tabularSupport
150: .containsValue(null));
151: }
152:
153: public void testKeySet() {
154: tabularSupport.put(compositeData);
155: Set set = tabularSupport.keySet();
156: assertTrue(set.size() == 1);
157: }
158:
159: public void testValues() {
160: tabularSupport.put(compositeData);
161: Collection values = tabularSupport.values();
162: assertTrue(values.contains(compositeData));
163: }
164:
165: public void testEquals() throws Exception {
166: assertFalse("Equal to 'null'", tabularSupport.equals(null));
167: assertFalse("Equal to Non-TabularData", tabularSupport
168: .equals(new Integer(42)));
169:
170: String[] items = { "model", "color", "size", "price" };
171:
172: String[] usdescriptions = { "Manufacturer's model name",
173: "The shirt's color", "How big is it",
174: "How much does it cost (dollars)" };
175: String[] eurodescriptions = { "Designer's model name",
176: "The hue of the garment", "Garment size",
177: "How much does it cost (euros)" };
178:
179: OpenType[] types = { SimpleType.STRING, SimpleType.STRING,
180: SimpleType.STRING, SimpleType.FLOAT };
181: String[] indices = { "model", "color", "size" };
182:
183: CompositeType usst = new CompositeType("tShirt", "US Shirt",
184: items, usdescriptions, types);
185: CompositeType eurost = new CompositeType("tShirt",
186: "European Shirt", items, usdescriptions, types);
187:
188: TabularType usstt = new TabularType("tShirts",
189: "List of available US Shirts", usst, indices);
190: TabularType eurostt = new TabularType("tShirts",
191: "List of available European Shirts", eurost, indices);
192:
193: TabularData ussdata = new TabularDataSupport(usstt);
194: TabularData eurosdata = new TabularDataSupport(eurostt);
195: assertTrue("Expecting equality for tabular shirt data", ussdata
196: .equals(eurosdata));
197: assertTrue("Expecting equal hash codes for equal data",
198: eurosdata.hashCode() == ussdata.hashCode());
199:
200: OpenType[] txtypes = { SimpleType.STRING, SimpleType.STRING,
201: SimpleType.STRING, SimpleType.DOUBLE };
202: CompositeType txst = new CompositeType("tShirt", "Texas Shirt",
203: items, usdescriptions, txtypes);
204: TabularType txstt = new TabularType("tShirts",
205: "List of available Texas Shirts", txst, indices);
206: TabularData txsdata = new TabularDataSupport(txstt);
207: assertFalse("Texas shirt equals US shirt", ussdata
208: .equals(txsdata));
209:
210: CompositeData ussnmpshirt = new CompositeDataSupport(usst,
211: items, new Object[] { "SNMP", "green", "M",
212: new Float(15.0f) });
213: CompositeData usjmxshirt = new CompositeDataSupport(usst,
214: items, new Object[] { "JMX", "blue", "M",
215: new Float(15.0f) });
216: ussdata.put(ussnmpshirt);
217: ussdata.put(usjmxshirt);
218:
219: CompositeData eurosnmpshirt = new CompositeDataSupport(usst,
220: items, new Object[] { "SNMP", "green", "M",
221: new Float(15.0f) });
222: CompositeData eurojmxshirt = new CompositeDataSupport(usst,
223: items, new Object[] { "JMX", "blue", "M",
224: new Float(15.0f) });
225: eurosdata.put(eurosnmpshirt);
226: eurosdata.put(eurojmxshirt);
227:
228: assertTrue("Expecting US and Euro shirt data to be equal",
229: ussdata.equals(eurosdata));
230: assertTrue(
231: "Expecting US and Euro shirt data hash codes to be equal",
232: eurosdata.hashCode() == ussdata.hashCode());
233: int ushashcode = ussdata.getTabularType().hashCode();
234: ushashcode += ussnmpshirt.hashCode();
235: ushashcode += usjmxshirt.hashCode();
236: assertTrue("Unexpected hash code computation", ussdata
237: .hashCode() == ushashcode);
238: }
239: }
|