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 static org.junit.jupiter.api.Assertions.assertNotNull;
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.junit.jupiter.api.AfterEach;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * @author Andres Almiray (aalmiray@users.sourceforge.net)
37   * @author Last changed by: $Author$
38   * @version $Revision$ $Date$
39   * @since 2.2.0
40   */
41  public abstract class AbstractDatabaseTesterIT
42  {
43      protected IDatabaseConnection _connection;
44      protected IDatabaseTester _databaseTester;
45  
46      protected final Logger logger =
47              LoggerFactory.getLogger(AbstractDatabaseTesterIT.class);
48      private String testName;
49  
50      protected DatabaseEnvironment getEnvironment() throws Exception
51      {
52          return DatabaseEnvironment.getInstance();
53      }
54  
55      protected ITable createOrderedTable(final String tableName,
56              final String orderByColumn) throws Exception
57      {
58          return new SortedTable(_connection.createDataSet().getTable(tableName),
59                  new String[] {orderByColumn});
60      }
61  
62      protected String getName()
63      {
64          return testName;
65      }
66      // //////////////////////////////////////////////////////////////////////////
67      // TestCase class
68  
69      @BeforeEach
70      protected void setUpConnection() throws Exception
71      {
72          assertNotNull(getDatabaseTester(), "DatabaseTester is not set");
73          getDatabaseTester().setSetUpOperation(getSetUpOperation());
74          getDatabaseTester().setDataSet(getDataSet());
75          getDatabaseTester().onSetup();
76  
77          _connection = getDatabaseTester().getConnection();
78      }
79  
80      @AfterEach
81      protected void tearDown() throws Exception
82      {
83  
84          assertNotNull(getDatabaseTester(), "DatabaseTester is not set");
85          getDatabaseTester().setTearDownOperation(getTearDownOperation());
86          getDatabaseTester().setDataSet(getDataSet());
87          getDatabaseTester().onTearDown();
88  
89          DatabaseOperation.DELETE_ALL.execute(_connection,
90                  _connection.createDataSet());
91  
92          _connection = null;
93      }
94  
95      // //////////////////////////////////////////////////////////////////////////
96  
97      protected IDataSet getDataSet() throws Exception
98      {
99          return getEnvironment().getInitDataSet();
100     }
101 
102     protected DatabaseOperation getSetUpOperation()
103     {
104         return DatabaseOperation.CLEAN_INSERT;
105     }
106 
107     protected DatabaseOperation getTearDownOperation()
108     {
109         return DatabaseOperation.NONE;
110     }
111 
112     protected abstract IDatabaseTester getDatabaseTester() throws Exception;
113 
114     public static boolean environmentHasFeature(final TestFeature feature)
115     {
116         try
117         {
118             final DatabaseEnvironment environment =
119                     DatabaseEnvironment.getInstance();
120             final boolean runIt = environment.support(feature);
121             return runIt;
122         } catch (final Exception e)
123         {
124             throw new DatabaseUnitRuntimeException(e);
125         }
126     }
127 }