1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove.test

File AbstractJdbcTestBase.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
53% of files have more coverage

Code metrics

0
22
11
2
135
108
12
0.55
2
5.5
1.09

Classes

Class Line # Actions
AbstractJdbcTestBase 35 22 0% 12 4
0.878787987.9%
JdbcTestConfig 135 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 54 tests. .

Source view

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)
 
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   
 
44  8 toggle @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   
 
65  8 toggle @AfterAll
66    static void globalTeardown(ApplicationContext context) { }
67   
 
68  51 toggle public DataSource getDataSource()
69    {
70  51 return ds;
71    }
72   
 
73  1 toggle public JdbcTemplate getJdbcTemplate()
74    {
75  1 return jdbcTemplate;
76    }
77   
 
78  61 toggle 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   
 
87  0 toggle public int getNumberOfRows(String tableName)
88    {
89  0 return JdbcTestUtils.countRowsInTable(jdbcTemplate, tableName);
90    }
91   
 
92  54 toggle public int getNumberOfRows(String tableName, String whereClause)
93    {
94  54 return JdbcTestUtils.countRowsInTableWhere(
95    jdbcTemplate, tableName, whereClause
96    );
97    }
98   
 
99  0 toggle public IntegerAssert assertNumberOfRows(String tableName)
100    {
101  0 return new IntegerAssert(getNumberOfRows(tableName));
102    }
103   
 
104  54 toggle public IntegerAssert assertNumberOfRows(String tableName, String whereClause)
105    {
106  54 return new IntegerAssert(getNumberOfRows(tableName, whereClause));
107    }
108   
 
109  8 toggle 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   
 
118  16 toggle 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
 
135    class JdbcTestConfig {}