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;
22  
23  import junit.framework.TestCase;
24  
25  import org.dbunit.database.IDatabaseConnection;
26  import org.dbunit.dataset.IDataSet;
27  import org.dbunit.dataset.ITable;
28  import org.dbunit.dataset.SortedTable;
29  import org.dbunit.operation.DatabaseOperation;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * @author Andres Almiray (aalmiray@users.sourceforge.net)
35   * @author Last changed by: $Author$
36   * @version $Revision$ $Date$
37   * @since 2.2.0
38   */
39  public abstract class AbstractDatabaseTesterIT extends TestCase
40  {
41     protected IDatabaseConnection _connection;
42     protected IDatabaseTester _databaseTester;
43  
44     protected final Logger logger = LoggerFactory.getLogger(AbstractDatabaseTesterIT.class);
45  
46     public AbstractDatabaseTesterIT( String s )
47     {
48        super( s );
49     }
50  
51      protected DatabaseEnvironment getEnvironment() throws Exception
52     {
53        return DatabaseEnvironment.getInstance();
54     }
55  
56     protected ITable createOrderedTable( String tableName, String orderByColumn ) throws Exception
57     {
58        return new SortedTable( _connection.createDataSet()
59              .getTable( tableName ), new String[] { orderByColumn } );
60     }
61  
62     // //////////////////////////////////////////////////////////////////////////
63     // TestCase class
64  
65     protected void setUp() throws Exception
66     {
67        super.setUp();
68  
69        assertNotNull( "DatabaseTester is not set", getDatabaseTester() );
70        getDatabaseTester().setSetUpOperation( getSetUpOperation() );
71        getDatabaseTester().setDataSet( getDataSet() );
72        getDatabaseTester().onSetup();
73  
74        _connection = getDatabaseTester().getConnection();
75     }
76  
77     protected void tearDown() throws Exception
78     {
79        super.tearDown();
80  
81        assertNotNull( "DatabaseTester is not set", getDatabaseTester() );
82        getDatabaseTester().setTearDownOperation( getTearDownOperation() );
83        getDatabaseTester().setDataSet( getDataSet() );
84        getDatabaseTester().onTearDown();
85  
86        DatabaseOperation.DELETE_ALL.execute( _connection, _connection.createDataSet() );
87  
88        _connection = null;
89     }
90  
91     // //////////////////////////////////////////////////////////////////////////
92  
93     protected IDataSet getDataSet() throws Exception
94     {
95        return getEnvironment().getInitDataSet();
96     }
97  
98     protected DatabaseOperation getSetUpOperation()
99     {
100       return DatabaseOperation.CLEAN_INSERT;
101    }
102 
103    protected DatabaseOperation getTearDownOperation()
104    {
105       return DatabaseOperation.NONE;
106    }
107 
108    protected abstract IDatabaseTester getDatabaseTester() throws Exception;
109 
110    /**
111     * This method is used so sub-classes can disable the tests according to some
112     * characteristics of the environment
113     * 
114     * @param testName name of the test to be checked
115     * @return flag indicating if the test should be executed or not
116     */
117    protected boolean runTest( String testName )
118    {
119       return true;
120    }
121 
122    protected void runTest() throws Throwable
123    {
124       if( runTest( getName() ) ){
125          super.runTest();
126       }else{
127          if( logger.isDebugEnabled() ){
128             logger.debug( "Skipping test " + getClass().getName() + "." + getName() );
129          }
130       }
131    }
132 
133    public static boolean environmentHasFeature( TestFeature feature )
134    {
135       try{
136          final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
137          final boolean runIt = environment.support( feature );
138          return runIt;
139       }
140       catch( Exception e ){
141          throw new DatabaseUnitRuntimeException( e );
142       }
143    }
144 }