Skip to content

Commit 4a3ca17

Browse files
committed
Create ConstructorUtilsTest.java
1 parent 26ec721 commit 4a3ca17

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.microsphere.annotation.processor.util;
19+
20+
21+
import io.microsphere.annotation.processor.AbstractAnnotationProcessingTest;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.core.env.Environment;
24+
25+
import javax.lang.model.element.ExecutableElement;
26+
import java.io.Serializable;
27+
import java.util.List;
28+
29+
import static io.microsphere.annotation.processor.util.ConstructorUtils.findDeclaredConstructor;
30+
import static io.microsphere.annotation.processor.util.ConstructorUtils.findDeclaredConstructors;
31+
import static io.microsphere.annotation.processor.util.ConstructorUtils.getDeclaredConstructors;
32+
import static io.microsphere.annotation.processor.util.ElementUtils.matchParameterTypes;
33+
import static io.microsphere.lang.function.Predicates.alwaysFalse;
34+
import static io.microsphere.lang.function.Predicates.alwaysTrue;
35+
import static java.util.Collections.emptyList;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertNull;
38+
import static org.junit.jupiter.api.Assertions.assertSame;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
41+
/**
42+
* {@link ConstructorUtils} Test
43+
*
44+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
45+
* @see ConstructorUtils
46+
* @since 1.0.0
47+
*/
48+
class ConstructorUtilsTest extends AbstractAnnotationProcessingTest {
49+
50+
@Test
51+
void testGetDeclaredConstructors() {
52+
List<ExecutableElement> constructors = getDeclaredConstructors(this.testTypeElement);
53+
assertTestServiceImplConstructors(constructors);
54+
55+
constructors = getDeclaredConstructors(this.testDeclaredType);
56+
assertTestServiceImplConstructors(constructors);
57+
}
58+
59+
@Test
60+
public void testGetDeclaredConstructorsOnNull() {
61+
assertSame(emptyList(), getDeclaredConstructors(NULL_TYPE_ELEMENT));
62+
assertSame(emptyList(), getDeclaredConstructors(NULL_TYPE_MIRROR));
63+
}
64+
65+
@Test
66+
void testFindDeclaredConstructor() {
67+
assertTestServiceImpl1stConstructor(findDeclaredConstructor(this.testTypeElement));
68+
assertTestServiceImpl1stConstructor(findDeclaredConstructor(this.testDeclaredType));
69+
70+
assertTestServiceImpl2ndConstructor(findDeclaredConstructor(this.testTypeElement, Environment.class));
71+
assertTestServiceImpl2ndConstructor(findDeclaredConstructor(this.testDeclaredType, Environment.class));
72+
}
73+
74+
@Test
75+
void testFindDeclaredConstructorOnNull() {
76+
assertNull(findDeclaredConstructor(NULL_TYPE_ELEMENT));
77+
assertNull(findDeclaredConstructor(NULL_TYPE_MIRROR));
78+
79+
assertNull(findDeclaredConstructor(this.testTypeElement, null));
80+
assertNull(findDeclaredConstructor(this.testDeclaredType, null));
81+
}
82+
83+
@Test
84+
void testFindDeclaredConstructorOnMismatch() {
85+
assertNull(findDeclaredConstructor(NULL_TYPE_ELEMENT, Object.class));
86+
assertNull(findDeclaredConstructor(NULL_TYPE_MIRROR, Object.class));
87+
88+
assertNull(findDeclaredConstructor(NULL_TYPE_ELEMENT, Object.class, String.class));
89+
assertNull(findDeclaredConstructor(NULL_TYPE_MIRROR, Object.class, String.class));
90+
91+
assertNull(findDeclaredConstructor(NULL_TYPE_ELEMENT, Object.class, String.class, Integer.class));
92+
assertNull(findDeclaredConstructor(NULL_TYPE_MIRROR, Object.class, String.class, Integer.class));
93+
}
94+
95+
@Test
96+
void testFindDeclaredConstructors() {
97+
List<ExecutableElement> constructors = findDeclaredConstructors(this.testTypeElement);
98+
assertTestServiceImplConstructors(constructors);
99+
100+
constructors = findDeclaredConstructors(this.testDeclaredType);
101+
assertTestServiceImplConstructors(constructors);
102+
103+
constructors = findDeclaredConstructors(this.testTypeElement, alwaysTrue());
104+
assertTestServiceImplConstructors(constructors);
105+
106+
constructors = findDeclaredConstructors(this.testDeclaredType, alwaysTrue());
107+
assertTestServiceImplConstructors(constructors);
108+
}
109+
110+
@Test
111+
void testFindDeclaredConstructorsOnNull() {
112+
assertSame(emptyList(), findDeclaredConstructors(NULL_TYPE_ELEMENT));
113+
assertSame(emptyList(), findDeclaredConstructors(NULL_TYPE_MIRROR));
114+
115+
assertSame(emptyList(), findDeclaredConstructors(this.testTypeElement, null));
116+
assertSame(emptyList(), findDeclaredConstructors(this.testDeclaredType, null));
117+
}
118+
119+
@Test
120+
void testFindDeclaredConstructorsOnMismatch() {
121+
assertSame(emptyList(), findDeclaredConstructors(this.testTypeElement, alwaysFalse()));
122+
assertSame(emptyList(), findDeclaredConstructors(this.testDeclaredType, alwaysFalse()));
123+
}
124+
125+
@Test
126+
void testFindDeclaredConstructorsOnNotFound() {
127+
assertSame(emptyList(), findDeclaredConstructors(getTypeElement(Serializable.class)));
128+
assertSame(emptyList(), findDeclaredConstructors(getDeclaredType(Serializable.class)));
129+
130+
assertSame(emptyList(), findDeclaredConstructors(getTypeElement(List.class)));
131+
assertSame(emptyList(), findDeclaredConstructors(getDeclaredType(List.class)));
132+
}
133+
134+
void assertTestServiceImplConstructors(List<? extends ExecutableElement> constructors) {
135+
assertEquals(2, constructors.size());
136+
assertTestServiceImpl1stConstructor(constructors.get(0));
137+
assertTestServiceImpl2ndConstructor(constructors.get(1));
138+
}
139+
140+
void assertTestServiceImpl1stConstructor(ExecutableElement constructor) {
141+
assertEquals(this.testTypeElement, constructor.getEnclosingElement());
142+
assertEquals(emptyList(), constructor.getParameters());
143+
}
144+
145+
void assertTestServiceImpl2ndConstructor(ExecutableElement constructor) {
146+
assertEquals(this.testTypeElement, constructor.getEnclosingElement());
147+
assertEquals(1, constructor.getParameters().size());
148+
assertTrue(matchParameterTypes(constructor, Environment.class));
149+
}
150+
}

0 commit comments

Comments
 (0)