001: /* TestA2O.java */
002:
003: package org.quilt.graph;
004:
005: import junit.framework.*;
006:
007: public class TestA2O extends TestCase {
008:
009: Directed graph = null;
010: Entry entry = null;
011: Exit exit = null;
012: Directed subgraph = null;
013: Vertex A, C, D, E, F, G, H, I, J, K, L, M, N, O;
014: Entry Try = null;
015: Exit subExit = null;
016:
017: public TestA2O(String name) {
018: super (name);
019: }
020:
021: public class Hansel implements Visitor {
022: private StringBuffer crumbs = new StringBuffer();
023:
024: public Hansel() {
025: }
026:
027: public void discoverGraph(Directed g) {
028: }
029:
030: public void finishGraph(Directed g) {
031: }
032:
033: public void discoverVertex(Vertex v) {
034: String d = v.getLabel();
035: if (d != null) {
036: crumbs.append(d);
037: }
038: }
039:
040: public void finishVertex(Vertex v) {
041: }
042:
043: public void discoverEdge(Edge e) {
044: }
045:
046: public void finishEdge(Edge e) {
047: }
048:
049: public String getCrumbs() {
050: return crumbs.toString();
051: }
052: }
053:
054: public void setUp() {
055: graph = new Directed();
056: entry = graph.getEntry();
057: entry.setLabel("W");
058: exit = graph.getExit();
059: exit.setLabel("Z");
060:
061: Edge e = entry.getEdge();
062:
063: A = graph.insertVertex(e);
064: A.setLabel("A");
065: e = A.getEdge();
066:
067: // try with three catch clauses
068: subgraph = graph.subgraph(e, 3);
069: Try = subgraph.getEntry();
070: Try.setLabel("X");
071:
072: Exit Y = subgraph.getExit();
073: Y.setLabel("Y");
074:
075: e = Try.getEdge();
076: // block ending with if
077: F = subgraph.insertVertex(e);
078: F.setLabel("F");
079: e = F.makeBinary(); // returns 'other' or 'then' edge
080:
081: // return block; its target should be exit
082: G = subgraph.insertVertex(e);
083: G.setLabel("G");
084:
085: e = F.getEdge();
086: // else leads to another if
087: L = subgraph.insertVertex(e);
088: L.setLabel("L");
089: e = L.getEdge(); // else edge
090: // else leads to a throw; M's edge should go to exit
091: M = subgraph.insertVertex(e);
092: M.setLabel("M");
093:
094: e = L.makeBinary(); // then edge
095: H = subgraph.insertVertex(e);
096: H.setLabel("H");
097: H.makeMulti(3);
098: // all of H's children should have exit as target
099: e = ((MultiConnector) H.getConnector()).getEdge(0);
100: I = subgraph.insertVertex(e);
101: I.setLabel("I");
102: e = ((MultiConnector) H.getConnector()).getEdge(1);
103: J = subgraph.insertVertex(e);
104: J.setLabel("J");
105: e = ((MultiConnector) H.getConnector()).getEdge(2);
106: K = subgraph.insertVertex(e);
107: K.setLabel("K");
108:
109: // catch blocks; these should also all have exit as target
110: e = ((ComplexConnector) Try.getConnector()).getEdge(0);
111: C = subgraph.insertVertex(e);
112: C.setLabel("C");
113: e = ((ComplexConnector) Try.getConnector()).getEdge(1);
114: D = subgraph.insertVertex(e);
115: D.setLabel("D");
116: e = ((ComplexConnector) Try.getConnector()).getEdge(2);
117: E = subgraph.insertVertex(e);
118: E.setLabel("E");
119:
120: subExit = subgraph.getExit();
121: e = subExit.getEdge();
122:
123: // vertices in the main graph
124: N = graph.insertVertex(e);
125: N.setLabel("N");
126: e = N.getEdge();
127: O = graph.insertVertex(e);
128: O.setLabel("O");
129: }
130:
131: public void testRecheck() {
132: assertNotNull("graph is null", graph);
133: assertNotNull("subgraph is null", subgraph);
134:
135: // C D E F G H I J K L M entry exit, total of 13
136: assertEquals("wrong number of vertices in subgraph", 13,
137: subgraph.size());
138: // subgraph A N O entry exit = 13 + 5 = 18
139: assertEquals("wrong number of vertices in graph", 18, graph
140: .size());
141: assertEquals("Vertex O's target is not exit", exit, O
142: .getTarget());
143: assertEquals("subgraph's exit doesn't point to Vertex N", N,
144: subExit.getTarget());
145: // catch blocks
146: assertEquals("Vertex C's target is not subgraph exit", subExit,
147: C.getTarget());
148: assertEquals("Vertex D's target is not subgraph exit", subExit,
149: D.getTarget());
150: assertEquals("Vertex E's target is not subgraph exit", subExit,
151: E.getTarget());
152:
153: // return blocks; arguably these should point to top-level exit
154: assertEquals("Vertex G's target is not subgraph exit", subExit,
155: G.getTarget());
156:
157: // select targets
158: assertEquals("Vertex I's target is not subgraph exit", subExit,
159: I.getTarget());
160: assertEquals("Vertex J's target is not subgraph exit", subExit,
161: J.getTarget());
162: assertEquals("Vertex K's target is not subgraph exit", subExit,
163: K.getTarget());
164: // throw block
165: assertEquals("Vertex M's target is not subgraph exit", subExit,
166: M.getTarget());
167:
168: }
169:
170: public void testHanselAndGretel() {
171: Walker gretel = new Walker();
172: Hansel hansel = new Hansel();
173: gretel.visit(subgraph, hansel);
174: String crumbs = hansel.getCrumbs();
175: assertEquals("unexpected path taken through subgraph",
176: "XFLMYHIJKGCDE", crumbs);
177:
178: gretel = new Walker();
179: hansel = new Hansel();
180: gretel.visit(graph, hansel);
181: crumbs = hansel.getCrumbs();
182: assertEquals("unexpected path taken through graph",
183: "WAXFLMYHIJKGCDENOZ", crumbs);
184: }
185: }
|