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