| 1 |
|
package guru.mikelue.foxglove.setting; |
| 2 |
|
|
| 3 |
|
import org.assertj.core.api.InstanceOfAssertFactories; |
| 4 |
|
import org.junit.jupiter.api.AfterEach; |
| 5 |
|
import org.junit.jupiter.api.BeforeEach; |
| 6 |
|
import org.junit.jupiter.api.Test; |
| 7 |
|
import org.junit.jupiter.params.ParameterizedTest; |
| 8 |
|
import org.junit.jupiter.params.provider.CsvSource; |
| 9 |
|
|
| 10 |
|
import java.sql.JDBCType; |
| 11 |
|
import java.sql.PreparedStatement; |
| 12 |
|
import java.sql.SQLException; |
| 13 |
|
import java.util.function.Supplier; |
| 14 |
|
|
| 15 |
|
import guru.mikelue.foxglove.ColumnMeta; |
| 16 |
|
import guru.mikelue.foxglove.jdbc.CustomStatementSetter; |
| 17 |
|
import guru.mikelue.misc.testlib.AbstractTestBase; |
| 18 |
|
import mockit.Mocked; |
| 19 |
|
import mockit.Verifications; |
| 20 |
|
|
| 21 |
|
import static guru.mikelue.foxglove.ColumnMetaTestUtils.newColumnMeta; |
| 22 |
|
import static org.assertj.core.api.Assertions.assertThat; |
| 23 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 7 |
Complexity Density: 0.39 |
|
| 24 |
|
public class LayeredDataSettingTest extends AbstractTestBase { |
| 25 |
|
private final static DataSettingInfo firstSetting = new DataSetting() |
| 26 |
|
.setDefaultNumberOfRows(34) |
| 27 |
|
.<String>givenType(JDBCType.VARCHAR) |
| 28 |
|
.useSupplier(gen().string().length(16)) |
| 29 |
|
.addStatementSetter( |
| 30 |
|
meta -> meta.name().equals("col_1"), |
| 31 |
|
(stmt, index, meta, value) -> stmt.setInt(index, 30) |
| 32 |
|
); |
| 33 |
|
private final static DataSettingInfo secondSetting = new DataSetting() |
| 34 |
|
.<Integer>givenType(JDBCType.INTEGER) |
| 35 |
|
.useSupplier(gen().ints().range(1, 100)) |
| 36 |
|
.<String>givenType(JDBCType.VARCHAR) |
| 37 |
|
.useSupplier(gen().string().length(4)) |
| 38 |
|
.addStatementSetter( |
| 39 |
|
meta -> meta.name().equals("col_1"), |
| 40 |
|
(stmt, index, meta, value) -> stmt.setInt(index, 37) |
| 41 |
|
) |
| 42 |
|
.addStatementSetter( |
| 43 |
|
meta -> meta.name().equals("col_2"), |
| 44 |
|
(stmt, index, meta, value) -> stmt.setInt(index, 42) |
| 45 |
|
); |
| 46 |
|
|
| 47 |
|
private DataSettingInfo testedSetting = null; |
| 48 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 49 |
4 |
public LayeredDataSettingTest() {}... |
| 50 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 51 |
4 |
@BeforeEach... |
| 52 |
|
void setup() |
| 53 |
|
{ |
| 54 |
4 |
testedSetting = new LayeredDataSetting( |
| 55 |
|
firstSetting, secondSetting |
| 56 |
|
); |
| 57 |
|
} |
| 58 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 59 |
4 |
@AfterEach... |
| 60 |
|
void tearDown() {} |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@link |
| 64 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
| 65 |
1 |
@Test... |
| 66 |
|
void resolveSupplier() |
| 67 |
|
{ |
| 68 |
1 |
var varcharColumnMeta = newColumnMeta( |
| 69 |
|
"cl_varchar", JDBCType.VARCHAR, 12 |
| 70 |
|
); |
| 71 |
1 |
var intColumnMeta = newColumnMeta( |
| 72 |
|
"cl_int", JDBCType.INTEGER |
| 73 |
|
); |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
1 |
var testedSupplier = testedSetting |
| 79 |
|
.<String>resolveSupplier(varcharColumnMeta); |
| 80 |
|
|
| 81 |
1 |
assertThat(testedSupplier) |
| 82 |
|
.isPresent() |
| 83 |
|
.get().extracting(Supplier::get, InstanceOfAssertFactories.STRING) |
| 84 |
|
.hasSize(16); |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
1 |
var testedFallbackSupplier = testedSetting |
| 91 |
|
.<Integer>resolveSupplier(intColumnMeta); |
| 92 |
|
|
| 93 |
1 |
assertThat(testedFallbackSupplier) |
| 94 |
|
.isPresent(); |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
1 |
assertThat(testedSetting.getDefaultNumberOfRows()) |
| 101 |
|
.isEqualTo(34); |
| 102 |
|
|
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
4-
|
|
| 108 |
3 |
@SuppressWarnings("unchecked")... |
| 109 |
|
@ParameterizedTest |
| 110 |
|
@CsvSource({ |
| 111 |
|
"col_1, 30", |
| 112 |
|
"col_2, 42", |
| 113 |
|
"col_3,", |
| 114 |
|
}) |
| 115 |
|
void getStatementSetter( |
| 116 |
|
String columnName, |
| 117 |
|
Integer expectedValueOfSetInt, |
| 118 |
|
@Mocked PreparedStatement mockStat |
| 119 |
|
) throws SQLException { |
| 120 |
3 |
var columnMeta = newColumnMeta(columnName); |
| 121 |
|
|
| 122 |
3 |
var setterAssertion = assertThat(testedSetting.getStatementSetter(columnMeta)); |
| 123 |
|
|
| 124 |
3 |
if (expectedValueOfSetInt == null) { |
| 125 |
1 |
setterAssertion.isEmpty(); |
| 126 |
1 |
return; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
2 |
var testedSetter = (CustomStatementSetter<Integer>)setterAssertion |
| 130 |
|
.isPresent() |
| 131 |
|
.get().actual(); |
| 132 |
|
|
| 133 |
2 |
testedSetter.setParameter( |
| 134 |
|
mockStat, 1, columnMeta, Integer.valueOf(-1) |
| 135 |
|
); |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 140 |
2 |
new Verifications() {{... |
| 141 |
2 |
mockStat.setInt(1, expectedValueOfSetInt); |
| 142 |
2 |
times = 1; |
| 143 |
|
}}; |
| 144 |
|
|
| 145 |
|
} |
| 146 |
|
} |