View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, 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  package org.dbunit.database.search;
22  
23  import java.io.File;
24  import java.sql.Connection;
25  import java.util.Set;
26  
27  import junit.framework.TestCase;
28  import junitx.framework.ArrayAssert;
29  
30  import org.dbunit.database.DatabaseConnection;
31  import org.dbunit.database.IDatabaseConnection;
32  
33  import org.dbunit.HypersonicEnvironment;
34  import org.dbunit.testutil.TestUtils;
35  import org.dbunit.util.CollectionsHelper;
36  import org.dbunit.util.search.DepthFirstSearch;
37  import org.dbunit.util.search.ISearchCallback;
38  
39  /**  
40   * @author Felipe Leme (dbunit@felipeal.net)
41   * @version $Revision$
42   * @since Aug 28, 2005
43   */
44  public abstract class AbstractMetaDataBasedSearchCallbackTestCase extends TestCase {
45    
46    private final String sqlFile;
47    
48    private Connection jdbcConnection;
49    
50    private IDatabaseConnection connection;
51    
52     public AbstractMetaDataBasedSearchCallbackTestCase(String testName, String sqlFile) {
53       super(testName );
54       this.sqlFile = sqlFile;
55     }   
56     
57     protected void setUp() throws Exception {
58       this.jdbcConnection = HypersonicEnvironment.createJdbcConnection("mem:tempdb");
59       HypersonicEnvironment.executeDdlFile(
60           TestUtils.getFile("sql/" + this.sqlFile),
61           this.jdbcConnection
62       );
63       this.connection = new DatabaseConnection(jdbcConnection);
64     }
65  
66     protected void tearDown() throws Exception {
67       HypersonicEnvironment.shutdown(this.jdbcConnection);
68       this.jdbcConnection.close();
69  //     HypersonicEnvironment.deleteFiles( "tempdb" );
70     }   
71     
72     protected IDatabaseConnection getConnection() {
73       return this.connection;
74     }
75     
76    protected abstract String[][] getInput(); 
77    
78    protected abstract String[][] getExpectedOutput(); 
79    
80    protected abstract AbstractMetaDataBasedSearchCallback getCallback(IDatabaseConnection connection2);
81  
82    public void testAllInput() throws Exception {
83       IDatabaseConnection connection = getConnection();
84       
85       String[][] allInput = getInput();
86       String[][] allExpectedOutput = getExpectedOutput();
87       ISearchCallback callback = getCallback(connection);
88       for (int i = 0; i < allInput.length; i++) {
89         String[] input = allInput[i];
90         String[] expectedOutput = allExpectedOutput[i];
91         DepthFirstSearch search = new DepthFirstSearch();
92         Set result = search.search( input, callback );
93         String[] actualOutput = CollectionsHelper.setToStrings( result ); 
94         ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutput );
95       }           
96    }
97  
98    
99    
100 }