1 8 package test.javax.management.openmbean; 9 10 import java.util.Collection ; 11 import java.util.Set ; 12 import javax.management.openmbean.CompositeData ; 13 import javax.management.openmbean.CompositeDataSupport ; 14 import javax.management.openmbean.CompositeType ; 15 import javax.management.openmbean.OpenType ; 16 import javax.management.openmbean.SimpleType ; 17 import javax.management.openmbean.TabularData ; 18 import javax.management.openmbean.TabularDataSupport ; 19 import javax.management.openmbean.TabularType ; 20 21 import junit.framework.TestCase; 22 23 26 public class TabularDataSupportTest extends TestCase 27 { 28 private String [] itemNames = null; 29 private String [] itemDescriptions = null; 30 private OpenType [] itemTypes; 31 private String [] indexNames; 32 private CompositeType tShirtType; 33 private TabularType allTShirtTypes; 34 private TabularDataSupport tabularSupport; 35 private CompositeData compositeData; 36 37 public TabularDataSupportTest(String s) 38 { 39 super(s); 40 } 41 42 protected void setUp() throws Exception  43 { 44 super.setUp(); 45 itemNames = new String []{"model", "color", "size", "price"}; 46 itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 47 itemTypes = new OpenType []{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT}; 48 indexNames = new String []{"model", "color", "size"}; 49 tShirtType = new CompositeType ("tShirt", 50 "a TShirt", 51 itemNames, 52 itemDescriptions, 53 itemTypes); 54 55 allTShirtTypes = new TabularType ("tShirts", 56 "List of available TShirts", 57 tShirtType, indexNames); 59 60 Object [] itemValues = new Object []{"MX4J", "red", "L", new Float (15.0f)}; 61 62 compositeData = new CompositeDataSupport (tShirtType, itemNames, itemValues); 63 tabularSupport = new TabularDataSupport (allTShirtTypes); 65 } 66 67 protected void tearDown() throws Exception  68 { 69 super.tearDown(); 70 } 71 72 public void testCreation() 73 { 74 TabularDataSupport tabularData2 = new TabularDataSupport (allTShirtTypes); 75 assertTrue(tabularData2 != null); 76 } 77 78 public void testPut() 79 { 80 try 81 { 82 tabularSupport.put(compositeData); 83 assertTrue("tabularSupport doesn't contain compositeData", tabularSupport.containsValue(compositeData)); 84 } 85 catch (Exception e) 86 { 87 e.printStackTrace(); 88 } 89 } 90 91 public void testPutAll() throws Exception  92 { 93 CompositeData snmpshirt = 94 new CompositeDataSupport (tShirtType, 95 itemNames, 96 new Object []{"SNMP", "green", null, new Float (15.0f)}); 97 CompositeData [] shirts = {compositeData, snmpshirt}; 98 tabularSupport.putAll(shirts); 99 assertTrue("tabularSupport doesn't contain compositeData", tabularSupport.containsValue(compositeData)); 100 assertTrue("tabularSupport doesn't contain snmpshirt", tabularSupport.containsValue(snmpshirt)); 101 } 102 103 public void testRemove() throws Exception  104 { 105 CompositeData snmpshirt = 106 new CompositeDataSupport (tShirtType, 107 itemNames, 108 new Object []{"SNMP", "green", null, new Float (15.0f)}); 109 CompositeData [] shirts = {compositeData, snmpshirt}; 110 tabularSupport.putAll(shirts); 111 CompositeData oldshirt = tabularSupport.remove(new Object []{"SNMP", "green", null}); 112 assertFalse("oldshirt is null", oldshirt == null); 113 assertTrue("Expecting oldshirt equals snmpshirt", snmpshirt.equals(oldshirt)); 114 } 115 116 public void testGetTabularType() 117 { 118 TabularType toVerify = tabularSupport.getTabularType(); 119 assertEquals(toVerify, allTShirtTypes); 120 } 121 122 123 public void testCalculateIndex() 124 { 125 Object [] object = tabularSupport.calculateIndex(compositeData); 127 assertTrue(object.length == indexNames.length); 128 } 129 130 public void testContainsValue() 131 { 132 OpenType [] keyTypes = new OpenType []{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING}; 133 tabularSupport.put(compositeData); 134 boolean expected = tabularSupport.containsValue(compositeData); 135 assertTrue(expected); 136 } 137 138 public void testContainsValueMissingValue() throws Exception  139 { 140 CompositeData snmpshirt = 141 new CompositeDataSupport (tShirtType, 142 itemNames, 143 new Object []{"SNMP", "green", "M", new Float (15.0f)}); 144 tabularSupport.put(compositeData); 145 tabularSupport.put(snmpshirt); 146 assertTrue("Should contain MX4J and SNMP shirts", 147 tabularSupport.containsValue(compositeData) 148 && tabularSupport.containsValue(snmpshirt)); 149 CompositeData jmxshirt = 150 new CompositeDataSupport (tShirtType, 151 itemNames, 152 new Object []{"JMX", "blue", "M", new Float (15.0f)}); 153 assertFalse("Contains JMX shirt", 154 tabularSupport.containsValue(jmxshirt)); 155 CompositeData bogusshirt = 156 new CompositeDataSupport (tShirtType, 157 itemNames, 158 new Object []{"Bogus", null, "M", new Float (15.0f)}); 159 assertFalse("Contains Bogus shirt", 160 tabularSupport.containsValue(bogusshirt)); 161 } 162 163 public void testContainsValueNullValue() throws Exception  164 { 165 assertFalse("Contains 'null'", tabularSupport.containsValue(null)); 166 } 167 168 public void testKeySet() 169 { 170 tabularSupport.put(compositeData); 171 Set set = tabularSupport.keySet(); 172 assertTrue(set.size() == 1); 173 } 174 175 176 public void testValues() 177 { 178 tabularSupport.put(compositeData); 179 Collection values = tabularSupport.values(); 180 assertTrue(values.contains(compositeData)); 181 } 182 183 public void testEquals() throws Exception  184 { 185 assertFalse("Equal to 'null'", tabularSupport.equals(null)); 186 assertFalse("Equal to Non-TabularData", 187 tabularSupport.equals(new Integer (42))); 188 189 String [] items = {"model", "color", "size", "price"}; 190 191 String [] usdescriptions = 192 { 193 "Manufacturer's model name", 194 "The shirt's color", 195 "How big is it", 196 "How much does it cost (dollars)"}; 197 String [] eurodescriptions = 198 { 199 "Designer's model name", 200 "The hue of the garment", 201 "Garment size", 202 "How much does it cost (euros)"}; 203 204 OpenType [] types = 205 { 206 SimpleType.STRING, 207 SimpleType.STRING, 208 SimpleType.STRING, 209 SimpleType.FLOAT}; 210 String [] indices = {"model", "color", "size"}; 211 212 CompositeType usst = 213 new CompositeType ("tShirt", 214 "US Shirt", 215 items, 216 usdescriptions, 217 types); 218 CompositeType eurost = 219 new CompositeType ("tShirt", 220 "European Shirt", 221 items, 222 usdescriptions, 223 types); 224 225 TabularType usstt = 226 new TabularType ("tShirts", 227 "List of available US Shirts", 228 usst, 229 indices); 230 TabularType eurostt = 231 new TabularType ("tShirts", 232 "List of available European Shirts", 233 eurost, 234 indices); 235 236 TabularData ussdata = new TabularDataSupport (usstt); 237 TabularData eurosdata = new TabularDataSupport (eurostt); 238 assertTrue("Expecting equality for tabular shirt data", 239 ussdata.equals(eurosdata)); 240 assertTrue("Expecting equal hash codes for equal data", 241 eurosdata.hashCode() == ussdata.hashCode()); 242 243 OpenType [] txtypes = 244 { 245 SimpleType.STRING, 246 SimpleType.STRING, 247 SimpleType.STRING, 248 SimpleType.DOUBLE}; 249 CompositeType txst = 250 new CompositeType ("tShirt", 251 "Texas Shirt", 252 items, 253 usdescriptions, 254 txtypes); 255 TabularType txstt = 256 new TabularType ("tShirts", 257 "List of available Texas Shirts", 258 txst, 259 indices); 260 TabularData txsdata = new TabularDataSupport (txstt); 261 assertFalse("Texas shirt equals US shirt", ussdata.equals(txsdata)); 262 263 CompositeData ussnmpshirt = 264 new CompositeDataSupport (usst, 265 items, 266 new Object []{"SNMP", "green", "M", new Float (15.0f)}); 267 CompositeData usjmxshirt = 268 new CompositeDataSupport (usst, 269 items, 270 new Object []{"JMX", "blue", "M", new Float (15.0f)}); 271 ussdata.put(ussnmpshirt); 272 ussdata.put(usjmxshirt); 273 274 CompositeData eurosnmpshirt = 275 new CompositeDataSupport (usst, 276 items, 277 new Object []{"SNMP", "green", "M", new Float (15.0f)}); 278 CompositeData eurojmxshirt = 279 new CompositeDataSupport (usst, 280 items, 281 new Object []{"JMX", "blue", "M", new Float (15.0f)}); 282 eurosdata.put(eurosnmpshirt); 283 eurosdata.put(eurojmxshirt); 284 285 assertTrue("Expecting US and Euro shirt data to be equal", 286 ussdata.equals(eurosdata)); 287 assertTrue("Expecting US and Euro shirt data hash codes to be equal", 288 eurosdata.hashCode() == ussdata.hashCode()); 289 int ushashcode = ussdata.getTabularType().hashCode(); 290 ushashcode += ussnmpshirt.hashCode(); 291 ushashcode += usjmxshirt.hashCode(); 292 assertTrue("Unexpected hash code computation", ussdata.hashCode() == ushashcode); 293 } 294 } 295 | Popular Tags |