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  
22  package org.dbunit;
23  
24  import org.dbunit.database.DatabaseConfig;
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 Manuel Laflamme
35   * @version $Revision$
36   * @since Feb 18, 2002
37   */
38  public abstract class AbstractDatabaseIT extends DatabaseTestCase
39  {
40      protected IDatabaseConnection _connection;
41      
42      protected final Logger logger = LoggerFactory.getLogger(getClass());
43  
44      public AbstractDatabaseIT(String s)
45      {
46          super(s);
47      }
48  
49      protected DatabaseEnvironment getEnvironment() throws Exception
50      {
51          return DatabaseEnvironment.getInstance();
52      }
53  
54      protected ITable createOrderedTable(String tableName, String orderByColumn)
55              throws Exception
56      {
57          return new SortedTable(_connection.createDataSet().getTable(tableName),
58                  new String[]{orderByColumn});
59  //        String sql = "select * from " + tableName + " order by " + orderByColumn;
60  //        return _connection.createQueryTable(tableName, sql);
61      }
62  
63      /**
64       * Returns the string converted as an identifier according to the metadata rules of the database environment.
65       * Most databases convert all metadata identifiers to uppercase.
66       * PostgreSQL converts identifiers to lowercase.
67       * MySQL preserves case.
68       * @param str The identifier.
69       * @return The identifier converted according to database rules.
70       */
71      protected String convertString(String str) throws Exception
72      {
73          return getEnvironment().convertString(str);
74      }
75  
76      ////////////////////////////////////////////////////////////////////////////
77      // TestCase class
78  
79      protected void setUp() throws Exception
80      {
81          super.setUp();
82  
83          _connection = getDatabaseTester().getConnection();
84          setUpDatabaseConfig(_connection.getConfig());
85      }
86  
87      protected IDatabaseTester getDatabaseTester() throws Exception
88      {
89         try{
90            return getEnvironment().getDatabaseTester();
91         }
92         catch( Exception e ){
93             //TODO matthias: this here hides original exceptions from being shown in the JUnit results 
94             //(logger is not configured for unit tests). Think about how exceptions can be passed through
95             // So I temporarily added the "e.printStackTrace()"...
96            logger.error("getDatabaseTester()", e );
97            e.printStackTrace();
98         }
99         return super.getDatabaseTester();
100     }
101 
102     protected void setUpDatabaseConfig(DatabaseConfig config)
103     {
104         try
105         {
106             getEnvironment().setupDatabaseConfig(config);
107         }
108         catch (Exception ex)
109         {
110             throw new RuntimeException(ex); // JH_TODO: is this the "DbUnit way" to handle exceptions?
111         }
112     }
113 
114     protected void tearDown() throws Exception
115     {
116         super.tearDown();
117 
118         DatabaseOperation.DELETE_ALL.execute(_connection, _connection.createDataSet());
119 
120         _connection = null;
121     }
122 
123     ////////////////////////////////////////////////////////////////////////////
124     // DatabaseTestCase class
125 
126     protected IDatabaseConnection getConnection() throws Exception
127     {
128         IDatabaseConnection connection = getEnvironment().getConnection();
129         return connection;
130 
131 //        return new DatabaseEnvironment(getEnvironment().getProfile()).getConnection();
132 //        return new DatabaseConnection(connection.getConnection(), connection.getSchema());
133     }
134 
135     protected IDataSet getDataSet() throws Exception
136     {
137         return getEnvironment().getInitDataSet();
138     }
139 
140     protected void closeConnection(IDatabaseConnection connection) throws Exception
141     {
142 //        getEnvironment().closeConnection();
143     }
144 //
145 //    protected DatabaseOperation getTearDownOperation() throws Exception
146 //    {
147 //        return DatabaseOperation.DELETE_ALL;
148 //    }
149 
150     /**
151      * This method is used so sub-classes can disable the tests according to 
152      * some characteristics of the environment
153      * @param testName name of the test to be checked
154      * @return flag indicating if the test should be executed or not
155      */
156     protected boolean runTest(String testName) {
157       return true;
158     }
159 
160     protected void runTest() throws Throwable {
161       if ( runTest(getName()) ) {
162         super.runTest();
163       } else { 
164         if ( logger.isDebugEnabled() ) {
165           logger.debug( "Skipping test " + getClass().getName() + "." + getName() );
166         }
167       }
168     }
169     
170     public static boolean environmentHasFeature(TestFeature feature) {
171       try {
172         final DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
173         final boolean runIt = environment.support(feature);
174         return runIt;
175       } catch ( Exception e ) {
176         throw new DatabaseUnitRuntimeException(e);
177       }
178     }
179     
180 }
181 
182 
183 
184 
185 
186