View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.util.search;
23  
24  import java.util.SortedSet;
25  
26  
27  /**
28   * @author Felipe Leme (dbunit@felipeal.net)
29   * @version $Revision$
30   * @since Aug 25, 2005
31   */
32  
33  public class BiDirectionalEdgesDepthFirstSearchTest extends DepthFirstSearchTest {
34    
35    protected ISearchCallback getCallback() {
36      return new ISearchCallback() {
37        public SortedSet getEdges(Object fromNode) {
38          SortedSet fromSet = getEdgesFromNode(fromNode);
39          SortedSet toSet = getEdgesToNode(fromNode);
40          if ( fromSet == null ) {
41            return toSet;
42          }
43          if ( toSet != null ) {
44            fromSet.addAll( toSet );
45          }
46          return fromSet;
47        };
48  
49        public void nodeAdded(Object fromNode) {
50        }
51  
52        public boolean searchNode(Object node) {
53          return true;
54        }
55      };
56    };
57    
58    // more tests, now adding nodes or graphs that would not directly reachable 
59    // using unidirectional edges.
60    // For, instance, on the graph C->A->B, C would never be reached from A or B
61    
62    public void testSingleReverseEdge() throws Exception {
63      setInput(new String[] { A });
64      addEdges(A, new String[] { B });
65      addEdges(C, new String[] { A });
66      setOutput(new String[] { B, A, C });
67      doIt();
68    }
69    
70    public void testSingleReverseEdgeInputB() throws Exception {
71      setInput(new String[] { B });
72      addEdges(A, new String[] { B });
73      addEdges(C, new String[] { A });
74      setOutput(new String[] { B, A, C });
75      doIt();
76    }
77  
78    public void testSingleReverseEdgeMultipleInput() throws Exception {
79      setInput(new String[] { B, A });
80      addEdges(A, new String[] { B });
81      addEdges(C, new String[] { A });
82      setOutput(new String[] { B, A, C });
83      doIt();
84    }
85    
86    public void testSingleReverseEdgeMultipleInputIncludingC() throws Exception {
87      setInput(new String[] { C, B, A });
88      addEdges(A, new String[] { B });
89      addEdges(C, new String[] { A });
90      setOutput(new String[] { B, A, C });
91      doIt();
92    }
93    
94    public void testOneInputTwoEdges() throws Exception {
95      setInput(new String[] { B });
96      addEdges(A, new String[] { C });
97      addEdges(B, new String[] { C });
98      setOutput(new String[] { C, B, A });
99      doIt();
100   }
101   
102   // TODO: continue adding more tests uncommenting and adapting tests below...
103   /*  
104 
105   public void testSingleEdgeRepeatedInput() throws Exception {
106     setInput(new String[] { A, B, B, A, B });
107     addEdges(A, new String[] { B });
108     setOutput(new String[] { B, A });
109     doIt();
110   }
111 
112   public void testDisconnected() throws Exception {
113     setInput(new String[] { A, C });
114     addEdges(A, new String[] { B });
115     setOutput(new String[] { B, A, C });
116     doIt();
117   }
118 
119   public void testDisconnectedInverseOrder() throws Exception {
120     setInput(new String[] { C, A });
121     addEdges(A, new String[] { B });
122     setOutput(new String[] { B, A, C });
123     doIt();
124   }
125 
126   public void testMultipleEdgesOneSource() throws Exception {
127     setInput(new String[] { A });
128     addEdges(A, new String[] { B, C });
129     setOutput(new String[] { B, C, A });
130     doIt();
131   }
132 
133   public void testMultipleEdgesMultipleSources() throws Exception {
134     setInput(new String[] { A });
135     addEdges(A, new String[] { B, C });
136     addEdges(B, new String[] { D, C });
137     setOutput(new String[] { C, D, B, A });
138     doIt();
139   }
140 */
141   public void testMultipleEdgesCycleFromA() throws Exception {
142     setInput(new String[] { A });
143     addEdges(A, new String[] { B });
144     addEdges(B, new String[] { C });
145     addEdges(C, new String[] { A });
146     setOutput(new String[] { A, C, B });
147     doIt();
148   }
149   public void testMultipleEdgesCycleFromB() throws Exception {
150     setInput(new String[] { B });
151     addEdges(A, new String[] { B });
152     addEdges(B, new String[] { C });
153     addEdges(C, new String[] { A });
154     setOutput(new String[] { B, A, C });
155     doIt();
156   }
157   public void testMultipleEdgesCycleFromBA() throws Exception {
158     setInput(new String[] { B, A });
159     addEdges(A, new String[] { B });
160     addEdges(B, new String[] { C });
161     addEdges(C, new String[] { A });
162     setOutput(new String[] { A, C, B });
163     doIt();
164   }
165 
166   /*
167   public void testSelfCyclic() throws Exception {
168     setInput(new String[] { A });
169     addEdges(A, new String[] { A });
170     setOutput(new String[] { A });
171     doIt();
172   }
173 
174 */
175   public void testCyclicAndSelfCyclic() throws Exception {
176     setInput(new String[] { A });
177     addEdges(A, new String[] { A, B });
178     addEdges(B, new String[] { C });
179     addEdges(C, new String[] { A });
180     setOutput(new String[] { A, C, B });
181     doIt();
182   }
183 
184   public void testDisconnectedCycles() throws Exception {
185     setInput(new String[] { A, D });
186     addEdges(A, new String[] { B });
187     addEdges(B, new String[] { C });
188     addEdges(C, new String[] { A });
189     addEdges(D, new String[] { E });
190     addEdges(E, new String[] { F });
191     addEdges(F, new String[] { D });
192     setOutput(new String[] { D, F, E, A, C, B });
193     doIt();
194   }
195 
196   public void testConnectedCycle() throws Exception {
197     setInput(new String[] { A });
198     addEdges(A, new String[] { B });
199     addEdges(B, new String[] { C });
200     addEdges(C, new String[] { A, D });
201     addEdges(D, new String[] { E });
202     addEdges(E, new String[] { C });
203     setOutput(new String[] { A, E, D, C, B });
204     doIt();
205   }
206   public void testBigConnectedCycle() throws Exception {
207     setInput(new String[] { A });
208     addEdges(A, new String[] { B });
209     addEdges(B, new String[] { C });
210     addEdges(C, new String[] { A, D });
211     addEdges(D, new String[] { E, B });
212     addEdges(E, new String[] { C });
213     setOutput(new String[] { A, C, B, E, D });
214     doIt();
215   }
216   
217 }