| 1 | package com.reallifedeveloper.tools.test.database.dbunit; | |
| 2 | ||
| 3 | import java.io.IOException; | |
| 4 | import java.io.InputStream; | |
| 5 | import java.util.ArrayList; | |
| 6 | import java.util.List; | |
| 7 | import java.util.Optional; | |
| 8 | ||
| 9 | import javax.sql.DataSource; | |
| 10 | ||
| 11 | import org.checkerframework.checker.nullness.qual.Nullable; | |
| 12 | import org.dbunit.DataSourceDatabaseTester; | |
| 13 | import org.dbunit.IDatabaseTester; | |
| 14 | import org.dbunit.database.DatabaseConfig; | |
| 15 | import org.dbunit.database.IDatabaseConnection; | |
| 16 | import org.dbunit.dataset.CompositeDataSet; | |
| 17 | import org.dbunit.dataset.DataSetException; | |
| 18 | import org.dbunit.dataset.IDataSet; | |
| 19 | import org.dbunit.dataset.datatype.IDataTypeFactory; | |
| 20 | import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; | |
| 21 | import org.dbunit.operation.DatabaseOperation; | |
| 22 | ||
| 23 | /** | |
| 24 | * A helper class used by {@link AbstractDbTest}. It could be used directly by a test class that for some reason cannot inherit from | |
| 25 | * {@code AbstractDbTest}. | |
| 26 | * | |
| 27 | * @author RealLifeDeveloper | |
| 28 | * | |
| 29 | */ | |
| 30 | public final class DbTestHelper { | |
| 31 | ||
| 32 | private final IDatabaseTester databaseTester; | |
| 33 | ||
| 34 | /** | |
| 35 | * Creates a new {@code DbTestHelper}, with test data provided by the given {@code dataSet} and using the given | |
| 36 | * {@code dataSource} to insert it. The database schema name may be provided (can be {@code null}), and the type of database | |
| 37 | * can be defined using {@code dataTypeFactory} (can also be {@code null}). | |
| 38 | * | |
| 39 | * @param dataSource the {@code DataSource} to use when inserting test data | |
| 40 | * @param dataSet the DbUnit test data set to read | |
| 41 | * @param schemaName optional name of the database schema | |
| 42 | * @param dataTypeFactory an optional {@code IDataTypeFactory} | |
| 43 | */ | |
| 44 | public DbTestHelper(DataSource dataSource, IDataSet dataSet, Optional<String> schemaName, Optional<IDataTypeFactory> dataTypeFactory) { | |
| 45 |
3
1. <init> : negated conditional → RUN_ERROR 2. <init> : negated conditional → KILLED 3. <init> : negated conditional → KILLED |
if (dataSource == null || dataSet == null || dataTypeFactory == null) { |
| 46 | throw new IllegalArgumentException("Arguments must not be null: dataSource=" + dataSource + ", dataSet=" + dataSet | |
| 47 | + ", schemaName=" + schemaName + ", dataTypeFactory=" + dataTypeFactory); | |
| 48 | } | |
| 49 | databaseTester = new DataSourceDatabaseTester(dataSource, schemaName.orElse(null)) { | |
| 50 | // We override this method to configure the dataTypeFactory for the connection, to avoid warnings from DbUnit. | |
| 51 | @Override | |
| 52 | public IDatabaseConnection getConnection() throws Exception { | |
| 53 | IDatabaseConnection conn = super.getConnection(); | |
| 54 |
1
1. getConnection : removed call to java/util/Optional::ifPresent → KILLED |
dataTypeFactory.ifPresent(dtf -> { |
| 55 |
1
1. lambda$getConnection$0 : removed call to org/dbunit/database/DatabaseConfig::setProperty → SURVIVED |
conn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dtf); |
| 56 |
1
1. lambda$getConnection$0 : removed call to org/dbunit/database/DatabaseConfig::setProperty → KILLED |
conn.getConfig().setProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, true); |
| 57 | }); | |
| 58 |
1
1. getConnection : replaced return value with null for com/reallifedeveloper/tools/test/database/dbunit/DbTestHelper$1::getConnection → KILLED |
return conn; |
| 59 | } | |
| 60 | }; | |
| 61 |
1
1. <init> : removed call to org/dbunit/IDatabaseTester::setDataSet → KILLED |
databaseTester.setDataSet(dataSet); |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * Reads XML data set files from the classpath resources pointed to by {@code dataSetResourceNames}, optionally validating using | |
| 66 | * the DTD pointed to by {@code dataSetDtdResourceName} property. | |
| 67 | * <p> | |
| 68 | * The {@code dataSetDtdResourceName} parameter may be {@code null}, in which case no validation is performed. | |
| 69 | * | |
| 70 | * @param dataSetDtdResourceName the name of the resource containing the DTD for test data files, or {@code null} | |
| 71 | * @param dataSetResourceNames the names of the resources containing test data | |
| 72 | * | |
| 73 | * @return the test data set | |
| 74 | * | |
| 75 | * @throws DataSetException if some resource if malformed | |
| 76 | * @throws IOException if reading a resource failed | |
| 77 | */ | |
| 78 | public static IDataSet readDataSetFromClasspath(@Nullable String dataSetDtdResourceName, String... dataSetResourceNames) | |
| 79 | throws DataSetException, IOException { | |
| 80 |
2
1. readDataSetFromClasspath : negated conditional → KILLED 2. readDataSetFromClasspath : negated conditional → KILLED |
if (dataSetResourceNames == null || dataSetResourceNames.length == 0) { |
| 81 | throw new IllegalArgumentException("You must provide at least one dataSetResourceName"); | |
| 82 | } else { | |
| 83 | FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder(); | |
| 84 | builder.setColumnSensing(true); | |
| 85 |
1
1. readDataSetFromClasspath : removed call to com/reallifedeveloper/tools/test/database/dbunit/DbTestHelper::addPotentialDtd → KILLED |
addPotentialDtd(builder, Optional.ofNullable(dataSetDtdResourceName)); |
| 86 | List<IDataSet> dataSets = readDataSets(builder, dataSetResourceNames); | |
| 87 |
1
1. readDataSetFromClasspath : replaced return value with null for com/reallifedeveloper/tools/test/database/dbunit/DbTestHelper::readDataSetFromClasspath → KILLED |
return new CompositeDataSet(dataSets.toArray(new IDataSet[0])); |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | private static void addPotentialDtd(FlatXmlDataSetBuilder builder, Optional<String> dataSetDtdResourceName) | |
| 92 | throws DataSetException, IOException { | |
| 93 |
1
1. addPotentialDtd : negated conditional → KILLED |
if (dataSetDtdResourceName.isPresent()) { |
| 94 | try (InputStream is = DbTestHelper.class.getResourceAsStream(dataSetDtdResourceName.get())) { | |
| 95 |
1
1. addPotentialDtd : negated conditional → KILLED |
if (is == null) { |
| 96 | throw new IllegalArgumentException("DTD not found on classpath: " + dataSetDtdResourceName.get()); | |
| 97 | } | |
| 98 | builder.setMetaDataSetFromDtd(is); | |
| 99 | } | |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | private static List<IDataSet> readDataSets(FlatXmlDataSetBuilder builder, String... dataSetResourceNames) | |
| 104 | throws DataSetException, IOException { | |
| 105 | List<IDataSet> dataSets = new ArrayList<>(); | |
| 106 | for (String dataSetResourceName : dataSetResourceNames) { | |
| 107 | try (InputStream is = DbTestHelper.class.getResourceAsStream(dataSetResourceName)) { | |
| 108 |
1
1. readDataSets : negated conditional → KILLED |
if (is == null) { |
| 109 | throw new IllegalArgumentException("Dataset not found on classpath: " + dataSetResourceName); | |
| 110 | } | |
| 111 | dataSets.add(builder.build(is)); | |
| 112 | } | |
| 113 | } | |
| 114 |
1
1. readDataSets : replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/dbunit/DbTestHelper::readDataSets → KILLED |
return dataSets; |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Initializes the test data before each test case. | |
| 119 | * | |
| 120 | * @throws Exception if something goes wrong | |
| 121 | */ | |
| 122 | @SuppressWarnings("PMD.SignatureDeclareThrowsException") | |
| 123 | public void init() throws Exception { | |
| 124 |
1
1. init : removed call to org/dbunit/IDatabaseTester::onSetup → KILLED |
databaseTester.onSetup(); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Cleans the database from test data after each test case. | |
| 129 | * | |
| 130 | * @throws Exception if something goes wrong | |
| 131 | */ | |
| 132 | @SuppressWarnings("PMD.SignatureDeclareThrowsException") | |
| 133 | public void clean() throws Exception { | |
| 134 |
1
1. clean : removed call to org/dbunit/IDatabaseTester::onTearDown → KILLED |
databaseTester.onTearDown(); |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * Change the operation performed before executing each test. | |
| 139 | * <p> | |
| 140 | * The default setup operation is {@code DatabaseOperation.CLEAN_INSERT}. | |
| 141 | * | |
| 142 | * @param setUpOperation the new setup operation to use | |
| 143 | */ | |
| 144 | public void setSetUpOperation(DatabaseOperation setUpOperation) { | |
| 145 |
1
1. setSetUpOperation : removed call to org/dbunit/IDatabaseTester::setSetUpOperation → SURVIVED |
databaseTester.setSetUpOperation(setUpOperation); |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Change the operation performed after executing each test. | |
| 150 | * <p> | |
| 151 | * The default is to perform no cleanup after the test. | |
| 152 | * | |
| 153 | * @param tearDownOperation the new teardown operation to use | |
| 154 | */ | |
| 155 | public void setTearDownOperation(DatabaseOperation tearDownOperation) { | |
| 156 |
1
1. setTearDownOperation : removed call to org/dbunit/IDatabaseTester::setTearDownOperation → KILLED |
databaseTester.setTearDownOperation(tearDownOperation); |
| 157 | } | |
| 158 | } | |
Mutations | ||
| 45 |
1.1 2.2 3.3 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 58 |
1.1 |
|
| 61 |
1.1 |
|
| 80 |
1.1 2.2 |
|
| 85 |
1.1 |
|
| 87 |
1.1 |
|
| 93 |
1.1 |
|
| 95 |
1.1 |
|
| 108 |
1.1 |
|
| 114 |
1.1 |
|
| 124 |
1.1 |
|
| 134 |
1.1 |
|
| 145 |
1.1 |
|
| 156 |
1.1 |