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 31 import javax.management.openmbean.ArrayType ; 32 import javax.management.openmbean.CompositeDataSupport ; 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.TabularType ; 38 import javax.management.openmbean.TabularDataSupport ; 39 40 45 public class ArrayTypeTestCase 46 extends TestCase 47 { 48 50 52 54 57 public ArrayTypeTestCase(String s) 58 { 59 super(s); 60 } 61 62 64 public void testArrayTypeOpenType() 65 throws Exception 66 { 67 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 68 assertEquals("[[[Ljava.lang.String;", arrayType.getClassName()); 69 assertEquals("3-dimension array of java.lang.String", arrayType.getDescription()); 70 assertEquals("[[[Ljava.lang.String;", arrayType.getTypeName()); 71 assertTrue("Composite type should be an array", arrayType.isArray()); 72 } 73 74 public void testGetDimension() 75 throws Exception 76 { 77 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 78 assertTrue("Dimension should be 3", arrayType.getDimension() == 3); 79 } 80 81 public void testElementOpenType() 82 throws Exception 83 { 84 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 85 assertTrue("Element OpenType should be " + SimpleType.STRING, arrayType.getElementOpenType().equals(SimpleType.STRING)); 86 } 87 88 public void testIsValue() 89 throws Exception 90 { 91 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 92 93 assertTrue("null is not a value of array type", arrayType.isValue(null) == false); 94 assertTrue("object is not a value of array type", arrayType.isValue(new Object ()) == false); 95 96 String [][][] data = new String [1][2][3]; 97 assertTrue("data should be a value of array type", arrayType.isValue(data)); 98 99 String [][] data2 = new String [1][2]; 100 assertTrue("data should not be a value of array type, wrong number of dimensions", arrayType.isValue(data2) == false); 101 102 Object [][][] data3 = new Object [1][2][3]; 103 assertTrue("data should not be a value of array type, wrong element type", arrayType.isValue(data3) == false); 104 105 String [] itemNames = new String [] { "name1", "name2" }; 106 String [] itemDescriptions = new String [] { "desc1", "desc2" }; 107 OpenType [] itemTypes = new OpenType [] { SimpleType.STRING, SimpleType.INTEGER }; 108 CompositeType compositeType = new CompositeType ("typeName", "description", 109 itemNames, itemDescriptions, itemTypes); 110 Object [] itemValues = new Object [] { "string", new Integer (1) }; 111 CompositeDataSupport cds = new CompositeDataSupport (compositeType, itemNames, itemValues); 112 CompositeDataSupport [][] compData1 = new CompositeDataSupport [][] 113 { 114 { cds, null }, { cds, cds } 115 }; 116 117 ArrayType compArrayType1 = new ArrayType (2, compositeType); 118 assertTrue("compData1 should be a value of array type", compArrayType1.isValue(compData1)); 119 120 ArrayType compArrayType2 = new ArrayType (1, compositeType); 121 assertTrue("compData1 should not be a value of array type, wrong dimension", compArrayType2.isValue(compData1) == false); 122 123 CompositeType compositeType2 = new CompositeType ("typeName2", "description", 124 itemNames, itemDescriptions, itemTypes); 125 ArrayType compArrayType3 = new ArrayType (2, compositeType2); 126 assertTrue("compData1 should not be a value of array type, wrong element type", compArrayType3.isValue(compData1) == false); 127 128 TabularType tabularType = new TabularType ("typeName", "description", compositeType, new String [] { "name1" }); 129 TabularDataSupport tds = new TabularDataSupport (tabularType); 130 TabularDataSupport [][] tabData1 = new TabularDataSupport [][] 131 { 132 { tds, null }, { tds, tds } 133 }; 134 135 ArrayType tabArrayType1 = new ArrayType (2, tabularType); 136 assertTrue("tabData1 should be a value of array type", tabArrayType1.isValue(tabData1)); 137 138 ArrayType tabArrayType2 = new ArrayType (1, tabularType); 139 assertTrue("tabData1 should not be a value of array type, wrong number of dimensions", tabArrayType2.isValue(tabData1) == false); 140 141 TabularType tabularType2 = new TabularType ("typeName2", "description", compositeType, new String [] { "name1" }); 142 ArrayType tabArrayType3 = new ArrayType (2, tabularType2); 143 assertTrue("tabData1 should not be a value of array type, wrong element type", tabArrayType3.isValue(tabData1) == false); 144 } 145 146 public void testEquals() 147 throws Exception 148 { 149 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 150 assertTrue("null is not an array type", arrayType.equals(null) == false); 151 assertTrue("object is not an array type", arrayType.equals(new Object ()) == false); 152 153 assertTrue("should be equal", arrayType.equals(arrayType)); 154 155 ArrayType arrayType2 = new ArrayType (3, SimpleType.STRING); 156 assertTrue("should be equal, even though different instances", arrayType.equals(arrayType2)); 157 assertTrue("should be equal, even though different instances", arrayType2.equals(arrayType)); 158 159 arrayType2 = new ArrayType (2, SimpleType.STRING); 160 assertTrue("should not be equal, wrong number of dimensions", arrayType.equals(arrayType2) == false); 161 assertTrue("should not be equal, wrong number of dimensions", arrayType2.equals(arrayType) == false); 162 163 arrayType2 = new ArrayType (3, SimpleType.INTEGER); 164 assertTrue("should not be equal, wrong element type", arrayType.equals(arrayType2) == false); 165 assertTrue("should not be equal, wrong element type", arrayType2.equals(arrayType) == false); 166 } 167 168 public void testHashCode() 169 throws Exception 170 { 171 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 172 173 int myHashCode = 3 + SimpleType.STRING.hashCode(); 174 assertTrue("Wrong hash code generated", myHashCode == arrayType.hashCode()); 175 } 176 177 public void testToString() 178 throws Exception 179 { 180 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 181 182 String toString = arrayType.toString(); 183 184 assertTrue("toString() should contain the array type class name", 185 toString.indexOf(ArrayType .class.getName()) != -1); 186 assertTrue("toString() should contain the dimension", 187 toString.indexOf("3") != -1); 188 assertTrue("toString() should contain the element type", 189 toString.indexOf(SimpleType.STRING.toString()) != -1); 190 } 191 192 public void testSerialization() 193 throws Exception 194 { 195 ArrayType arrayType = new ArrayType (3, SimpleType.STRING); 196 197 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 199 ObjectOutputStream oos = new ObjectOutputStream (baos); 200 oos.writeObject(arrayType); 201 202 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 204 ObjectInputStream ois = new ObjectInputStream (bais); 205 Object result = ois.readObject(); 206 207 assertEquals(arrayType, result); 208 } 209 210 public void testErrors() 211 throws Exception 212 { 213 boolean caught = false; 214 try 215 { 216 new ArrayType (-1, SimpleType.STRING); 217 } 218 catch (IllegalArgumentException e) 219 { 220 caught = true; 221 } 222 if (caught == false) 223 fail("Excepted IllegalArgumentException for negative dimension"); 224 225 caught = false; 226 try 227 { 228 new ArrayType (1, new ArrayType (2, SimpleType.STRING)); 229 } 230 catch (OpenDataException e) 231 { 232 caught = true; 233 } 234 if (caught == false) 235 fail("Excepted OpenDataException for ArrayType element type"); 236 } 237 238 public void testErrors2() 239 throws Exception 240 { 241 boolean caught = false; 242 try 243 { 244 new ArrayType (1, null); 245 } 246 catch (NullPointerException e) 247 { 248 fail("FAILS IN RI: expected IllegalArgumentException for null element type"); 249 } 250 catch (IllegalArgumentException e) 251 { 252 caught = true; 253 } 254 if (caught == false) 255 fail("Excepted IllegalArgumentException for null element type"); 256 } 257 } 258 | Popular Tags |