1 22 package org.jboss.test.jmx.compliance.openmbean; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.math.BigDecimal ; 29 import java.math.BigInteger ; 30 import java.util.Date ; 31 32 import javax.management.ObjectName ; 33 import javax.management.openmbean.SimpleType ; 34 35 import junit.framework.TestCase; 36 37 42 public class SimpleTypeTestCase 43 extends TestCase 44 { 45 47 static ObjectName objectName; 48 49 static 50 { 51 try 52 { 53 objectName = new ObjectName ("test:test=test"); 54 } 55 catch (Exception e) 56 { 57 throw new RuntimeException (e.toString()); 58 } 59 } 60 61 63 SimpleType [] types = new SimpleType [] 64 { 65 SimpleType.BIGDECIMAL, 66 SimpleType.BIGINTEGER, 67 SimpleType.BOOLEAN, 68 SimpleType.BYTE, 69 SimpleType.CHARACTER, 70 SimpleType.DATE, 71 SimpleType.DOUBLE, 72 SimpleType.FLOAT, 73 SimpleType.INTEGER, 74 SimpleType.LONG, 75 SimpleType.OBJECTNAME, 76 SimpleType.SHORT, 77 SimpleType.STRING, 78 SimpleType.VOID 79 }; 80 81 Class [] classes = new Class [] 82 { 83 BigDecimal .class, 84 BigInteger .class, 85 Boolean .class, 86 Byte .class, 87 Character .class, 88 Date .class, 89 Double .class, 90 Float .class, 91 Integer .class, 92 Long .class, 93 ObjectName .class, 94 Short .class, 95 String .class, 96 Void .class 97 }; 98 99 Object [] objects = new Object [] 100 { 101 new BigDecimal (1), 102 BigInteger.ONE, 103 new Boolean (false), 104 new Byte (Byte.MAX_VALUE), 105 new Character ('a'), 106 new Date (System.currentTimeMillis()), 107 new Double (1), 108 new Float (1), 109 new Integer (1), 110 new Long (1), 111 objectName, 112 new Short (Short.MAX_VALUE), 113 new String ("hello"), 114 null 115 }; 116 117 119 122 public SimpleTypeTestCase(String s) 123 { 124 super(s); 125 } 126 127 129 public void testSimpleTypes() 130 throws Exception 131 { 132 for (int i = 0; i < types.length; i++) 133 { 134 String className = classes[i].getName(); 135 assertEquals(className, types[i].getClassName()); 136 assertEquals(className, types[i].getTypeName()); 137 assertEquals(className, types[i].getDescription()); 138 } 139 } 140 141 public void testEquals() 142 throws Exception 143 { 144 for (int i = 0; i < types.length; i++) 145 for (int j = 0; j < types.length; j++) 146 { 147 if (i == j) 148 assertEquals(types[i], types[j]); 149 else 150 assertTrue("Simple Types should be different " + classes[i], 151 types[i] != types[j]); 152 } 153 } 154 155 public void testIsValue() 156 throws Exception 157 { 158 for (int i = 0; i < types.length; i++) 159 { 160 for (int j = 0; j < types.length; j++) 161 { 162 if (objects[i] == null) 164 continue; 165 166 if (i == j) 167 assertTrue(classes[i] + " should be a simple value of " + types[j], 168 types[j].isValue(objects[i])); 169 else 170 assertTrue(classes[i] + " should NOT be a simple value of " + types[j], 171 types[j].isValue(objects[i]) == false); 172 } 173 174 assertTrue("null should NOT be a simple value of " + types[i], types[i].isValue(null) == false); 175 } 176 } 177 178 public void testHashCode() 179 throws Exception 180 { 181 for (int i = 0; i < types.length; i++) 182 assertEquals(classes[i].getName().hashCode(), types[i].hashCode()); 183 } 184 185 public void testToString() 186 throws Exception 187 { 188 for (int i = 0; i < types.length; i++) 189 { 190 assertTrue("Simple Type " + classes[i].getName() + 191 " should contain " + SimpleType .class.getName(), 192 types[i].toString().indexOf(SimpleType .class.getName()) != -1); 193 assertTrue("Simple Type " + classes[i].getName() + 194 " should contain " + classes[i].getName(), 195 types[i].toString().indexOf(classes[i].getName()) != -1); 196 } 197 } 198 199 public void testSerialization() 200 throws Exception 201 { 202 for (int i = 0; i < types.length; i++) 203 { 204 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 206 ObjectOutputStream oos = new ObjectOutputStream (baos); 207 oos.writeObject(types[i]); 208 209 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 211 ObjectInputStream ois = new ObjectInputStream (bais); 212 SimpleType result = (SimpleType ) ois.readObject(); 213 214 assertTrue("Should resolve to same object after serialization " + types[i], types[i] == result); 215 } 216 } 217 } 218 | Popular Tags |