| 1 |
|
package guru.mikelue.foxglove.test; |
| 2 |
|
|
| 3 |
|
import java.io.IOException; |
| 4 |
|
|
| 5 |
|
import javax.sql.DataSource; |
| 6 |
|
|
| 7 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 8 |
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 9 |
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; |
| 10 |
|
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; |
| 11 |
|
import org.springframework.context.ApplicationContext; |
| 12 |
|
import org.springframework.context.annotation.Configuration; |
| 13 |
|
import org.springframework.jdbc.core.JdbcTemplate; |
| 14 |
|
import org.springframework.test.context.ContextConfiguration; |
| 15 |
|
import org.springframework.test.jdbc.JdbcTestUtils; |
| 16 |
|
import org.springframework.transaction.annotation.Propagation; |
| 17 |
|
import org.springframework.transaction.annotation.Transactional; |
| 18 |
|
import org.springframework.util.StreamUtils; |
| 19 |
|
|
| 20 |
|
import org.assertj.core.api.IntegerAssert; |
| 21 |
|
import org.junit.jupiter.api.AfterAll; |
| 22 |
|
import org.junit.jupiter.api.BeforeAll; |
| 23 |
|
import org.slf4j.Logger; |
| 24 |
|
import org.slf4j.LoggerFactory; |
| 25 |
|
|
| 26 |
|
import guru.mikelue.misc.testlib.AbstractTestBase; |
| 27 |
|
|
| 28 |
|
import static guru.mikelue.foxglove.test.SampleSchema.*; |
| 29 |
|
import static java.nio.charset.StandardCharsets.UTF_8; |
| 30 |
|
|
| 31 |
|
@JdbcTest |
| 32 |
|
@AutoConfigureTestDatabase(replace = Replace.NONE) |
| 33 |
|
@ContextConfiguration(classes = JdbcTestConfig.class) |
| 34 |
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
| |
|
| 87.9% |
Uncovered Elements: 4 (33) |
Complexity: 12 |
Complexity Density: 0.55 |
|
| 35 |
|
public class AbstractJdbcTestBase extends AbstractTestBase { |
| 36 |
|
private final static Logger logger = LoggerFactory.getLogger(AbstractJdbcTestBase.class); |
| 37 |
|
|
| 38 |
|
@Autowired |
| 39 |
|
private JdbcTemplate jdbcTemplate; |
| 40 |
|
|
| 41 |
|
@Autowired |
| 42 |
|
private DataSource ds; |
| 43 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 44 |
8 |
@BeforeAll... |
| 45 |
|
static void globalSetup(ApplicationContext context) throws IOException |
| 46 |
|
{ |
| 47 |
8 |
var jdbcTemplate = context.getBean(JdbcTemplate.class); |
| 48 |
|
|
| 49 |
8 |
var vendor = System.getProperty("database.vendor"); |
| 50 |
|
|
| 51 |
8 |
switch (vendor) { |
| 52 |
8 |
case "org.h2.Driver": |
| 53 |
8 |
dropTables( |
| 54 |
|
jdbcTemplate, |
| 55 |
|
TABLE_DATA_TYPES, TABLE_RENT, TABLE_CAR_FEATURE, TABLE_CAR, TABLE_MEMBER, |
| 56 |
|
TABLE_CAR_ARCHIVED |
| 57 |
|
); |
| 58 |
|
|
| 59 |
8 |
build("classpath:data-types.sql", context); |
| 60 |
8 |
build("classpath:car-renting.sql", context); |
| 61 |
8 |
break; |
| 62 |
|
} |
| 63 |
|
} |
| 64 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 65 |
8 |
@AfterAll... |
| 66 |
|
static void globalTeardown(ApplicationContext context) { } |
| 67 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 68 |
51 |
public DataSource getDataSource()... |
| 69 |
|
{ |
| 70 |
51 |
return ds; |
| 71 |
|
} |
| 72 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 73 |
1 |
public JdbcTemplate getJdbcTemplate()... |
| 74 |
|
{ |
| 75 |
1 |
return jdbcTemplate; |
| 76 |
|
} |
| 77 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 78 |
61 |
public void deleteAll(String... tableNames)... |
| 79 |
|
{ |
| 80 |
61 |
for (var otherTableName: tableNames) { |
| 81 |
173 |
jdbcTemplate.execute( |
| 82 |
|
String.format("DELETE FROM %s", otherTableName) |
| 83 |
|
); |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 87 |
0 |
public int getNumberOfRows(String tableName)... |
| 88 |
|
{ |
| 89 |
0 |
return JdbcTestUtils.countRowsInTable(jdbcTemplate, tableName); |
| 90 |
|
} |
| 91 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 92 |
54 |
public int getNumberOfRows(String tableName, String whereClause)... |
| 93 |
|
{ |
| 94 |
54 |
return JdbcTestUtils.countRowsInTableWhere( |
| 95 |
|
jdbcTemplate, tableName, whereClause |
| 96 |
|
); |
| 97 |
|
} |
| 98 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 99 |
0 |
public IntegerAssert assertNumberOfRows(String tableName)... |
| 100 |
|
{ |
| 101 |
0 |
return new IntegerAssert(getNumberOfRows(tableName)); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 104 |
54 |
public IntegerAssert assertNumberOfRows(String tableName, String whereClause)... |
| 105 |
|
{ |
| 106 |
54 |
return new IntegerAssert(getNumberOfRows(tableName, whereClause)); |
| 107 |
|
} |
| 108 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 109 |
8 |
protected static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)... |
| 110 |
|
{ |
| 111 |
8 |
for (var tableName: tableNames) { |
| 112 |
48 |
jdbcTemplate.execute( |
| 113 |
|
String.format("DROP TABLE IF EXISTS %s", tableName) |
| 114 |
|
); |
| 115 |
|
} |
| 116 |
|
} |
| 117 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 118 |
16 |
protected static void build(String resourcePath, ApplicationContext appContext) throws IOException... |
| 119 |
|
{ |
| 120 |
16 |
logger.debug("Loading resource: \"{}\"", resourcePath); |
| 121 |
|
|
| 122 |
16 |
var ddl = StreamUtils.copyToString( |
| 123 |
|
appContext.getResource(resourcePath) |
| 124 |
|
.getInputStream(), |
| 125 |
|
UTF_8 |
| 126 |
|
); |
| 127 |
|
|
| 128 |
16 |
var jdbcTemplate = appContext.getBean(JdbcTemplate.class); |
| 129 |
|
|
| 130 |
16 |
jdbcTemplate.execute(ddl); |
| 131 |
|
} |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
@Configuration |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 135 |
|
class JdbcTestConfig {} |