1 package org.junit.internal;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertArrayEquals;
5
6 import java.lang.reflect.Method;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10
11 import org.junit.FixMethodOrder;
12 import org.junit.Test;
13 import org.junit.runners.MethodSorters;
14
15 public class MethodSorterTest {
16 private static final String ALPHA = "java.lang.Object alpha(int,double,java.lang.Thread)";
17 private static final String BETA = "void beta(int[][])";
18 private static final String GAMMA_VOID = "int gamma()";
19 private static final String GAMMA_BOOLEAN = "void gamma(boolean)";
20 private static final String DELTA = "void delta()";
21 private static final String EPSILON = "void epsilon()";
22 private static final String SUPER_METHOD = "void superMario()";
23 private static final String SUB_METHOD = "void subBowser()";
24
25 static class DummySortWithoutAnnotation {
26 Object alpha(int i, double d, Thread t) {
27 return null;
28 }
29
30 void beta(int[][] x) {
31 }
32
33 int gamma() {
34 return 0;
35 }
36
37 void gamma(boolean b) {
38 }
39
40 void delta() {
41 }
42
43 void epsilon() {
44 }
45 }
46
47 static class Super {
48 void superMario() {
49 }
50 }
51
52 static class Sub extends Super {
53 void subBowser() {
54 }
55 }
56
57 private List<String> getDeclaredMethodNames(Class<?> clazz) {
58 Method[] actualMethods = MethodSorter.getDeclaredMethods(clazz);
59
60
61 List<String> names = new ArrayList<String>();
62 for (Method m : actualMethods) {
63
64 if (!m.isSynthetic()) {
65 names.add(m.toString().replace(clazz.getName() + '.', ""));
66 }
67 }
68
69 return names;
70 }
71
72 @Test
73 public void testMethodsNullSorterSelf() {
74 List<String> expected = Arrays.asList(EPSILON, BETA, ALPHA, DELTA, GAMMA_VOID, GAMMA_BOOLEAN);
75 List<String> actual = getDeclaredMethodNames(DummySortWithoutAnnotation.class);
76 assertEquals(expected, actual);
77 }
78
79 @Test
80 public void testMethodsNullSorterSuper() {
81 List<String> expected = Arrays.asList(SUPER_METHOD);
82 List<String> actual = getDeclaredMethodNames(Super.class);
83 assertEquals(expected, actual);
84 }
85
86 @Test
87 public void testMethodsNullSorterSub() {
88 List<String> expected = Arrays.asList(SUB_METHOD);
89 List<String> actual = getDeclaredMethodNames(Sub.class);
90 assertEquals(expected, actual);
91 }
92
93 @FixMethodOrder(MethodSorters.DEFAULT)
94 static class DummySortWithDefault {
95 Object alpha(int i, double d, Thread t) {
96 return null;
97 }
98
99 void beta(int[][] x) {
100 }
101
102 int gamma() {
103 return 0;
104 }
105
106 void gamma(boolean b) {
107 }
108
109 void delta() {
110 }
111
112 void epsilon() {
113 }
114 }
115
116 @Test
117 public void testDefaultMethodSorter() {
118 List<String> expected = Arrays.asList(EPSILON, BETA, ALPHA, DELTA, GAMMA_VOID, GAMMA_BOOLEAN);
119 List<String> actual = getDeclaredMethodNames(DummySortWithDefault.class);
120 assertEquals(expected, actual);
121 }
122
123 @FixMethodOrder(MethodSorters.JVM)
124 static class DummySortJvm {
125 Object alpha(int i, double d, Thread t) {
126 return null;
127 }
128
129 void beta(int[][] x) {
130 }
131
132 int gamma() {
133 return 0;
134 }
135
136 void gamma(boolean b) {
137 }
138
139 void delta() {
140 }
141
142 void epsilon() {
143 }
144 }
145
146 @Test
147 public void testJvmMethodSorter() {
148 Method[] fromJvmWithSynthetics = DummySortJvm.class.getDeclaredMethods();
149 Method[] sorted = MethodSorter.getDeclaredMethods(DummySortJvm.class);
150 assertArrayEquals(fromJvmWithSynthetics, sorted);
151 }
152
153 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
154 static class DummySortWithNameAsc {
155 Object alpha(int i, double d, Thread t) {
156 return null;
157 }
158
159 void beta(int[][] x) {
160 }
161
162 int gamma() {
163 return 0;
164 }
165
166 void gamma(boolean b) {
167 }
168
169 void delta() {
170 }
171
172 void epsilon() {
173 }
174 }
175
176 @Test
177 public void testAscendingMethodSorter() {
178 List<String> expected = Arrays.asList(ALPHA, BETA, DELTA, EPSILON, GAMMA_VOID, GAMMA_BOOLEAN);
179 List<String> actual = getDeclaredMethodNames(DummySortWithNameAsc.class);
180 assertEquals(expected, actual);
181 }
182 }