001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang.reflect;
019:
020: import org.apache.harmony.lang.AnnotatedElementTestFrame.MissingClassValAntn;
021: import org.apache.harmony.lang.AnnotatedElementTestFrame.MissingTypeAntn;
022: import org.apache.harmony.lang.AnnotatedElementTestFrame.TagAntn;
023: import org.apache.harmony.lang.AnnotatedElementTestFrame.ValAntn;
024:
025: /**
026: * @author Alexey V. Varlamov
027: * @version $Revision$
028: */
029: public class Ctor5Test extends Method5Test {
030:
031: public static void main(String[] args) {
032: junit.textui.TestRunner.run(Ctor5Test.class);
033: }
034:
035: protected @Override
036: AnnotatedElement getElement1() throws Throwable {
037: return AnnotatedCtor.class.getConstructor();
038: }
039:
040: protected @Override
041: AnnotatedElement getElement2() throws Throwable {
042: return AnnotatedCtor.class.getConstructor(Object.class);
043: }
044:
045: protected @Override
046: AnnotatedElement getElement3() throws Throwable {
047: return AnnotatedCtor.class.getConstructor(String.class);
048: }
049:
050: /**
051: * Provides an instance to be tested. The instance must be annotated
052: * by the notfound.MissingAntn.
053: */
054: protected @Override
055: AnnotatedElement getElement4() throws Throwable {
056: return AnnotatedCtor.class.getConstructor(int.class);
057: }
058:
059: /**
060: * Provides an instance to be tested. The instance must be annotated
061: * by the MissingClassValAntn.
062: */
063: protected @Override
064: AnnotatedElement getElement5() throws Throwable {
065: return AnnotatedCtor.class.getConstructor(long.class);
066: }
067:
068: /**
069: * Provides an instance to be tested. The instance must be annotated
070: * by the MissingTypeAntn.
071: */
072: protected @Override
073: AnnotatedElement getElement6() throws Throwable {
074: return AnnotatedCtor.class.getConstructor(char.class);
075: }
076:
077: @Override
078: protected Member getParamElement1() throws Throwable {
079: return AnnotatedParamCtor.class.getConstructor();
080: }
081:
082: @Override
083: protected Member getParamElement2() throws Throwable {
084: return AnnotatedParamCtor.class.getConstructor(Object.class,
085: Class.class, boolean.class);
086: }
087:
088: @Override
089: protected Member getParamElement3() throws Throwable {
090: return AnnotatedParamCtor.class.getConstructor(String.class,
091: int.class);
092: }
093:
094: static class A {
095: private Object obj;
096:
097: class InA {
098: Object o = obj;
099: }
100: }
101:
102: enum E {
103: E1, E2, E3
104: }
105:
106: static class B {
107: public B() {
108: }
109:
110: public B(Object o) {
111: }
112:
113: public B(Object o1, int... i) {
114: }
115:
116: public B(Object[] o) {
117: }
118: }
119:
120: /**
121: * isSynthetic() should return true if and only if
122: * the target c-tor does not appear in the source code.
123: */
124: public void testIsSynthetic() throws Exception {
125: assertFalse("case1.1: ordinary ctor", AnnotatedCtor.class
126: .getConstructor().isSynthetic());
127: assertFalse("case1.2: implicit default c-tor", A.class
128: .getDeclaredConstructor().isSynthetic());
129:
130: Constructor[] cs = B.class.getConstructors();
131: for (Constructor<?> c : cs) {
132: assertFalse("case2: " + c, c.isSynthetic());
133: }
134: // XXX how to force synthetic c-tor ?
135: }
136:
137: /**
138: * isVarArgs() should return true if and only if
139: * the target method is declared with varargs.
140: */
141: public void testIsVarargs() throws Exception {
142: assertFalse("case1: ordinary c-tor", AnnotatedCtor.class
143: .getConstructor().isVarArgs());
144:
145: assertTrue("case2: varargs c-tor", B.class.getConstructor(
146: Object.class, int[].class).isVarArgs());
147:
148: assertFalse("case3: ordinary c-tor", B.class.getConstructor(
149: Object[].class).isVarArgs());
150: }
151:
152: static abstract strictfp class C {
153: protected <T extends Throwable & Comparable<Throwable>> C(
154: T num, OneParamType<? super T> l) throws T, Error {
155: }
156: }
157:
158: /**
159: * toGenericString() should return a string exactly matching
160: * the API specification.
161: */
162: public void testToGenericString() throws Exception {
163: String s = C.class.getDeclaredConstructor(Throwable.class,
164: OneParamType.class).toGenericString();
165: System.out.println(s);
166: assertEquals(
167: // Should constructor type parameter be followed by a type bound? It is unspecified.
168: // The known reference implementation doesn't do it as well:
169: //"protected <T extends java.lang.Throwable & "
170: //+ "java.lang.Comparable<java.lang.Throwable>>"
171: "protected strictfp <T>"
172: + " java.lang.reflect.Ctor5Test$C(T,java.lang.reflect.OneParamType<? super T>)"
173: + " throws T,java.lang.Error", s);
174: }
175:
176: /**
177: * toGenericString() should return a string exactly matching
178: * the API specification.
179: */
180: public void testToGenericString2() throws Exception {
181: String s = B.class.getConstructor(Object.class, int[].class)
182: .toGenericString();
183: System.out.println(s);
184: assertEquals("public java.lang.reflect.Ctor5Test$B("
185: + "java.lang.Object,int[])", s);
186: }
187:
188: }
189:
190: class AnnotatedCtor {
191: @TagAntn
192: public AnnotatedCtor() {
193: }
194:
195: @TagAntn
196: @ValAntn
197: public AnnotatedCtor(Object param) {
198: }
199:
200: public AnnotatedCtor(@P1Antn
201: String s) {
202: }
203:
204: @notfound.MissingAntn
205: public AnnotatedCtor(int i) {
206: }
207:
208: @MissingClassValAntn
209: public AnnotatedCtor(long l) {
210: }
211:
212: @MissingTypeAntn
213: public AnnotatedCtor(char ch) {
214: }
215: }
216:
217: class AnnotatedParamCtor {
218: @TagAntn
219: public AnnotatedParamCtor() {
220: }
221:
222: @ValAntn("abc")
223: public AnnotatedParamCtor(@P1Antn
224: Object p1, @P2Antn(123)
225: Class p2, @P3Antn
226: @ValAntn("xyz")
227: boolean p3) {
228: }
229:
230: public AnnotatedParamCtor(String s, @TagAntn
231: int i) {
232: }
233: }
|