-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor]
- Loading branch information
Showing
11 changed files
with
282 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/testingsyndicate/jupiter/extensions/resources/Annotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.testingsyndicate.jupiter.extensions.resources; | ||
|
||
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Member; | ||
import java.lang.reflect.Parameter; | ||
import java.util.List; | ||
import org.junit.platform.commons.support.AnnotationSupport; | ||
import org.junit.platform.commons.support.SearchOption; | ||
|
||
class Annotations { | ||
private Annotations() {} | ||
|
||
static <T extends Annotation> T findHierarchical(AnnotatedElement element, Class<T> annotation) { | ||
var result = | ||
element instanceof Class<?> clazz | ||
? findAnnotation(clazz, annotation, SearchOption.INCLUDE_ENCLOSING_CLASSES) | ||
: findAnnotation(element, annotation); | ||
|
||
if (result.isPresent()) { | ||
return result.get(); | ||
} | ||
|
||
if (element instanceof Parameter parameter) { | ||
return findHierarchical(parameter.getDeclaringExecutable(), annotation); | ||
} | ||
|
||
if (element instanceof Member member) { | ||
return findHierarchical(member.getDeclaringClass(), annotation); | ||
} | ||
|
||
if (element instanceof Class<?> clazz) { | ||
return findHierarchical(clazz.getPackage(), annotation); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
static <T extends Annotation> List<AnnotatedField<T>> findAnnotatedFields( | ||
Class<?> clazz, Class<T> annotation) { | ||
return AnnotationSupport.findAnnotatedFields(clazz, annotation).stream() | ||
.map(f -> new AnnotatedField<>(f, f.getAnnotation(annotation))) | ||
.toList(); | ||
} | ||
|
||
record AnnotatedField<T extends Annotation>(Field field, T annotation) {} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/testingsyndicate/jupiter/extensions/resources/NameBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.testingsyndicate.jupiter.extensions.resources; | ||
|
||
class NameBuilder { | ||
private static final String SEPARATOR = "/"; | ||
private final StringBuilder name = new StringBuilder(); | ||
|
||
NameBuilder() {} | ||
|
||
public NameBuilder append(String part) { | ||
if (part == null) { | ||
return this; | ||
} | ||
if (part.startsWith(SEPARATOR) || name.isEmpty()) { | ||
name.replace(0, name.length(), part); | ||
return this; | ||
} | ||
if (!SEPARATOR.equals(name.substring(name.length() - SEPARATOR.length()))) { | ||
name.append(SEPARATOR); | ||
} | ||
name.append(part); | ||
|
||
return this; | ||
} | ||
|
||
public String build() { | ||
return name.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/test/java/com/testingsyndicate/jupiter/extensions/resources/AnnotationsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.testingsyndicate.jupiter.extensions.resources; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.reflect.AnnotatedElement; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class AnnotationsTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
void findsAnnotationHierarchically(AnnotatedElement element, String expected) { | ||
// when | ||
var actual = Annotations.findHierarchical(element, FindMe.class); | ||
|
||
// then | ||
var value = actual == null ? null : actual.value(); | ||
assertThat(value).isEqualTo(expected); | ||
} | ||
|
||
static Stream<Arguments> findsAnnotationHierarchically() throws Exception { | ||
var clazz = AnnotatedClass.class; | ||
var annotatedMethod = clazz.getDeclaredMethod("annotatedMethod", String.class, String.class); | ||
var unannotatedMethod = clazz.getDeclaredMethod("unannotatedMethod"); | ||
return Stream.of( | ||
arguments(clazz, "class"), | ||
arguments(clazz.getDeclaredField("annotatedField"), "field"), | ||
arguments(clazz.getDeclaredField("unannotatedField"), "class"), | ||
arguments(annotatedMethod, "method"), | ||
arguments(unannotatedMethod, "class"), | ||
arguments(annotatedMethod.getParameters()[0], "parameter"), | ||
arguments(annotatedMethod.getParameters()[1], "method"), | ||
arguments(UnannotatedClass.class, "package")); | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface FindMe { | ||
String value(); | ||
} | ||
|
||
@FindMe("class") | ||
private static class AnnotatedClass { | ||
|
||
@FindMe("field") | ||
private String annotatedField; | ||
|
||
private String unannotatedField; | ||
|
||
@FindMe("method") | ||
private void annotatedMethod( | ||
@FindMe("parameter") String annotatedParameter, String unannotatedParameter) {} | ||
|
||
private void unannotatedMethod() {} | ||
} | ||
|
||
private static class UnannotatedClass {} | ||
} |
Oops, something went wrong.