File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\util\CyclicGraphDetectorTest.java.html

1 package hudson.util;
2
3 import hudson.util.CyclicGraphDetector.CycleDetectedException;
4 import junit.framework.TestCase;
5
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.LinkedHashSet;
9 import java.util.Set;
10
11 /**
12 * @author Kohsuke Kawaguchi
13 */
14 public class CyclicGraphDetectorTest extends TestCase {
15 private class Edge {
16 String src,dst;
17
18 private Edge(String src, String dst) {
19 this.src = src;
20 this.dst = dst;
21 }
22 }
23
24 private class Graph extends ArrayList<Edge> {
25 Graph e(String src, String dst) {
26 add(new Edge(src,dst));
27 return this;
28 }
29
30 Set<String> nodes() {
31 Set<String> nodes = new LinkedHashSet<String>();
32 for (Edge e : this) {
33 nodes.add(e.src);
34 nodes.add(e.dst);
35 }
36 return nodes;
37 }
38
39 Set<String> edges(String from) {
40 Set<String> edges = new LinkedHashSet<String>();
41 for (Edge e : this) {
42 if (e.src.equals(from))
43 edges.add(e.dst);
44 }
45 return edges;
46 }
47
48 /**
49 * Performs a cycle check.
50 */
51 void check() throws Exception {
52 new CyclicGraphDetector<String>() {
53 protected Set<String> getEdges(String s) {
54 return edges(s);
55 }
56 }.run(nodes());
57 }
58
59 void mustContainCycle(String... members) throws Exception {
60 try {
61 check();
62 fail("Cycle expected");
63 } catch (CycleDetectedException e) {
64 String msg = "Expected cycle of " + Arrays.asList(members) + " but found " + e.cycle;
65 for (String s : members)
66 assertTrue(msg, e.cycle.contains(s));
67 }
68 }
69 }
70
71 public void testCycle() throws Exception {
72 new Graph().e("A","B").e("B","C").e("C","A").mustContainCycle("A","B","C");
73 }
74
75 public void testCycle2() throws Exception {
76 new Graph().e("A","B").e("B","C").e("C","C").mustContainCycle("C");
77 }
78
79 public void testCycle3() throws Exception {
80 new Graph().e("A","B").e("B","C").e("C","D").e("B","E").e("E","D").e("E","A").mustContainCycle("A","B","E");
81 }
82
83 }
84