1 8 package test.javax.management.openmbean; 9 10 import java.io.IOException ; 11 import javax.management.openmbean.CompositeType ; 12 import javax.management.openmbean.OpenDataException ; 13 import javax.management.openmbean.OpenType ; 14 import javax.management.openmbean.SimpleType ; 15 16 import junit.framework.TestCase; 17 import test.javax.management.compliance.serialization.support.Serializer; 18 19 22 public class CompositeTypeTest extends TestCase 23 { 24 private String [] itemNames = null; 25 private String [] itemDescriptions = null; 26 private OpenType [] itemTypes; 27 private String [] indexNames; 28 private CompositeType tShirtType; 29 30 public CompositeTypeTest(String s) 31 { 32 super(s); 33 } 34 35 protected void setUp() throws Exception 36 { 37 super.setUp(); 38 itemNames = new String []{"model", "color", "size", "price"}; 39 itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 40 itemTypes = new OpenType []{SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.FLOAT}; 41 indexNames = new String []{"model", "color", "size"}; 42 tShirtType = new CompositeType ("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes); 43 } 44 45 protected void tearDown() throws Exception 46 { 47 super.tearDown(); 48 } 49 50 public void testCreation() 51 { 52 assertTrue(tShirtType != null); 53 } 54 55 public void testInvalidCreation() 56 { 57 try 58 { 59 itemNames = new String []{"model", "color", "size", "model"}; 61 tShirtType = new CompositeType ("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes); 62 fail("Expect exception, invalid itemDescriptions (not the same size as itemNames - we should not see this"); 63 } 64 catch (OpenDataException e) 65 { 66 } 67 68 try 69 { 70 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 72 tShirtType = new CompositeType ("", "a TShirt", itemNames, itemDescriptions, itemTypes); 73 fail("No exception thrown"); 74 } 75 catch (IllegalArgumentException x) 76 { 77 assertTrue(true); } 79 catch (Exception x) 80 { 81 fail("Expecting IllegalArgumentException"); 82 } 83 84 try 85 { 86 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 88 tShirtType = new CompositeType (null, "a TShirt", itemNames, itemDescriptions, itemTypes); 89 fail("No exception thrown"); 90 } 91 catch (IllegalArgumentException x) 92 { 93 assertTrue(true); } 95 catch (Exception x) 96 { 97 fail("Expecting IllegalArgumentException"); 98 } 99 100 try 101 { 102 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 104 tShirtType = new CompositeType ("tShirt", "", itemNames, itemDescriptions, itemTypes); 105 fail("No exception thrown"); 106 } 107 catch (IllegalArgumentException x) 108 { 109 assertTrue(true); } 111 catch (Exception x) 112 { 113 fail("Expecting IllegalArgumentException"); 114 } 115 116 try 117 { 118 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 120 tShirtType = new CompositeType ("tShirt", null, itemNames, itemDescriptions, itemTypes); 121 fail("No exception thrown"); 122 } 123 catch (IllegalArgumentException x) 124 { 125 assertTrue(true); } 127 catch (Exception x) 128 { 129 fail("Expecting IllegalArgumentException"); 130 } 131 132 try 133 { 134 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 136 itemNames = new String []{"model", null, "size", "price"}; 137 tShirtType = new CompositeType ("tShirt", "", itemNames, itemDescriptions, itemTypes); 138 fail("No exception thrown"); 139 } 140 catch (IllegalArgumentException x) 141 { 142 assertTrue(true); } 144 catch (Exception x) 145 { 146 fail("Expecting IllegalArgumentException"); 147 } 148 149 try 150 { 151 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's size", "TShirt's price"}; 153 itemNames = new String []{"model", "color", "", "price"}; 154 tShirtType = new CompositeType ("tShirt", "", itemNames, itemDescriptions, itemTypes); 155 fail("No exception thrown"); 156 } 157 catch (IllegalArgumentException x) 158 { 159 assertTrue(true); } 161 catch (Exception x) 162 { 163 fail("Expecting IllegalArgumentException"); 164 } 165 166 try 167 { 168 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", null, "TShirt's price"}; 170 tShirtType = new CompositeType ("tShirt", "", itemNames, itemDescriptions, itemTypes); 171 fail("No exception thrown"); 172 } 173 catch (IllegalArgumentException x) 174 { 175 assertTrue(true); } 177 catch (Exception x) 178 { 179 fail("Expecting IllegalArgumentException"); 180 } 181 182 try 183 { 184 String [] itemDescriptions = new String []{"TShirt's model name", "", "TShirt's size", "TShirt's price"}; 186 tShirtType = new CompositeType ("tShirt", "", itemNames, itemDescriptions, itemTypes); 187 fail("No exception thrown"); 188 } 189 catch (IllegalArgumentException x) 190 { 191 assertTrue(true); } 193 catch (Exception x) 194 { 195 fail("Expecting IllegalArgumentException"); 196 } 197 198 try 199 { 200 tShirtType = new CompositeType ("tShirt", "a TShirt", itemNames, itemDescriptions, null); 202 fail("No exception thrown"); 203 } 204 catch (IllegalArgumentException x) 205 { 206 assertTrue(true); } 208 catch (Exception x) 209 { 210 fail("Expecting IllegalArgumentException"); 211 } 212 213 try 214 { 215 String [] itemDescriptions = new String []{"TShirt's model name", "TShirt's color", "TShirt's price"}; 217 itemNames = new String []{"model", "color", "size", "price"}; 218 tShirtType = new CompositeType ("tShirt", "a TShirt", itemNames, itemDescriptions, itemTypes); 219 fail("No exception thrown"); 220 } 221 catch (IllegalArgumentException x) 222 { 223 assertTrue(true); } 225 catch (Exception x) 226 { 227 fail("Expecting IllegalArgumentException"); 228 } 229 230 } 231 232 public void testContainsKey() 233 { 234 boolean valid = tShirtType.containsKey("model"); 235 assertTrue(valid); 236 237 assertTrue(tShirtType.containsKey("Model") == false); 239 } 240 241 public void testGetDescription() 242 { 243 String expected = "TShirt's color"; 244 String result = tShirtType.getDescription("color"); 245 assertTrue(expected == result); 246 } 247 248 public void testGetType() 249 { 250 OpenType expected = SimpleType.FLOAT; 251 OpenType result = tShirtType.getType("price"); 252 assertEquals(expected, result); 253 } 254 255 public void testSerialization() 256 { 257 259 try 260 { 261 Serializer serializer = new Serializer(); 262 byte[] data = serializer.serialize(tShirtType); 263 Object obj = serializer.deserialize(data); 264 assertTrue(obj instanceof CompositeType ); 266 267 CompositeType type = (CompositeType )obj; 269 assertEquals(type.hashCode(), tShirtType.hashCode()); 271 assertTrue(type.getType("price").equals(SimpleType.FLOAT)); 272 assertEquals(type.getType("size"), tShirtType.getType("size")); 273 assertEquals(type.getDescription("model"), tShirtType.getDescription("model")); 274 assertEquals(type.keySet(), tShirtType.keySet()); 275 } 276 catch (IOException e) 277 { 278 e.printStackTrace(); 279 } 280 catch (ClassNotFoundException e) 281 { 282 } 283 } 284 285 public void testHashCode() throws Exception 286 { 287 int ehc = 0; 288 ehc += tShirtType.getTypeName().hashCode(); 289 for (int i = 0; i < itemNames.length; i++) 290 { 291 ehc += itemNames[i].hashCode(); 292 ehc += itemTypes[i].hashCode(); 293 } 294 int hc = tShirtType.hashCode(); 295 assertTrue("Unexpected hashcode", hc == ehc); 296 } 297 298 public void testEquals() throws Exception 299 { 300 CompositeType undershirt = 301 new CompositeType ("UnderShirt", 302 "a TShirt", 303 itemNames, 304 itemDescriptions, 305 itemTypes); 306 assertFalse("tShirtType and undershirt should not be equal", 307 undershirt.equals(tShirtType)); 308 String [] italianDescriptions = 309 {"modello", "nome", "colore", "prezzo"}; 310 CompositeType italianshirt = 311 new CompositeType ("UnderShirt", 312 "una Camicia", 313 itemNames, 314 italianDescriptions, 315 itemTypes); 316 assertTrue("undershirt and italianshirt should be equal", 317 italianshirt.equals(undershirt)); 318 } 319 } 320 | Popular Tags |