1
1
package org .junit .runners .model ;
2
2
3
3
import static java .lang .reflect .Modifier .isStatic ;
4
+ import static org .junit .internal .MethodSorter .NAME_ASCENDING ;
4
5
5
6
import java .lang .annotation .Annotation ;
6
7
import java .lang .reflect .Constructor ;
11
12
import java .util .Collections ;
12
13
import java .util .Comparator ;
13
14
import java .util .LinkedHashMap ;
15
+ import java .util .LinkedHashSet ;
14
16
import java .util .List ;
15
17
import java .util .Map ;
18
+ import java .util .Set ;
16
19
17
20
import org .junit .Assert ;
18
21
import org .junit .Before ;
24
27
*
25
28
* @since 4.5
26
29
*/
27
- public class TestClass {
30
+ public class TestClass implements Annotatable {
31
+ private static final FieldComparator FIELD_COMPARATOR = new FieldComparator ();
32
+ private static final MethodComparator METHOD_COMPARATOR = new MethodComparator ();
33
+
28
34
private final Class <?> fClass ;
29
35
private final Map <Class <? extends Annotation >, List <FrameworkMethod >> fMethodsForAnnotations ;
30
36
private final Map <Class <? extends Annotation >, List <FrameworkField >> fFieldsForAnnotations ;
@@ -68,11 +74,7 @@ protected void scanAnnotatedMembers(Map<Class<? extends Annotation>, List<Framew
68
74
69
75
private static Field [] getSortedDeclaredFields (Class <?> clazz ) {
70
76
Field [] declaredFields = clazz .getDeclaredFields ();
71
- Arrays .sort (declaredFields , new Comparator <Field >() {
72
- public int compare (Field field1 , Field field2 ) {
73
- return field1 .getName ().compareTo (field2 .getName ());
74
- }
75
- });
77
+ Arrays .sort (declaredFields , FIELD_COMPARATOR );
76
78
return declaredFields ;
77
79
}
78
80
@@ -102,6 +104,17 @@ protected static <T extends FrameworkMember<T>> void addToAnnotationLists(T memb
102
104
return Collections .unmodifiableMap (copy );
103
105
}
104
106
107
+ /**
108
+ * Returns, efficiently, all the non-overridden methods in this class and
109
+ * its superclasses that are annotated}.
110
+ *
111
+ * @since 4.12
112
+ */
113
+ public List <FrameworkMethod > getAnnotatedMethods () {
114
+ List <FrameworkMethod > methods = collectValues (fMethodsForAnnotations );
115
+ Collections .sort (methods , METHOD_COMPARATOR );
116
+ return methods ;
117
+ }
105
118
106
119
/**
107
120
* Returns, efficiently, all the non-overridden methods in this class and
@@ -114,31 +127,29 @@ public List<FrameworkMethod> getAnnotatedMethods(
114
127
115
128
/**
116
129
* Returns, efficiently, all the non-overridden fields in this class and its
117
- * superclasses that are annotated with {@code annotationClass}.
130
+ * superclasses that are annotated.
131
+ *
132
+ * @since 4.12
118
133
*/
119
- public List <FrameworkField > getAnnotatedFields (
120
- Class <? extends Annotation > annotationClass ) {
121
- return Collections .unmodifiableList (getAnnotatedMembers (fFieldsForAnnotations , annotationClass , false ));
134
+ public List <FrameworkField > getAnnotatedFields () {
135
+ return collectValues (fFieldsForAnnotations );
122
136
}
123
137
124
138
/**
125
- * Gets a {@code Map} between annotations and methods that have
126
- * the annotation in this class or its superclasses.
127
- *
128
- * @since 4.12
139
+ * Returns, efficiently, all the non-overridden fields in this class and its
140
+ * superclasses that are annotated with {@code annotationClass}.
129
141
*/
130
- public Map <Class <? extends Annotation >, List <FrameworkMethod >> getAnnotationToMethods () {
131
- return fMethodsForAnnotations ;
142
+ public List <FrameworkField > getAnnotatedFields (
143
+ Class <? extends Annotation > annotationClass ) {
144
+ return Collections .unmodifiableList (getAnnotatedMembers (fFieldsForAnnotations , annotationClass , false ));
132
145
}
133
146
134
- /**
135
- * Gets a {@code Map} between annotations and fields that have
136
- * the annotation in this class or its superclasses.
137
- *
138
- * @since 4.12
139
- */
140
- public Map <Class <? extends Annotation >, List <FrameworkField >> getAnnotationToFields () {
141
- return fFieldsForAnnotations ;
147
+ private <T > List <T > collectValues (Map <?, List <T >> map ) {
148
+ Set <T > values = new LinkedHashSet <T >();
149
+ for (List <T > additionalValues : map .values ()) {
150
+ values .addAll (additionalValues );
151
+ }
152
+ return new ArrayList <T >(values );
142
153
}
143
154
144
155
private static <T > List <T > getAnnotatedMembers (Map <Class <? extends Annotation >, List <T >> map ,
@@ -240,4 +251,23 @@ public <T> List<T> getAnnotatedMethodValues(Object test,
240
251
public boolean isANonStaticInnerClass () {
241
252
return fClass .isMemberClass () && !isStatic (fClass .getModifiers ());
242
253
}
254
+
255
+ /**
256
+ * Compares two fields by its name.
257
+ */
258
+ private static class FieldComparator implements Comparator <Field > {
259
+ public int compare (Field left , Field right ) {
260
+ return left .getName ().compareTo (right .getName ());
261
+ }
262
+ }
263
+
264
+ /**
265
+ * Compares two methods by its name.
266
+ */
267
+ private static class MethodComparator implements
268
+ Comparator <FrameworkMethod > {
269
+ public int compare (FrameworkMethod left , FrameworkMethod right ) {
270
+ return NAME_ASCENDING .compare (left .getMethod (), right .getMethod ());
271
+ }
272
+ }
243
273
}
0 commit comments