1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
37
38
39
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
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 }