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 org.apache.commons.collections.set.ListOrderedSet;
33  
34  import junit.framework.TestCase;
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      {
64          final Set actualOutput = fSearch.search(this.fInput, getCallback());
65          assertEquals(
66                  "Input and output sets do not have the same number of members",
67                  this.fExpectedOutput.size(), actualOutput.size());
68          assertEquals("Sets do not contain the same members",
69                  this.fExpectedOutput, actualOutput);
70      }
71  
72      protected void setInput(final String[] nodes)
73      {
74          for (int i = 0; i < nodes.length; i++)
75          {
76              this.fInput.add(nodes[i]);
77          }
78      }
79  
80      protected void setOutput(final String[] nodes)
81      {
82          for (int i = 0; i < nodes.length; i++)
83          {
84              this.fExpectedOutput.add(nodes[i]);
85          }
86      }
87  
88      protected void addEdges(final String from, final String[] tos)
89      {
90          final Set tmpEdges = new TreeSet();
91          for (int i = 0; i < tos.length; i++)
92          {
93              final 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     {
102         return new ISearchCallback()
103         {
104             public SortedSet getEdges(final Object fromNode)
105             {
106                 return getEdgesFromNode(fromNode);
107             };
108 
109             public void nodeAdded(final Object fromNode)
110             {
111             }
112 
113             public boolean searchNode(final Object node)
114             {
115                 return true;
116             }
117         };
118     }
119 
120     protected SortedSet getEdgesFromNode(final Object fromNode)
121     {
122         return (SortedSet) fEdgesPerNodeMap.get(fromNode);
123     }
124 
125     protected SortedSet getEdgesToNode(final Object toNode)
126     {
127         final TreeSet set = new TreeSet();
128         final Iterator iterator = this.fAllEdgesSet.iterator();
129         while (iterator.hasNext())
130         {
131             final Edge edge = (Edge) iterator.next();
132             if (edge.getTo().equals(toNode))
133             {
134                 set.add(edge);
135             }
136         }
137         return set;
138     };
139 }