1 30 package org.objectweb.asm; 31 32 import java.io.InputStream ; 33 import java.lang.annotation.Annotation ; 34 35 import org.objectweb.asm.ClassReader; 36 import org.objectweb.asm.tree.ClassNode; 37 38 import annotations.ValueAttrAnnotation; 39 import annotations.Values; 40 import annotations.ValuesAnnotation; 41 import annotations.ValuesDump; 42 import annotations.ValuesEnum; 43 44 import junit.framework.TestCase; 45 46 public class AnnotationTest extends TestCase { 47 48 private ValuesAnnotation a; 49 50 public AnnotationTest(String name) { 51 super(name); 52 } 53 54 protected void setUp() throws Exception { 55 TestClassLoader cl = new TestClassLoader("annotations.Values", 56 getClass().getClassLoader()); 57 Class c = cl.loadClass("annotations.Values"); 58 Annotation [] annotations = c.getAnnotations(); 59 for (int i = 0; i < annotations.length; ++i) { 60 if (annotations[i] instanceof ValuesAnnotation) { 61 a = (ValuesAnnotation) annotations[i]; 62 } 63 } 64 if (a == null) { 65 fail(); 66 } 67 } 68 69 public void testByteValue() { 70 assertEquals(1, a.byteValue()); 71 } 72 73 public void testCharValue() { 74 assertEquals('A', a.charValue()); 75 } 76 77 public void testBooleanValue() { 78 assertEquals(true, a.booleanValue()); 79 } 80 81 public void testIntValue() { 82 assertEquals(1, a.intValue()); 83 } 84 85 public void testShortValue() { 86 assertEquals(1, a.shortValue()); 87 } 88 89 public void testLongValue() { 90 assertEquals(1L, a.longValue()); 91 } 92 93 public void testFloatValue() { 94 assertEquals(1.0f, a.floatValue(), 0.1f); 95 } 96 97 public void testDoubleValue() { 98 assertEquals(1.0d, a.doubleValue(), 0.1d); 99 } 100 101 public void testStringValue() { 102 assertEquals("A", a.stringValue()); 103 } 104 105 public void testAnnotationValue() { 106 ValueAttrAnnotation ann = a.annotationValue(); 107 assertEquals("annotation", ann.value()); 108 } 109 110 public void testEnumValue() { 111 ValuesEnum en = a.enumValue(); 112 assertEquals(ValuesEnum.ONE, en); 113 } 114 115 public void testClassValue() { 116 Class c = a.classValue(); 117 assertEquals(Values.class.getName(), c.getName()); 118 } 119 120 public void testByteArrayValue() { 121 byte[] bs = a.byteArrayValue(); 122 assertEquals(1, bs[0]); 123 assertEquals(-1, bs[1]); 124 } 125 126 public void testCharArrayValue() { 127 char[] bs = a.charArrayValue(); 128 assertEquals('c', bs[0]); 129 assertEquals('b', bs[1]); 130 assertEquals((char) -1, bs[2]); 131 } 132 133 public void testBooleanArrayValue() { 134 boolean[] bs = a.booleanArrayValue(); 135 assertEquals(true, bs[0]); 136 assertEquals(false, bs[1]); 137 } 138 139 public void testIntArrayValue() { 140 int[] bs = a.intArrayValue(); 141 assertEquals(1, bs[0]); 142 assertEquals(-1, bs[1]); 143 } 144 145 public void testShortArrayValue() { 146 short[] bs = a.shortArrayValue(); 147 assertEquals(1, bs[0]); 148 assertEquals(-1, bs[1]); 149 } 150 151 public void testLongArrayValue() { 152 long[] bs = a.longArrayValue(); 153 assertEquals(1L, bs[0]); 154 assertEquals(-1L, bs[1]); 155 } 156 157 public void testFloatArrayValue() { 158 float[] bs = a.floatArrayValue(); 159 assertEquals(1.0f, bs[0], 0.1f); 160 assertEquals(-1.0f, bs[1], 0.1f); 161 } 162 163 public void testDoubleArrayValue() { 164 double[] bs = a.doubleArrayValue(); 165 assertEquals(1.0d, bs[0], 0.1d); 166 assertEquals(-1.0d, bs[1], 0.1d); 167 } 168 169 public void testStringArrayValue() { 170 String [] s = a.stringArrayValue(); 171 assertEquals("aa", s[0]); 172 assertEquals("bb", s[1]); 173 } 174 175 public void testAnnotationArrayValue() { 176 ValueAttrAnnotation[] ann = a.annotationArrayValue(); 177 assertEquals("annotation1", ann[0].value()); 178 assertEquals("annotation2", ann[1].value()); 179 } 180 181 public void testEnumArrayValue() { 182 ValuesEnum[] en = a.enumArrayValue(); 183 assertEquals(ValuesEnum.ONE, en[0]); 184 assertEquals(ValuesEnum.TWO, en[1]); 185 } 186 187 public void testClassArrayValue() { 188 Class [] c = a.classArrayValue(); 189 assertEquals(Values.class.getName(), c[0].getName()); 190 assertEquals(Values.class.getName(), c[1].getName()); 191 } 192 193 public void testMethodNode() throws Exception { 195 InputStream is = getClass().getResourceAsStream("/" 196 + annotations.Values.class.getName().replace('.', '/') 197 + ".class"); 198 ClassReader cr = new ClassReader(is); 199 ClassNode cn = new ClassNode(); 200 cr.accept(cn, false); 201 } 202 203 private static final class TestClassLoader extends ClassLoader { 204 205 private final String className; 206 207 private final ClassLoader loader; 208 209 public TestClassLoader(String className, ClassLoader loader) { 210 super(); 211 this.className = className; 212 this.loader = loader; 213 } 214 215 public Class loadClass(String name) throws ClassNotFoundException { 216 if (className.equals(name)) { 217 try { 218 byte[] bytecode = ValuesDump.dump(); 219 return super.defineClass(className, 220 bytecode, 221 0, 222 bytecode.length); 223 } catch (Exception ex) { 224 ex.printStackTrace(); 225 throw new ClassNotFoundException ("Load error: " 226 + ex.toString(), ex); 227 } 228 } 229 230 return loader.loadClass(name); 231 } 232 } 233 } 234 | Popular Tags |