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.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   * @author Felipe Leme (dbunit@felipeal.net)
38   * @author Last changed by: $Author$
39   * @version $Revision$ $Date$
40   * @since Aug 25, 2005
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      // fixtures
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 }