1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.reflect;
18
19 import static org.apache.juneau.reflect.ConstructorInfo.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.lang.reflect.*;
23 import java.util.*;
24 import java.util.function.*;
25 import java.util.stream.*;
26
27 import org.apache.juneau.*;
28 import org.junit.jupiter.api.*;
29
30 class ConstructorInfoTest extends TestBase {
31
32 private static void check(String expected, Object o) {
33 assertEquals(expected, TO_STRING.apply(o));
34 }
35
36 private static final Function<Object,String> TO_STRING = new Function<>() {
37 @Override
38 public String apply(Object t) {
39 if (t == null)
40 return null;
41 if (t instanceof Iterable)
42 return StreamSupport.stream(((Iterable<?>)t).spliterator(), false).map(this).collect(Collectors.joining(","));
43 if (t instanceof ClassInfo)
44 return ((ClassInfo)t).getSimpleName();
45 if (t instanceof ConstructorInfo)
46 return ((ConstructorInfo)t).getShortName();
47 if (t instanceof Constructor)
48 return ConstructorInfo.of((Constructor<?>)t).getShortName();
49 return t.toString();
50 }
51 };
52
53 private static ConstructorInfo ofc(Class<?> c, Class<?>...pt) {
54 try {
55 return of(c.getConstructor(pt));
56 } catch (NoSuchMethodException | SecurityException e) {
57 fail(e.getLocalizedMessage());
58 }
59 return null;
60 }
61
62
63
64
65
66 static class A {
67 public A() {}
68 }
69 static ConstructorInfo a = ofc(A.class);
70
71 @Test void of_withDeclaringClass() {
72 check("A()", ConstructorInfo.of(ClassInfo.of(A.class), a.inner()));
73 }
74
75 @Test void of_noDeclaringClass() {
76 check("A()", a.inner());
77 }
78
79 @Test void getDeclaringClass() {
80 check("A", a.getDeclaringClass());
81 }
82
83 @Test void of_null() {
84 check(null, ConstructorInfo.of(null));
85 check(null, ConstructorInfo.of(null, null));
86 }
87
88
89
90
91
92 public static class B {
93 private String f;
94 public B() {}
95 public B(String f) {
96 this.f = f;
97 }
98 public B(String f, String f2) {
99 this.f = f;
100 }
101 protected B(int f) {}
102 @Override
103 public String toString() {
104 return f;
105 }
106 }
107 static ClassInfo b = ClassInfo.of(B.class);
108 static ConstructorInfo
109 b_c1 = b.getPublicConstructor(ConstructorInfo::hasNoParams),
110 b_c2 = b.getPublicConstructor(x -> x.hasParamTypes(String.class)),
111 b_c3 = b.getDeclaredConstructor(x -> x.hasParamTypes(int.class)),
112 b_c4 = b.getPublicConstructor(x -> x.hasParamTypes(String.class, String.class));
113
114
115 @Test void invoke() throws Exception {
116 assertEquals(null, b_c1.invokeFuzzy().toString());
117 assertEquals("foo", b_c2.invokeFuzzy("foo").toString());
118 }
119
120 @Test void accessible() throws Exception {
121 b_c3.accessible(Visibility.PROTECTED);
122 assertEquals(null, b_c3.invokeFuzzy(123).toString());
123 }
124
125 @Test void compareTo() {
126 var s = new TreeSet<>(Arrays.asList(b_c1, b_c2, b_c3, b_c4, a));
127 check("A(),B(),B(int),B(String),B(String,String)", s);
128
129 }
130 }