1 package org.codehaus.plexus.util.dag;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.List;
20
21 import org.junit.jupiter.api.Test;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.junit.jupiter.api.Assertions.fail;
29
30
31
32
33
34
35
36 class CycleDetectorTest {
37
38 @Test
39 void cycyleDetection() {
40
41
42
43
44 assertDoesNotThrow(
45 () -> {
46 final DAG dag1 = new DAG();
47
48 dag1.addEdge("a", "b");
49
50 dag1.addEdge("b", "c");
51 },
52 "Cycle should not be detected");
53
54
55
56
57
58
59
60 try {
61 final DAG dag2 = new DAG();
62
63 dag2.addEdge("a", "b");
64
65 dag2.addEdge("b", "c");
66
67 dag2.addEdge("c", "a");
68
69 fail("Cycle should be detected");
70
71 } catch (CycleDetectedException e) {
72
73 final List<String> cycle = e.getCycle();
74
75 assertNotNull(cycle, "Cycle should be not null");
76
77 assertTrue(cycle.contains("a"), "Cycle contains 'a'");
78
79 assertTrue(cycle.contains("b"), "Cycle contains 'b'");
80
81 assertTrue(cycle.contains("c"), "Cycle contains 'c'");
82 }
83
84
85
86
87
88 assertDoesNotThrow(
89 () -> {
90 final DAG dag3 = new DAG();
91
92 dag3.addEdge("a", "b");
93
94 dag3.addEdge("b", "c");
95
96 dag3.addEdge("b", "d");
97
98 dag3.addEdge("a", "d");
99 },
100 "Cycle should not be detected");
101
102
103
104
105
106
107
108 try {
109 final DAG dag4 = new DAG();
110
111 dag4.addEdge("a", "b");
112
113 dag4.addEdge("b", "c");
114
115 dag4.addEdge("b", "d");
116
117 dag4.addEdge("a", "d");
118
119 dag4.addEdge("c", "a");
120
121 fail("Cycle should be detected");
122
123 } catch (CycleDetectedException e) {
124 final List<String> cycle = e.getCycle();
125
126 assertNotNull(cycle, "Cycle should be not null");
127
128 assertEquals("a", cycle.get(0), "Cycle contains 'a'");
129
130 assertEquals("b", cycle.get(1), "Cycle contains 'b'");
131
132 assertEquals("c", cycle.get(2), "Cycle contains 'c'");
133
134 assertEquals("a", cycle.get(3), "Cycle contains 'a'");
135 }
136
137
138
139
140
141
142
143
144
145 final DAG dag5 = new DAG();
146
147 try {
148
149 dag5.addEdge("a", "b");
150
151 dag5.addEdge("b", "c");
152
153 dag5.addEdge("b", "f");
154
155 dag5.addEdge("f", "g");
156
157 dag5.addEdge("g", "h");
158
159 dag5.addEdge("c", "d");
160
161 dag5.addEdge("d", "e");
162
163 dag5.addEdge("e", "b");
164
165 fail("Cycle should be detected");
166
167 } catch (CycleDetectedException e) {
168 final List<String> cycle = e.getCycle();
169
170 assertNotNull(cycle, "Cycle should be not null");
171
172 assertEquals(5, cycle.size(), "Cycle contains 5 elements");
173
174 assertEquals("b", cycle.get(0), "Cycle contains 'b'");
175
176 assertEquals("c", cycle.get(1), "Cycle contains 'c'");
177
178 assertEquals("d", cycle.get(2), "Cycle contains 'd'");
179
180 assertEquals("e", cycle.get(3), "Cycle contains 'e'");
181
182 assertEquals("b", cycle.get(4), "Cycle contains 'b'");
183
184 assertTrue(dag5.hasEdge("a", "b"), "Edge exists");
185
186 assertTrue(dag5.hasEdge("b", "c"), "Edge exists");
187
188 assertTrue(dag5.hasEdge("b", "f"), "Edge exists");
189
190 assertTrue(dag5.hasEdge("f", "g"), "Edge exists");
191
192 assertTrue(dag5.hasEdge("g", "h"), "Edge exists");
193
194 assertTrue(dag5.hasEdge("c", "d"), "Edge exists");
195
196 assertTrue(dag5.hasEdge("d", "e"), "Edge exists");
197
198 assertFalse(dag5.hasEdge("e", "b"));
199 }
200 }
201 }