1 22 package org.jboss.test.jmx.compliance.openmbean; 23 24 import junit.framework.TestCase; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import javax.management.openmbean.CompositeType ; 34 import javax.management.openmbean.OpenDataException ; 35 import javax.management.openmbean.OpenType ; 36 import javax.management.openmbean.SimpleType ; 37 import javax.management.openmbean.TabularData ; 38 import javax.management.openmbean.TabularDataSupport ; 39 import javax.management.openmbean.TabularType ; 40 41 46 public class TabularTypeTestCase 47 extends TestCase 48 { 49 51 53 55 58 public TabularTypeTestCase(String s) 59 { 60 super(s); 61 } 62 63 65 public void testTabularTypeOpenType() 66 throws Exception 67 { 68 String [] itemNames = new String [] { "name1", "name2" }; 69 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 70 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 71 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 72 itemNames, itemDescriptions, itemTypes); 73 74 String [] indexNames = new String [] { "name1", "name2" }; 75 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 76 77 assertEquals(TabularData .class.getName(), tabularType.getClassName()); 78 assertEquals("description", tabularType.getDescription()); 79 assertEquals("typeName", tabularType.getTypeName()); 80 assertTrue("Tabular type should not be an array", tabularType.isArray() == false); 81 } 82 83 public void testGetRowType() 84 throws Exception 85 { 86 String [] itemNames = new String [] { "name1", "name2" }; 87 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 88 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 89 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 90 itemNames, itemDescriptions, itemTypes); 91 92 String [] indexNames = new String [] { "name1", "name2" }; 93 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 94 95 assertEquals(rowType, tabularType.getRowType()); 96 } 97 98 public void testIndexNames() 99 throws Exception 100 { 101 String [] itemNames = new String [] { "name1", "name2" }; 102 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 103 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 104 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 105 itemNames, itemDescriptions, itemTypes); 106 107 String [] indexNames = new String [] { "name1", "name2" }; 108 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 109 110 List indexList = tabularType.getIndexNames(); 111 assertTrue("wrong number of index names", indexList.size() == 2); 112 assertTrue("index list should contain name1", indexList.contains("name1")); 113 assertTrue("index list should contain name2", indexList.contains("name2")); 114 Iterator i = indexList.iterator(); 115 assertTrue("first index is name1", i.next().equals("name1")); 116 assertTrue("second index is name2", i.next().equals("name2")); 117 } 118 119 public void testIsValue() 120 throws Exception 121 { 122 String [] itemNames = new String [] { "name1", "name2" }; 123 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 124 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 125 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 126 itemNames, itemDescriptions, itemTypes); 127 128 String [] indexNames = new String [] { "name1", "name2" }; 129 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 130 131 assertTrue("null is not a value of tabular type", tabularType.isValue(null) == false); 132 assertTrue("object is not a value of tabular type", tabularType.isValue(new Object ()) == false); 133 134 TabularDataSupport data = new TabularDataSupport (tabularType); 135 assertTrue("data should is a value", tabularType.isValue(data)); 136 137 TabularType tabularType2 = new TabularType ("typeName", "description", rowType, indexNames); 138 data = new TabularDataSupport (tabularType2); 139 assertTrue("data is a value, even though the tabular type is a different instance", 140 tabularType.isValue(data)); 141 142 tabularType2 = new TabularType ("typeName2", "description", rowType, indexNames); 143 data = new TabularDataSupport (tabularType2); 144 assertTrue("data should not be a value, they have different type names", 145 tabularType.isValue(data) == false); 146 147 CompositeType rowType2 = new CompositeType ("rowTypeName2", "rowDescription", 148 itemNames, itemDescriptions, itemTypes); 149 tabularType2 = new TabularType ("typeName", "description", rowType2, indexNames); 150 data = new TabularDataSupport (tabularType2); 151 assertTrue("data should not be a value, they have different row types", 152 tabularType.isValue(data) == false); 153 154 String [] indexNames2 = new String [] { "name2", "name1" }; 155 tabularType2 = new TabularType ("typeName", "description", rowType, indexNames2); 156 data = new TabularDataSupport (tabularType2); 157 assertTrue("data should not be a value, they have different index names", 158 tabularType.isValue(data) == false); 159 } 160 161 public void testEquals() 162 throws Exception 163 { 164 String [] itemNames = new String [] { "name1", "name2" }; 165 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 166 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 167 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 168 itemNames, itemDescriptions, itemTypes); 169 170 String [] indexNames = new String [] { "name1", "name2" }; 171 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 172 173 assertTrue("null is not equal to tabular type", tabularType.equals(null) == false); 174 assertTrue("object is not a equal to tabular type", tabularType.equals(new Object ()) == false); 175 176 TabularType tabularType2 = new TabularType ("typeName", "description", rowType, indexNames); 177 assertTrue("Should be equal, even though the tabular type is a different instance", 178 tabularType.equals(tabularType2)); 179 assertTrue("Should be equal, even though the tabular type is a different instance", 180 tabularType2.equals(tabularType)); 181 182 tabularType2 = new TabularType ("typeName2", "description", rowType, indexNames); 183 assertTrue("should not be equal, they have different type names", 184 tabularType.equals(tabularType2) == false); 185 assertTrue("should not be equal, they have different type names", 186 tabularType2.equals(tabularType) == false); 187 188 CompositeType rowType2 = new CompositeType ("rowTypeName2", "rowDescription", 189 itemNames, itemDescriptions, itemTypes); 190 tabularType2 = new TabularType ("typeName", "description", rowType2, indexNames); 191 assertTrue("should not be a equal, they have different row types", 192 tabularType.equals(tabularType2) == false); 193 assertTrue("should not be a equal, they have different row types", 194 tabularType2.equals(tabularType) == false); 195 196 String [] indexNames2 = new String [] { "name2", "name1" }; 197 tabularType2 = new TabularType ("typeName", "description", rowType, indexNames2); 198 assertTrue("should not be equal, they have different index names", 199 tabularType.equals(tabularType2) == false); 200 assertTrue("should not be equal, they have different index names", 201 tabularType2.equals(tabularType) == false); 202 } 203 204 public void testHashCode() 205 throws Exception 206 { 207 String [] itemNames = new String [] { "name1", "name2" }; 208 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 209 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 210 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 211 itemNames, itemDescriptions, itemTypes); 212 213 String [] indexNames = new String [] { "name1", "name2" }; 214 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 215 216 int myHashCode = "typeName".hashCode() + rowType.hashCode() 217 + "name1".hashCode() + "name2".hashCode(); 218 assertTrue("Wrong hash code generated", myHashCode == tabularType.hashCode()); 219 } 220 221 public void testToString() 222 throws Exception 223 { 224 String [] itemNames = new String [] { "name1", "name2" }; 225 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 226 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 227 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 228 itemNames, itemDescriptions, itemTypes); 229 230 String [] indexNames = new String [] { "name1", "name2" }; 231 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 232 233 String toString = tabularType.toString(); 234 235 assertTrue("toString() should contain the tabular type class name", 236 toString.indexOf(TabularType .class.getName()) != -1); 237 assertTrue("toString() should contain the type name", 238 toString.indexOf("typeName") != -1); 239 assertTrue("toString() should contain the row type " + rowType, 240 toString.indexOf(rowType.toString()) != -1); 241 assertTrue("toString() should contain the index name1", 242 toString.indexOf("name1") != -1); 243 assertTrue("toString() should contain the index name2", 244 toString.indexOf("name2") != -1); 245 } 246 247 public void testSerialization() 248 throws Exception 249 { 250 String [] itemNames = new String [] { "name1", "name2" }; 251 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 252 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 253 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 254 itemNames, itemDescriptions, itemTypes); 255 256 String [] indexNames = new String [] { "name1", "name2" }; 257 TabularType tabularType = new TabularType ("typeName", "description", rowType, indexNames); 258 259 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 261 ObjectOutputStream oos = new ObjectOutputStream (baos); 262 oos.writeObject(tabularType); 263 264 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 266 ObjectInputStream ois = new ObjectInputStream (bais); 267 Object result = ois.readObject(); 268 269 assertEquals(tabularType, result); 270 } 271 272 public void testErrors() 273 throws Exception 274 { 275 String [] itemNames = new String [] { "name1", "name2" }; 276 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 277 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 278 CompositeType rowType = new CompositeType ("rowTypeName", "rowDescription", 279 itemNames, itemDescriptions, itemTypes); 280 281 String [] indexNames = new String [] { "name1", "name2" }; 282 283 boolean caught = false; 284 try 285 { 286 new TabularType (null, "description", rowType, indexNames); 287 } 288 catch (IllegalArgumentException e) 289 { 290 caught = true; 291 } 292 if (caught == false) 293 fail("Expected IllegalArgumentException for null type name"); 294 295 caught = false; 296 try 297 { 298 new TabularType ("", "description", rowType, indexNames); 299 } 300 catch (IllegalArgumentException e) 301 { 302 caught = true; 303 } 304 if (caught == false) 305 fail("Expected IllegalArgumentException for empty type name"); 306 307 caught = false; 308 try 309 { 310 new TabularType ("typeName", null, rowType, indexNames); 311 } 312 catch (IllegalArgumentException e) 313 { 314 caught = true; 315 } 316 if (caught == false) 317 fail("Expected IllegalArgumentException for null description"); 318 319 caught = false; 320 try 321 { 322 new TabularType ("typeName", "", rowType, indexNames); 323 } 324 catch (IllegalArgumentException e) 325 { 326 caught = true; 327 } 328 if (caught == false) 329 fail("Expected IllegalArgumentException for empty description"); 330 331 caught = false; 332 try 333 { 334 new TabularType ("typeName", "description", null, indexNames); 335 } 336 catch (IllegalArgumentException e) 337 { 338 caught = true; 339 } 340 if (caught == false) 341 fail("Expected IllegalArgumentException for null row type"); 342 343 caught = false; 344 try 345 { 346 new TabularType ("typeName", "description", rowType, null); 347 } 348 catch (IllegalArgumentException e) 349 { 350 caught = true; 351 } 352 if (caught == false) 353 fail("Expected IllegalArgumentException for null index names"); 354 355 caught = false; 356 try 357 { 358 new TabularType ("typeName", "description", rowType, new String [0]); 359 } 360 catch (IllegalArgumentException e) 361 { 362 caught = true; 363 } 364 if (caught == false) 365 fail("Expected IllegalArgumentException for empty index names"); 366 367 caught = false; 368 try 369 { 370 new TabularType ("typeName", "description", rowType, new String [] { "name1", null }); 371 } 372 catch (IllegalArgumentException e) 373 { 374 caught = true; 375 } 376 if (caught == false) 377 fail("Expected IllegalArgumentException for null index name element"); 378 379 caught = false; 380 try 381 { 382 new TabularType ("typeName", "description", rowType, new String [] { "name1", "" }); 383 } 384 catch (IllegalArgumentException e) 385 { 386 caught = true; 387 } 388 if (caught == false) 389 fail("Expected IllegalArgumentException for empty index name element"); 390 391 caught = false; 392 try 393 { 394 new TabularType ("typeName", "description", rowType, new String [] { "name1", "nameX" }); 395 } 396 catch (OpenDataException e) 397 { 398 caught = true; 399 } 400 if (caught == false) 401 fail("Expected OpenDataException for invalid index name"); 402 } 403 404 } 406 | Popular Tags |