1 /* TestA2O.java */
2
3 package org.quilt.graph;
4
5 import junit.framework.*;
6
7 public class TestA2O extends TestCase {
8
9 Directed graph = null;
10 Entry entry = null;
11 Exit exit = null;
12 Directed subgraph = null;
13 Vertex A, C, D, E, F, G, H, I, J, K, L, M, N, O;
14 Entry Try = null;
15 Exit subExit = null;
16
17 public TestA2O(String name) {
18 super(name);
19 }
20
21 public class Hansel implements Visitor {
22 private StringBuffer crumbs = new StringBuffer();
23 public Hansel () {}
24 public void discoverGraph (Directed g) {}
25 public void finishGraph (Directed g) {}
26 public void discoverVertex (Vertex v) {
27 String d = v.getLabel();
28 if (d != null) {
29 crumbs.append( d );
30 }
31 }
32 public void finishVertex (Vertex v) {}
33 public void discoverEdge (Edge e) {}
34 public void finishEdge (Edge e) {}
35
36 public String getCrumbs () {
37 return crumbs.toString();
38 }
39 }
40 public void setUp() {
41 graph = new Directed();
42 entry = graph.getEntry();
43 entry.setLabel( "W");
44 exit = graph.getExit();
45 exit.setLabel( "Z");
46
47 Edge e = entry.getEdge();
48
49 A = graph.insertVertex(e);
50 A.setLabel( "A");
51 e = A.getEdge();
52
53 // try with three catch clauses
54 subgraph = graph.subgraph (e, 3);
55 Try = subgraph.getEntry();
56 Try.setLabel( "X");
57
58 Exit Y = subgraph.getExit();
59 Y.setLabel ("Y");
60
61 e = Try.getEdge();
62 // block ending with if
63 F = subgraph.insertVertex(e);
64 F.setLabel( "F");
65 e = F.makeBinary(); // returns 'other' or 'then' edge
66
67 // return block; its target should be exit
68 G = subgraph.insertVertex(e);
69 G.setLabel( "G");
70
71 e = F.getEdge();
72 // else leads to another if
73 L = subgraph.insertVertex(e);
74 L.setLabel( "L");
75 e = L.getEdge(); // else edge
76 // else leads to a throw; M's edge should go to exit
77 M = subgraph.insertVertex(e);
78 M.setLabel( "M");
79
80 e = L.makeBinary(); // then edge
81 H = subgraph.insertVertex(e);
82 H.setLabel( "H");
83 H.makeMulti(3);
84 // all of H's children should have exit as target
85 e = ((MultiConnector)H.getConnector()).getEdge(0);
86 I = subgraph.insertVertex(e);
87 I.setLabel( "I");
88 e = ((MultiConnector)H.getConnector()).getEdge(1);
89 J = subgraph.insertVertex(e);
90 J.setLabel( "J");
91 e = ((MultiConnector)H.getConnector()).getEdge(2);
92 K = subgraph.insertVertex(e);
93 K.setLabel( "K");
94
95 // catch blocks; these should also all have exit as target
96 e = ((ComplexConnector) Try.getConnector()).getEdge(0);
97 C = subgraph.insertVertex(e);
98 C.setLabel( "C");
99 e = ((ComplexConnector) Try.getConnector()).getEdge(1);
100 D = subgraph.insertVertex(e);
101 D.setLabel( "D");
102 e = ((ComplexConnector) Try.getConnector()).getEdge(2);
103 E = subgraph.insertVertex(e);
104 E.setLabel( "E");
105
106 subExit = subgraph.getExit();
107 e = subExit.getEdge();
108
109 // vertices in the main graph
110 N = graph.insertVertex(e);
111 N.setLabel( "N");
112 e = N.getEdge();
113 O = graph.insertVertex(e);
114 O.setLabel( "O");
115 }
116
117 public void testRecheck() {
118 assertNotNull("graph is null", graph);
119 assertNotNull("subgraph is null", subgraph);
120
121 // C D E F G H I J K L M entry exit, total of 13
122 assertEquals ("wrong number of vertices in subgraph",
123 13, subgraph.size() );
124 // subgraph A N O entry exit = 13 + 5 = 18
125 assertEquals ("wrong number of vertices in graph",
126 18, graph.size() );
127 assertEquals ("Vertex O's target is not exit",
128 exit, O.getTarget() );
129 assertEquals ("subgraph's exit doesn't point to Vertex N",
130 N, subExit.getTarget() );
131 // catch blocks
132 assertEquals ("Vertex C's target is not subgraph exit",
133 subExit, C.getTarget() );
134 assertEquals ("Vertex D's target is not subgraph exit",
135 subExit, D.getTarget() );
136 assertEquals ("Vertex E's target is not subgraph exit",
137 subExit, E.getTarget() );
138
139 // return blocks; arguably these should point to top-level exit
140 assertEquals ("Vertex G's target is not subgraph exit",
141 subExit, G.getTarget() );
142
143 // select targets
144 assertEquals ("Vertex I's target is not subgraph exit",
145 subExit, I.getTarget() );
146 assertEquals ("Vertex J's target is not subgraph exit",
147 subExit, J.getTarget() );
148 assertEquals ("Vertex K's target is not subgraph exit",
149 subExit, K.getTarget() );
150 // throw block
151 assertEquals ("Vertex M's target is not subgraph exit",
152 subExit, M.getTarget() );
153
154 }
155 public void testHanselAndGretel() {
156 Walker gretel = new Walker();
157 Hansel hansel = new Hansel();
158 gretel.visit(subgraph, hansel);
159 String crumbs = hansel.getCrumbs();
160 assertEquals ("unexpected path taken through subgraph",
161 "XFLMYHIJKGCDE", crumbs);
162
163 gretel = new Walker();
164 hansel = new Hansel();
165 gretel.visit(graph, hansel);
166 crumbs = hansel.getCrumbs();
167 assertEquals ("unexpected path taken through graph",
168 "WAXFLMYHIJKGCDENOZ", crumbs);
169 }
170 }
171
This page was automatically generated by Maven