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