1 package org.junit.tests.manipulation; 2 3 import static org.junit.Assert.assertEquals; 4 5 import java.util.Comparator; 6 7 import junit.framework.JUnit4TestAdapter; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.experimental.runners.Enclosed; 11 import org.junit.runner.Description; 12 import org.junit.runner.JUnitCore; 13 import org.junit.runner.Request; 14 import org.junit.runner.RunWith; 15 import org.junit.runner.Runner; 16 import org.junit.runner.notification.RunNotifier; 17 18 @RunWith(Enclosed.class) 19 public class SortableTest { 20 private static Comparator<Description> forward() { 21 return new Comparator<Description>() { 22 public int compare(Description o1, Description o2) { 23 return o1.getDisplayName().compareTo(o2.getDisplayName()); 24 } 25 }; 26 } 27 28 private static Comparator<Description> backward() { 29 return new Comparator<Description>() { 30 public int compare(Description o1, Description o2) { 31 return o2.getDisplayName().compareTo(o1.getDisplayName()); 32 } 33 }; 34 } 35 36 public static class TestClassRunnerIsSortable { 37 private static String log = ""; 38 39 public static class SortMe { 40 @Test 41 public void a() { 42 log += "a"; 43 } 44 45 @Test 46 public void b() { 47 log += "b"; 48 } 49 50 @Test 51 public void c() { 52 log += "c"; 53 } 54 } 55 56 @Before 57 public void resetLog() { 58 log = ""; 59 } 60 61 @Test 62 public void sortingForwardWorksOnTestClassRunner() { 63 Request forward = Request.aClass(SortMe.class).sortWith(forward()); 64 65 new JUnitCore().run(forward); 66 assertEquals("abc", log); 67 } 68 69 @Test 70 public void sortingBackwardWorksOnTestClassRunner() { 71 Request backward = Request.aClass(SortMe.class).sortWith(backward()); 72 73 new JUnitCore().run(backward); 74 assertEquals("cba", log); 75 } 76 77 @RunWith(Enclosed.class) 78 public static class Enclosing { 79 public static class A { 80 @Test 81 public void a() { 82 log += "Aa"; 83 } 84 85 @Test 86 public void b() { 87 log += "Ab"; 88 } 89 90 @Test 91 public void c() { 92 log += "Ac"; 93 } 94 } 95 96 public static class B { 97 @Test 98 public void a() { 99 log += "Ba"; 100 } 101 102 @Test 103 public void b() { 104 log += "Bb"; 105 } 106 107 @Test 108 public void c() { 109 log += "Bc"; 110 } 111 } 112 } 113 114 @Test 115 public void sortingForwardWorksOnSuite() { 116 Request forward = Request.aClass(Enclosing.class).sortWith(forward()); 117 118 new JUnitCore().run(forward); 119 assertEquals("AaAbAcBaBbBc", log); 120 } 121 122 @Test 123 public void sortingBackwardWorksOnSuite() { 124 Request backward = Request.aClass(Enclosing.class).sortWith(backward()); 125 126 new JUnitCore().run(backward); 127 assertEquals("BcBbBaAcAbAa", log); 128 } 129 } 130 131 public static class TestClassRunnerIsSortableWithSuiteMethod { 132 private static String log = ""; 133 134 public static class SortMe { 135 @Test 136 public void a() { 137 log += "a"; 138 } 139 140 @Test 141 public void b() { 142 log += "b"; 143 } 144 145 @Test 146 public void c() { 147 log += "c"; 148 } 149 150 public static junit.framework.Test suite() { 151 return new JUnit4TestAdapter(SortMe.class); 152 } 153 } 154 155 @Before 156 public void resetLog() { 157 log = ""; 158 } 159 160 @Test 161 public void sortingForwardWorksOnTestClassRunner() { 162 Request forward = Request.aClass(SortMe.class).sortWith(forward()); 163 164 new JUnitCore().run(forward); 165 assertEquals("abc", log); 166 } 167 168 @Test 169 public void sortingBackwardWorksOnTestClassRunner() { 170 Request backward = Request.aClass(SortMe.class).sortWith(backward()); 171 172 new JUnitCore().run(backward); 173 assertEquals("cba", log); 174 } 175 } 176 177 public static class UnsortableRunnersAreHandledWithoutCrashing { 178 public static class UnsortableRunner extends Runner { 179 public UnsortableRunner(Class<?> klass) { 180 } 181 182 @Override 183 public Description getDescription() { 184 return Description.EMPTY; 185 } 186 187 @Override 188 public void run(RunNotifier notifier) { 189 } 190 } 191 192 @RunWith(UnsortableRunner.class) 193 public static class Unsortable { 194 @Test 195 public void a() { 196 } 197 } 198 199 @Test 200 public void unsortablesAreHandledWithoutCrashing() { 201 Request unsorted = Request.aClass(Unsortable.class).sortWith(forward()); 202 new JUnitCore().run(unsorted); 203 } 204 } 205 }