1 32 package net.sf.retrotranslator.runtime.java.lang.reflect; 33 34 import java.lang.annotation.Annotation ; 35 import java.lang.reflect.Constructor ; 36 import net.sf.retrotranslator.runtime.java.lang.*; 37 import net.sf.retrotranslator.tests.BaseTestCase; 38 39 42 public class _ConstructorTestCase extends BaseTestCase { 43 44 @MyStyle("bold") 45 public _ConstructorTestCase() { 46 } 47 48 @MyStyle("italic") 49 public _ConstructorTestCase(String string) { 50 super(string); 51 } 52 53 public _ConstructorTestCase(String string, @MyStyle("glass") int i) throws RuntimeException { 54 super(string + i); 55 } 56 57 public _ConstructorTestCase(String string, int... numbers) { 58 super(string + numbers.length); 59 } 60 61 public void testGetAnnotationForNoParam() throws Exception { 62 Constructor <_ConstructorTestCase> noParamConstructor = _ConstructorTestCase.class.getConstructor(); 63 assertEquals("bold", noParamConstructor.getAnnotation(MyStyle.class).value()); 64 } 65 66 public void testGetAnnotationForOneParam() throws Exception { 67 Constructor <_ConstructorTestCase> oneParamConstructor = _ConstructorTestCase.class.getConstructor(String .class); 68 assertEquals("italic", oneParamConstructor.getAnnotation(MyStyle.class).value()); 69 } 70 71 public void testGetAnnotationForTwoParam() throws Exception { 72 Constructor <_ConstructorTestCase> twoParamConstructor = _ConstructorTestCase.class.getConstructor(String .class, int.class); 73 assertNull(twoParamConstructor.getAnnotation(MyStyle.class)); 74 } 75 76 public void testGetDeclaredAnnotations() throws Exception { 77 Constructor <_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(); 78 assertEqualElements(constructor.getAnnotations(), (Object []) constructor.getDeclaredAnnotations()); 79 } 80 81 public void testGetGenericExceptionTypes() throws Exception { 82 Constructor <_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(String .class, int.class); 83 assertEquals(RuntimeException .class, singleton(constructor.getGenericExceptionTypes())); 84 } 85 86 public void testGetGenericParameterTypes() throws Exception { 87 Constructor <_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(String .class); 88 assertEquals(String .class, constructor.getGenericParameterTypes()[0]); 89 } 90 91 public void testGetParameterAnnotationsForTwoParam() throws Exception { 92 Constructor <_ConstructorTestCase> twoParamConstructor = _ConstructorTestCase.class.getConstructor(String .class, int.class); 93 Annotation [][] annotations = twoParamConstructor.getParameterAnnotations(); 94 assertEquals(2, annotations.length); 95 assertEquals(0, annotations[0].length); 96 assertEquals(1, annotations[1].length); 97 assertEquals("glass", ((MyStyle) annotations[1][0]).value()); 98 } 99 100 public void testGetTypeParameters() throws Exception { 101 Constructor <_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(String .class, int.class); 102 assertEquals(0, constructor.getTypeParameters().length); 103 } 104 105 public void testIsAnnotationPresent() throws Exception { 106 Constructor <_ConstructorTestCase> constructor = _ConstructorTestCase.class.getConstructor(); 107 assertTrue(constructor.isAnnotationPresent(MyStyle.class)); 108 assertFalse(constructor.isAnnotationPresent(MyFormatter.class)); 109 } 110 111 private static class Test { 112 private Test() { 113 } 114 } 115 116 public void testIsSynthetic() throws Exception { 117 new Test(); 118 Constructor [] constructors = Test.class.getDeclaredConstructors(); 119 assertEquals(2, constructors.length); 120 Constructor syntheticConstructor = null; 121 for (Constructor constructor : constructors) { 122 if (constructor.isSynthetic()) syntheticConstructor = constructor; 123 } 124 assertNotNull(syntheticConstructor); 125 Class parameterType = syntheticConstructor.getParameterTypes()[0]; 126 assertTrue(parameterType.isSynthetic()); 127 assertFalse(Test.class.isSynthetic()); 128 assertFalse(_ConstructorTestCase.class.isSynthetic()); 129 } 130 131 public void testIsVarArgs() throws Exception { 132 assertTrue(_ConstructorTestCase.class.getConstructor(String .class, int[].class).isVarArgs()); 133 assertFalse(_ConstructorTestCase.class.getConstructor(String .class, int.class).isVarArgs()); 134 } 135 136 public void testGetGenericString() throws Exception { 137 class Test<T extends String , RE extends RuntimeException > { 138 public <E extends Number > Test(T t, E e, String [] strings) throws RE, ClassNotFoundException { 139 } 140 } 141 142 assertEquals("public <E> " + 143 this.getClass().getName() + 144 "$1Test(T,E,java.lang.String[])" + 145 " throws RE,java.lang.ClassNotFoundException", 146 Test.class.getConstructors()[0].toGenericString()); 147 } 148 } | Popular Tags |