1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.util.search;
23
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31
32 import junit.framework.TestCase;
33
34 import org.apache.commons.collections.set.ListOrderedSet;
35
36
37
38
39
40
41
42 public abstract class AbstractSearchTestCase extends TestCase {
43
44 protected static final String A = "A";
45 protected static final String B = "B";
46 protected static final String C = "C";
47 protected static final String D = "D";
48 protected static final String E = "E";
49 protected static final String F = "F";
50
51
52 protected final Map fEdgesPerNodeMap = new HashMap();
53
54 protected final Set fAllEdgesSet = new HashSet();
55
56 protected final Set fExpectedOutput = new ListOrderedSet();
57
58 protected final Set fInput = new HashSet();
59
60 protected final DepthFirstSearch fSearch = new DepthFirstSearch();
61
62 protected void doIt() throws Exception {
63 Set actualOutput = fSearch.search(this.fInput, getCallback());
64 assertEquals("sizes does not match", this.fExpectedOutput.size(),
65 actualOutput.size());
66 Iterator iterExpected = this.fExpectedOutput.iterator();
67 Iterator iterActual = actualOutput.iterator();
68 int i = 0;
69 while (iterExpected.hasNext()) {
70 Object expectedNode = iterExpected.next();
71 Object actualNode = iterActual.next();
72 assertEquals("mismatched element at position " + i, expectedNode,
73 actualNode);
74 i++;
75 }
76 }
77
78 protected void setInput(String[] nodes) {
79 for (int i = 0; i < nodes.length; i++) {
80 this.fInput.add(nodes[i]);
81 }
82 }
83
84 protected void setOutput(String[] nodes) {
85 for (int i = 0; i < nodes.length; i++) {
86 this.fExpectedOutput.add(nodes[i]);
87 }
88 }
89
90 protected void addEdges(String from, String[] tos) {
91 Set tmpEdges = new TreeSet();
92 for (int i = 0; i < tos.length; i++) {
93 Edge edge = new Edge(from, tos[i]);
94 this.fAllEdgesSet.add( edge );
95 tmpEdges.add(edge);
96 }
97 this.fEdgesPerNodeMap.put(from, tmpEdges);
98 }
99
100 protected ISearchCallback getCallback() {
101 return new ISearchCallback() {
102 public SortedSet getEdges(Object fromNode) {
103 return getEdgesFromNode(fromNode);
104 };
105
106 public void nodeAdded(Object fromNode) {
107 }
108
109 public boolean searchNode(Object node) {
110 return true;
111 }
112 };
113 }
114
115 protected SortedSet getEdgesFromNode(Object fromNode) {
116 return (SortedSet) fEdgesPerNodeMap.get(fromNode);
117 }
118
119 protected SortedSet getEdgesToNode(Object toNode) {
120 TreeSet set = new TreeSet();
121 Iterator iterator = this.fAllEdgesSet.iterator();
122 while ( iterator.hasNext() ) {
123 Edge edge = (Edge) iterator.next();
124 if ( edge.getTo().equals(toNode)) {
125 set.add( edge );
126 }
127 }
128 return set;
129 };
130
131 }