Skip to content

Commit c8d4e4b

Browse files
authored
Merge pull request #129 from mercyblitz/dev
Test coverage
2 parents 2b57675 + 5b04a5c commit c8d4e4b

77 files changed

Lines changed: 2172 additions & 727 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/AnnotationUtils.java

Lines changed: 177 additions & 42 deletions
Large diffs are not rendered by default.

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/FieldUtils.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
import javax.lang.model.element.Modifier;
2121
import javax.lang.model.element.VariableElement;
2222
import javax.lang.model.type.TypeMirror;
23-
import java.util.Collection;
2423
import java.util.List;
2524
import java.util.function.Predicate;
26-
import java.util.stream.Collectors;
2725

26+
import static io.microsphere.annotation.processor.util.MemberUtils.getAllDeclaredMembers;
2827
import static io.microsphere.annotation.processor.util.MemberUtils.getDeclaredMembers;
2928
import static io.microsphere.annotation.processor.util.MemberUtils.hasModifiers;
3029
import static io.microsphere.annotation.processor.util.MemberUtils.matchesElementKind;
31-
import static io.microsphere.annotation.processor.util.TypeUtils.getAllDeclaredTypes;
3230
import static io.microsphere.annotation.processor.util.TypeUtils.isEnumType;
31+
import static io.microsphere.collection.CollectionUtils.isEmpty;
3332
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
3433
import static io.microsphere.lang.function.Streams.filterAll;
3534
import static io.microsphere.lang.function.Streams.filterFirst;
35+
import static io.microsphere.util.ArrayUtils.isNotEmpty;
3636
import static java.util.Collections.emptyList;
3737
import static javax.lang.model.element.ElementKind.ENUM_CONSTANT;
3838
import static javax.lang.model.element.ElementKind.FIELD;
@@ -47,6 +47,14 @@
4747
*/
4848
public interface FieldUtils {
4949

50+
static VariableElement getDeclaredField(Element element, String fieldName) {
51+
return element == null ? null : getDeclaredField(element.asType(), fieldName);
52+
}
53+
54+
static VariableElement getDeclaredField(TypeMirror type, String fieldName) {
55+
return filterFirst(findDeclaredFields(type, field -> fieldName.equals(field.getSimpleName().toString())));
56+
}
57+
5058
static List<VariableElement> getDeclaredFields(Element element) {
5159
return findDeclaredFields(element, EMPTY_PREDICATE_ARRAY);
5260
}
@@ -63,40 +71,50 @@ static List<VariableElement> getAllDeclaredFields(TypeMirror type) {
6371
return findAllDeclaredFields(type, EMPTY_PREDICATE_ARRAY);
6472
}
6573

74+
static VariableElement findField(Element element, String fieldName) {
75+
return element == null ? null : findField(element.asType(), fieldName);
76+
}
77+
78+
static VariableElement findField(TypeMirror type, String fieldName) {
79+
return filterFirst(findAllDeclaredFields(type, field -> equalsFieldName(field, fieldName)));
80+
}
81+
6682
static List<VariableElement> findDeclaredFields(Element element, Predicate<? super VariableElement>... fieldFilters) {
6783
return element == null ? emptyList() : findDeclaredFields(element.asType(), fieldFilters);
6884
}
6985

7086
static List<VariableElement> findDeclaredFields(TypeMirror type, Predicate<? super VariableElement>... fieldFilters) {
71-
return filterAll(fieldsIn(getDeclaredMembers(type)), fieldFilters);
87+
return filterDeclaredFields(type, false, fieldFilters);
7288
}
7389

7490
static List<VariableElement> findAllDeclaredFields(Element element, Predicate<? super VariableElement>... fieldFilters) {
7591
return element == null ? emptyList() : findAllDeclaredFields(element.asType(), fieldFilters);
7692
}
7793

7894
static List<VariableElement> findAllDeclaredFields(TypeMirror type, Predicate<? super VariableElement>... fieldFilters) {
79-
return getAllDeclaredTypes(type)
80-
.stream()
81-
.map(t -> findDeclaredFields(t, fieldFilters))
82-
.flatMap(Collection::stream)
83-
.collect(Collectors.toList());
95+
return filterDeclaredFields(type, true, fieldFilters);
8496
}
8597

86-
static VariableElement getDeclaredField(Element element, String fieldName) {
87-
return element == null ? null : getDeclaredField(element.asType(), fieldName);
88-
}
98+
static List<VariableElement> filterDeclaredFields(TypeMirror type, boolean all, Predicate<? super VariableElement>... fieldFilters) {
99+
if (type == null) {
100+
return emptyList();
101+
}
89102

90-
static VariableElement getDeclaredField(TypeMirror type, String fieldName) {
91-
return filterFirst(findDeclaredFields(type, field -> fieldName.equals(field.getSimpleName().toString())));
92-
}
103+
List<? extends Element> declaredMembers = all ? getAllDeclaredMembers(type) : getDeclaredMembers(type);
104+
if (isEmpty(declaredMembers)) {
105+
return emptyList();
106+
}
93107

94-
static VariableElement findField(Element element, String fieldName) {
95-
return element == null ? null : findField(element.asType(), fieldName);
96-
}
108+
List<VariableElement> fields = fieldsIn(declaredMembers);
109+
if (isEmpty(fields)) {
110+
return emptyList();
111+
}
97112

98-
static VariableElement findField(TypeMirror type, String fieldName) {
99-
return filterFirst(findAllDeclaredFields(type, field -> equals(field, fieldName)));
113+
if (isNotEmpty(fieldFilters)) {
114+
fields = filterAll(fields, fieldFilters);
115+
}
116+
117+
return isEmpty(fields) ? emptyList() : fields;
100118
}
101119

102120
/**
@@ -140,8 +158,7 @@ static List<VariableElement> getAllNonStaticFields(Element element) {
140158
return element == null ? emptyList() : getAllNonStaticFields(element.asType());
141159
}
142160

143-
static boolean equals(VariableElement field, CharSequence fieldName) {
161+
static boolean equalsFieldName(VariableElement field, CharSequence fieldName) {
144162
return field != null && fieldName != null && field.getSimpleName().toString().equals(fieldName.toString());
145163
}
146-
147164
}

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/LoggerUtils.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,22 @@ public interface LoggerUtils {
3232
Logger LOGGER = getLogger("microsphere-annotation-processor");
3333

3434
static void trace(String format, Object... args) {
35-
if (LOGGER.isTraceEnabled()) {
36-
LOGGER.trace(format, args);
37-
}
35+
LOGGER.trace(format, args);
3836
}
3937

4038
static void debug(String format, Object... args) {
41-
if (LOGGER.isDebugEnabled()) {
42-
LOGGER.debug(format, args);
43-
}
39+
LOGGER.debug(format, args);
4440
}
4541

4642
static void info(String format, Object... args) {
47-
if (LOGGER.isInfoEnabled()) {
48-
LOGGER.info(format, args);
49-
}
43+
LOGGER.info(format, args);
5044
}
5145

5246
static void warn(String format, Object... args) {
53-
if (LOGGER.isWarnEnabled()) {
54-
LOGGER.warn(format, args);
55-
}
47+
LOGGER.warn(format, args);
5648
}
5749

5850
static void error(String format, Object... args) {
59-
if (LOGGER.isErrorEnabled()) {
60-
LOGGER.error(format, args);
61-
}
51+
LOGGER.error(format, args);
6252
}
6353
}

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/MethodUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ static List<ExecutableElement> findAllDeclaredMethods(TypeMirror type, Type... e
111111

112112
static List<ExecutableElement> findPublicNonStaticMethods(TypeElement type, Type... excludedTypes) {
113113
return type == null ? emptyList() : findPublicNonStaticMethods(ofDeclaredType(type), excludedTypes);
114-
115114
}
116115

117116
static List<ExecutableElement> findPublicNonStaticMethods(TypeMirror type, Type... excludedTypes) {

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/TypeUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ static boolean isSameType(TypeMirror type, CharSequence typeName) {
110110
return Objects.equals(valueOf(type), valueOf(typeName));
111111
}
112112

113-
114113
static boolean isArrayType(TypeMirror type) {
115114
return type != null && ARRAY == type.getKind();
116115
}
@@ -278,6 +277,10 @@ static List<TypeElement> getAllTypeElementsOfInterfaces(TypeElement type) {
278277
return findAllTypeElementsOfInterfaces(type, EMPTY_PREDICATE_ARRAY);
279278
}
280279

280+
static List<TypeElement> getTypeElements(TypeElement type) {
281+
return getTypeElements(type, true, false, true, true);
282+
}
283+
281284
static List<TypeElement> getAllTypeElements(TypeElement type) {
282285
return getTypeElements(type, true, true, true, true);
283286
}
@@ -319,7 +322,7 @@ static List<TypeElement> findTypeElements(TypeElement type,
319322
return emptyList();
320323
}
321324
assertNoNullElements(typeFilters, () -> "Any element of 'typeFilters' array must not be null");
322-
return type == null ? emptyList() : typeElementFinder(type, includeSelf, includeHierarchicalTypes, includeSuperclass, includeSuperInterfaces).findTypes(typeFilters);
325+
return typeElementFinder(type, includeSelf, includeHierarchicalTypes, includeSuperclass, includeSuperInterfaces).findTypes(typeFilters);
323326
}
324327

325328
static DeclaredType getDeclaredTypeOfSuperclass(Element typeElement) {

microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/AbstractAnnotationProcessingTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
import org.junit.jupiter.api.extension.ExtendWith;
2323

2424
import javax.annotation.processing.ProcessingEnvironment;
25+
import javax.lang.model.AnnotatedConstruct;
26+
import javax.lang.model.element.AnnotationMirror;
2527
import javax.lang.model.element.Element;
28+
import javax.lang.model.element.ElementKind;
29+
import javax.lang.model.element.ExecutableElement;
30+
import javax.lang.model.element.Modifier;
2631
import javax.lang.model.element.TypeElement;
32+
import javax.lang.model.element.VariableElement;
2733
import javax.lang.model.type.DeclaredType;
2834
import javax.lang.model.type.TypeMirror;
2935
import javax.lang.model.util.Elements;
@@ -33,8 +39,11 @@
3339
import java.util.Collection;
3440
import java.util.List;
3541
import java.util.Set;
42+
import java.util.function.Predicate;
3643

3744
import static io.microsphere.annotation.processor.util.TypeUtils.ofDeclaredType;
45+
import static java.util.Collections.emptyList;
46+
import static org.junit.jupiter.api.Assertions.assertSame;
3847

3948
/**
4049
* Abstract {@link Annotation} Processing Test case
@@ -55,8 +64,12 @@ public abstract class AbstractAnnotationProcessingTest {
5564

5665
protected static final Collection NULL_COLLECTION = null;
5766

67+
protected static final List NULL_LIST = null;
68+
5869
protected static final Element NULL_ELEMENT = null;
5970

71+
protected static final ElementKind NULL_ELEMENT_KIND = null;
72+
6073
protected static final Element[] EMPTY_ELEMENT_ARRAY = new Element[0];
6174

6275
protected static final Element[] NULL_ELEMENT_ARRAY = null;
@@ -75,6 +88,26 @@ public abstract class AbstractAnnotationProcessingTest {
7588

7689
protected static final String[] NULL_STRING_ARRAY = null;
7790

91+
protected static final Class NULL_CLASS = null;
92+
93+
protected static final Class[] NULL_CLASS_ARRAY = null;
94+
95+
protected static final AnnotatedConstruct NULL_ANNOTATED_CONSTRUCT = null;
96+
97+
protected static final Predicate[] NULL_PREDICATE_ARRAY = null;
98+
99+
protected static final VariableElement NULL_FIELD = null;
100+
101+
protected static final Modifier NULL_MODIFIER = null;
102+
103+
protected static final Modifier[] NULL_MODIFIER_ARRAY = null;
104+
105+
protected static final ExecutableElement NULL_METHOD = null;
106+
107+
protected static final ExecutableElement[] NULL_METHOD_ARRAY = null;
108+
109+
protected static final AnnotationMirror NULL_ANNOTATION_MIRROR = null;
110+
78111
static ThreadLocal<AbstractAnnotationProcessingTest> testInstanceHolder = new ThreadLocal<>();
79112

80113
protected ProcessingEnvironment processingEnv;
@@ -141,4 +174,8 @@ protected DeclaredType getDeclaredType(Type type) {
141174
return TypeUtils.getDeclaredType(processingEnv, type);
142175
}
143176

177+
protected void assertEmptyList(List<?> list) {
178+
assertSame(emptyList(), list);
179+
}
180+
144181
}

microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/Compiler.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
import javax.annotation.processing.Processor;
2222
import javax.tools.JavaCompiler;
23+
import javax.tools.JavaCompiler.CompilationTask;
2324
import javax.tools.JavaFileObject;
2425
import javax.tools.StandardJavaFileManager;
2526
import java.io.File;
2627
import java.io.IOException;
2728
import java.net.URL;
28-
import java.util.Collections;
2929
import java.util.LinkedHashSet;
3030
import java.util.List;
3131
import java.util.Set;
@@ -39,6 +39,7 @@
3939
import static io.microsphere.logging.LoggerFactory.getLogger;
4040
import static io.microsphere.util.ClassUtils.getTypeName;
4141
import static io.microsphere.util.StringUtils.substringBefore;
42+
import static java.util.Collections.singleton;
4243
import static javax.tools.StandardLocation.CLASS_OUTPUT;
4344
import static javax.tools.StandardLocation.SOURCE_OUTPUT;
4445
import static javax.tools.ToolProvider.getSystemJavaCompiler;
@@ -73,8 +74,8 @@ public Compiler(File defaultSourceDirectory, File targetDirectory) throws IOExce
7374
this.sourcePaths = newLinkedHashSet(defaultSourceDirectory);
7475
this.javaCompiler = getSystemJavaCompiler();
7576
this.javaFileManager = javaCompiler.getStandardFileManager(null, null, null);
76-
this.javaFileManager.setLocation(CLASS_OUTPUT, Collections.singleton(targetDirectory));
77-
this.javaFileManager.setLocation(SOURCE_OUTPUT, Collections.singleton(targetDirectory));
77+
this.javaFileManager.setLocation(CLASS_OUTPUT, singleton(targetDirectory));
78+
this.javaFileManager.setLocation(SOURCE_OUTPUT, singleton(targetDirectory));
7879
}
7980

8081
public Compiler sourcePaths(File... sourcePaths) {
@@ -191,7 +192,7 @@ static String resolveJavaSourceFileRelativePath(Class<?> sourceClass) {
191192
}
192193

193194
public boolean compile(Class<?>... sourceClasses) {
194-
JavaCompiler.CompilationTask task = javaCompiler.getTask(null, this.javaFileManager, null,
195+
CompilationTask task = javaCompiler.getTask(null, this.javaFileManager, null,
195196
ofList("-parameters", "-Xlint:unchecked", "-nowarn", "-Xlint:deprecation"),
196197
// null,
197198
null, getJavaFileObjects(sourceClasses));

microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/TestServiceImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*/
1717
package io.microsphere.annotation.processor;
1818

19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.cache.annotation.Cacheable;
21+
import org.springframework.context.ApplicationContext;
22+
import org.springframework.core.env.Environment;
2023
import org.springframework.stereotype.Service;
2124

2225
import javax.xml.ws.ServiceMode;
@@ -30,6 +33,19 @@
3033
@ServiceMode
3134
public class TestServiceImpl extends GenericTestService implements TestService, AutoCloseable, Serializable {
3235

36+
@Autowired
37+
private ApplicationContext context;
38+
39+
private Environment environment;
40+
41+
public TestServiceImpl() {
42+
this(null);
43+
}
44+
45+
public TestServiceImpl(@Autowired Environment environment) {
46+
this.environment = environment;
47+
}
48+
3349
@Override
3450
@Cacheable(cacheNames = {"cache-1", "cache-2"})
3551
public String echo(String message) {

0 commit comments

Comments
 (0)