19
19
20
20
import java .util .Arrays ;
21
21
import java .util .Collections ;
22
- import java .util .HashSet ;
23
22
import java .util .List ;
24
- import java .util .Set ;
25
23
import org .junit .Test ;
26
24
import org .junit .runner .RunWith ;
27
25
import org .junit .runners .JUnit4 ;
28
26
29
27
@ RunWith (JUnit4 .class )
30
- public class ComponentSorterTest {
28
+ public class CycleDetectorTest {
31
29
private static final ComponentFactory <Object > NULL_FACTORY = container -> null ;
32
30
33
31
@ SuppressWarnings ("unchecked" )
@@ -58,7 +56,7 @@ private interface TestInterface7 {}
58
56
* ----> 2 <----
59
57
*/
60
58
@ Test
61
- public void sort_shouldTopologicallySortComponents1 () {
59
+ public void detect_shouldNotDetectACycle1 () {
62
60
List <Component <?>> components =
63
61
Arrays .asList (
64
62
Component .builder (TestInterface4 .class )
@@ -75,7 +73,7 @@ public void sort_shouldTopologicallySortComponents1() {
75
73
.factory (nullFactory ())
76
74
.build ());
77
75
78
- twice (() -> testSort (components ));
76
+ twice (() -> detect (components ));
79
77
}
80
78
81
79
/*
@@ -86,7 +84,7 @@ public void sort_shouldTopologicallySortComponents1() {
86
84
* 6 -> 7
87
85
*/
88
86
@ Test
89
- public void sort_shouldTopologicallySortComponents2 () {
87
+ public void detect_shouldNotDetectACycle2 () {
90
88
List <Component <?>> components =
91
89
Arrays .asList (
92
90
Component .builder (TestInterface1 .class )
@@ -112,7 +110,7 @@ public void sort_shouldTopologicallySortComponents2() {
112
110
.build (),
113
111
Component .builder (TestInterface7 .class ).factory (nullFactory ()).build ());
114
112
115
- twice (() -> testSort (components ));
113
+ twice (() -> detect (components ));
116
114
}
117
115
118
116
/*
@@ -127,7 +125,7 @@ public void sort_shouldTopologicallySortComponents2() {
127
125
* 5 6 7
128
126
*/
129
127
@ Test
130
- public void sort_shouldTopologicallySortComponents3 () {
128
+ public void detect_shouldNotDetectACycle3 () {
131
129
List <Component <?>> components =
132
130
Arrays .asList (
133
131
Component .builder (TestInterface1 .class )
@@ -152,7 +150,7 @@ public void sort_shouldTopologicallySortComponents3() {
152
150
Component .builder (TestInterface6 .class ).factory (nullFactory ()).build (),
153
151
Component .builder (TestInterface7 .class ).factory (nullFactory ()).build ());
154
152
155
- twice (() -> testSort (components ));
153
+ twice (() -> detect (components ));
156
154
}
157
155
158
156
/*
@@ -164,7 +162,7 @@ public void sort_shouldTopologicallySortComponents3() {
164
162
* 3 4 5 6
165
163
*/
166
164
@ Test
167
- public void sort_shouldTopologicallySortComponents4 () {
165
+ public void detect_shouldNotDetectACycle4 () {
168
166
List <Component <?>> components =
169
167
Arrays .asList (
170
168
Component .builder (TestInterface1 .class )
@@ -183,7 +181,7 @@ public void sort_shouldTopologicallySortComponents4() {
183
181
Component .builder (TestInterface6 .class ).factory (nullFactory ()).build (),
184
182
Component .builder (TestInterface7 .class ).factory (nullFactory ()).build ());
185
183
186
- twice (() -> testSort (components ));
184
+ twice (() -> detect (components ));
187
185
}
188
186
189
187
/*
@@ -194,7 +192,7 @@ public void sort_shouldTopologicallySortComponents4() {
194
192
* |_________|
195
193
*/
196
194
@ Test
197
- public void sort_withDependencyCycle_shouldThrow () {
195
+ public void detect_withDependencyCycle_shouldThrow () {
198
196
List <Component <?>> components =
199
197
Arrays .asList (
200
198
Component .builder (TestInterface1 .class )
@@ -211,7 +209,7 @@ public void sort_withDependencyCycle_shouldThrow() {
211
209
.build ());
212
210
213
211
try {
214
- ComponentSorter . sorted (components );
212
+ CycleDetector . detect (components );
215
213
fail ("Not thrown" );
216
214
} catch (DependencyCycleException ex ) {
217
215
assertThat (ex .getComponentsInCycle ()).containsExactlyElementsIn (components );
@@ -227,7 +225,7 @@ public void sort_withDependencyCycle_shouldThrow() {
227
225
|_Provider_|
228
226
*/
229
227
@ Test
230
- public void sort_withProviderDependencyCycle_shouldSortCorrectly () {
228
+ public void detect_withProviderDependencyCycle_shouldNotThrow () {
231
229
List <Component <?>> components =
232
230
Arrays .asList (
233
231
Component .builder (TestInterface1 .class )
@@ -243,60 +241,37 @@ public void sort_withProviderDependencyCycle_shouldSortCorrectly() {
243
241
.factory (nullFactory ())
244
242
.build ());
245
243
246
- ComponentSorter . sorted (components );
247
- twice (() -> testSort (components ));
244
+ CycleDetector . detect (components );
245
+ twice (() -> detect (components ));
248
246
}
249
247
250
248
@ Test
251
- public void sort_withMultipleComponentsImplementingSameIface_shouldThrow () {
249
+ public void detect_withMultipleComponentsImplementingSameIface_shouldThrow () {
252
250
List <Component <?>> components =
253
251
Arrays .asList (
254
252
Component .builder (TestInterface1 .class ).factory (nullFactory ()).build (),
255
253
Component .builder (TestInterface1 .class ).factory (nullFactory ()).build ());
256
254
257
255
try {
258
- ComponentSorter . sorted (components );
256
+ CycleDetector . detect (components );
259
257
fail ();
260
258
} catch (IllegalArgumentException ex ) {
261
259
// success.
262
260
}
263
261
}
264
262
265
- private static void testSort (List <Component <?>> components ) {
263
+ private static void detect (List <Component <?>> components ) {
266
264
Collections .shuffle (components );
267
- List <Component <?>> sorted = ComponentSorter .sorted (components );
268
-
269
- assertThat (sorted ).hasSize (components .size ());
270
- assertAscendingOrder (sorted );
265
+ try {
266
+ CycleDetector .detect (components );
267
+ } catch (DependencyException ex ) {
268
+ fail (String .format ("Unexpected exception thrown: %s" , ex ));
269
+ }
271
270
}
272
271
273
272
private static void twice (Runnable runnable ) {
274
273
for (int i = 0 ; i < 2 ; i ++) {
275
274
runnable .run ();
276
275
}
277
276
}
278
-
279
- private static void assertAscendingOrder (List <Component <?>> components ) {
280
- Set <Class <?>> seenInterfaces = new HashSet <>();
281
- Set <Class <?>> allInterfaces = new HashSet <>();
282
- for (Component <?> component : components ) {
283
- allInterfaces .addAll (component .getProvidedInterfaces ());
284
- }
285
-
286
- for (Component <?> component : components ) {
287
- for (Dependency dependency : component .getDependencies ()) {
288
- if (!dependency .isDirectInjection ()) {
289
- continue ;
290
- }
291
- Class <?> iface = dependency .getInterface ();
292
- if (allInterfaces .contains (iface ) && !seenInterfaces .contains (iface )) {
293
- fail (
294
- String .format (
295
- "Encountered component before its dependency. Component: %s, Dependency: %s" ,
296
- component , iface ));
297
- }
298
- }
299
- seenInterfaces .addAll (component .getProvidedInterfaces ());
300
- }
301
- }
302
277
}
0 commit comments