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

File MetaUtilsTest.java

 

Code metrics

2
63
20
2
298
227
21
0.33
3.15
10
1.05

Classes

Class Line # Actions
MetaUtilsTest 28 63 0% 21 0
1.0100%
MetaUtilsTest.TestCase 83 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    package guru.mikelue.foxglove.jdbc;
2   
3    import java.sql.*;
4    import java.util.EnumSet;
5    import java.util.List;
6    import java.util.function.Consumer;
7    import java.util.stream.Stream;
8   
9    import org.junit.jupiter.api.AfterEach;
10    import org.junit.jupiter.api.BeforeEach;
11    import org.junit.jupiter.api.Test;
12    import org.junit.jupiter.params.ParameterizedTest;
13    import org.junit.jupiter.params.provider.Arguments;
14    import org.junit.jupiter.params.provider.CsvSource;
15    import org.junit.jupiter.params.provider.MethodSource;
16   
17    import guru.mikelue.foxglove.ColumnMeta;
18    import guru.mikelue.foxglove.setting.DataSetting;
19    import guru.mikelue.misc.testlib.AbstractTestBase;
20   
21    import mockit.Expectations;
22    import mockit.Mocked;
23   
24    import static guru.mikelue.foxglove.ColumnMeta.Property.*;
25    import static guru.mikelue.foxglove.ColumnMetaTestUtils.newColumnMeta;
26    import static org.assertj.core.api.Assertions.assertThat;
27   
 
28    public class MetaUtilsTest extends AbstractTestBase {
29    @Mocked
30    private DatabaseMetaData mockDbMeta;
31    @Mocked
32    private ResultSet mockRs;
33   
34    private final static String TEST_TABLE = "ap_table";
35    private final static String SAMPLE_COLUMN_NAME = "rb_any_column";
36   
 
37  16 toggle public MetaUtilsTest() {}
38   
 
39  16 toggle @BeforeEach
40    void setup() {}
41   
 
42  16 toggle @AfterEach
43    void tearDown() {}
44   
45    /**
46    * Tests the filtering of columns with {@link JdbcTableFacet.ColumnInclusionMode#Include}
47    * and {@link DataSetInfo}.
48    */
 
49  3 toggle @ParameterizedTest
50    @MethodSource
51    void filterColumns(
52    Consumer<JdbcTableFacet.Builder> setupBuilder,
53    List<String> expectedInclusions
54    ) {
55  3 var builder = JdbcTableFacet.builder("kc_person");
56  3 setupBuilder.accept(builder);
57  3 var sampleTable = builder.build();
58   
59  3 var sampleSetting = new DataSetting()
60    .excludeWhen(columnMeta -> columnMeta.name().equals("kc_excluded_by_setting"));
61   
62  3 var testedResult = MetaUtils.filterColumns(
63    List.of(
64    newColumnMeta("kc_included_1"),
65    newColumnMeta("kc_included_2"),
66    newColumnMeta("kc_excluded_1"),
67    newColumnMeta("kc_excluded_2"),
68    newColumnMeta("kc_excluded_by_setting")
69    ),
70    sampleSetting, sampleTable
71    );
72   
73    /*
74    * Asserts the columns that should be included
75    */
76  3 assertThat(testedResult)
77    .extracting(ColumnMeta::name)
78    .containsExactlyInAnyOrderElementsOf(expectedInclusions);
79    // :~)
80    }
 
81  1 toggle static Stream<Arguments> filterColumns()
82    {
 
83    record TestCase(
84    Consumer<JdbcTableFacet.Builder> builder,
85    List<String> expectedInclusions
86    ) {}
87   
88  1 return Stream.of(
89    new TestCase( // Only includes columns defined by JdbcTableFacet
90    builder -> builder
91    .includeColumns("kc_included_1", "kc_included_2"),
92    List.of(
93    "kc_included_1", "kc_included_2"
94    )
95    ),
96    new TestCase( // Only excludes columns defined by JdbcTableFacet
97    builder -> builder
98    .excludeColumns("kc_excluded_1", "kc_excluded_2"),
99    List.of("kc_included_1", "kc_included_2")
100    ),
101    new TestCase( // Applies setting's auto-generating
102    builder -> builder
103    .column("kc_included_1").fixed(20),
104    List.of("kc_included_1", "kc_included_2", "kc_excluded_1", "kc_excluded_2")
105    )
106    )
107    .map(testCase -> Arguments.of(
108    testCase.builder,
109    testCase.expectedInclusions
110    ));
111    }
112   
113    /**
114    * Tests the mapping for: typeName, jdbcType, size, and decimalDigits.
115    */
 
116  1 toggle @Test
117    void descriptiveMeta() throws SQLException
118    {
119  1 final String sampleTypeName = "VARCHAR";
120  1 final int sampleColumnSize = 32;
121  1 final int sampleDecimalDigits = 10;
122   
123  1 mockOneRow();
124   
 
125  1 toggle new Expectations() {{
126  1 mockRs.getString("TYPE_NAME");
127  1 result = sampleTypeName;
128   
129  1 mockRs.getInt("DATA_TYPE");
130  1 result = Types.VARCHAR;
131   
132  1 mockRs.getInt("COLUMN_SIZE");
133  1 result = sampleColumnSize;
134   
135  1 mockRs.getInt("DECIMAL_DIGITS");
136  1 result = sampleDecimalDigits;
137    }};
138   
139  1 var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE);
140   
141  1 assertThat(testedMeta.get(0))
142    .hasFieldOrPropertyWithValue("name", SAMPLE_COLUMN_NAME)
143    .hasFieldOrPropertyWithValue("typeName", sampleTypeName)
144    .hasFieldOrPropertyWithValue("jdbcType", JDBCType.VARCHAR)
145    .hasFieldOrPropertyWithValue("size", sampleColumnSize)
146    .hasFieldOrPropertyWithValue("decimalDigits", sampleDecimalDigits);
147    }
148   
149    /**
150    * Tests the mapping for nullable property.
151    */
 
152  3 toggle @ParameterizedTest
153    @CsvSource({
154    "0, false", // columnNoNulls
155    "1, true", // columnNullable
156    "2, true" // columnNullableUnknown
157    })
158    void isNullable(int nullableValue, boolean expectedNullable) throws SQLException
159    {
160  3 mockOneRow();
161   
 
162  3 toggle new Expectations() {{
163  3 mockRs.getInt("NULLABLE");
164  3 result = nullableValue;
165    }};
166   
167  3 var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE);
168  3 assertContains(testedMeta.get(0).properties(),
169    NULLABLE, expectedNullable
170    );
171    }
172   
173    /**
174    * Tests the mapping for default value property.
175    */
 
176  2 toggle @ParameterizedTest
177    @CsvSource(
178    value={
179    "NULL, false",
180    "10, true"
181    },
182    nullValues={"NULL"}
183    )
184    void hasDefaultValue(Integer defaultValue, boolean expectedHasDefault) throws SQLException
185    {
186  2 mockOneRow();
187   
 
188  2 toggle new Expectations() {{
189  2 mockRs.getString("COLUMN_DEF");
190  2 result = defaultValue;
191    }};
192   
193  2 var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE);
194  2 assertContains(testedMeta.get(0).properties(), DEFAULT_VALUE, expectedHasDefault);
195    }
196   
197    /**
198    * Tests the mapping for auto-increment property.
199    */
 
200  3 toggle @ParameterizedTest
201    @CsvSource({
202    "YES, true",
203    "NO, false",
204    ", false"
205    })
206    void isAutoIncrement(String isAutoIncrementValue, boolean expectedIsAutoIncrement) throws SQLException
207    {
208  3 mockOneRow();
209   
 
210  3 toggle new Expectations() {{
211  3 mockRs.getString("IS_AUTOINCREMENT");
212  3 result = isAutoIncrementValue;
213   
214    }};
215   
216  3 var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE);
217  3 assertContains(testedMeta.get(0).properties(),
218    AUTO_INCREMENT, expectedIsAutoIncrement
219    );
220    }
221   
222    /**
223    * Tests the mapping for generated column property.
224    */
 
225  3 toggle @ParameterizedTest
226    @CsvSource({
227    "YES, true",
228    "NO, false",
229    ", false"
230    })
231    void isGeneratedColumn(String isGeneratedColumnValue, boolean expectedIsGeneratedColumn) throws SQLException
232    {
233  3 mockOneRow();
234   
 
235  3 toggle new Expectations() {{
236  3 mockRs.getString("IS_GENERATEDCOLUMN");
237  3 result = isGeneratedColumnValue;
238    }};
239   
240  3 var testedMeta = MetaUtils.getColumnMetaList(mockDbMeta, TEST_TABLE);
241  3 assertContains(testedMeta.get(0).properties(),
242    GENERATED, expectedIsGeneratedColumn
243    );
244    }
245   
246    /**
247    * Tests the building SQL by list of columns' meta.
248    */
 
249  1 toggle @Test
250    void buildInsertSql() throws SQLException
251    {
252  1 var sampleColumns = List.of(
253    newColumnMeta("kc_name", JDBCType.VARCHAR),
254    newColumnMeta("kc_age", JDBCType.INTEGER),
255    newColumnMeta("kc_address", JDBCType.VARCHAR)
256    );
257   
 
258  1 toggle new Expectations() {{
259  1 mockDbMeta.getIdentifierQuoteString();
260  1 result = "`";
261    }};
262   
263  1 var testedSql = MetaUtils.buildInsertSql(mockDbMeta, "kc_person", sampleColumns);
264   
265  1 getLogger().debug("Generated SQL:\n{}", testedSql);
266   
267  1 assertThat(testedSql)
268    .contains("INSERT INTO kc_person (kc_name, kc_age, kc_address)")
269    .contains("?, ?, ?");
270    }
271   
 
272  12 toggle private void mockOneRow() throws SQLException
273    {
 
274  12 toggle new Expectations() {{
275  12 mockDbMeta.getColumns(null, null, TEST_TABLE, null);
276  12 result = mockRs;
277   
278  12 mockRs.next();
279  12 returns(true, false);
280   
281  12 mockRs.getString("COLUMN_NAME");
282  12 result = SAMPLE_COLUMN_NAME;
283    }};
284    }
285   
 
286  11 toggle private static void assertContains(
287    EnumSet<ColumnMeta.Property> testedProperties, ColumnMeta.Property checkedProperty,
288    boolean expectedContains
289    ) {
290  11 if (expectedContains) {
291  5 assertThat(testedProperties)
292    .contains(checkedProperty);
293    } else {
294  6 assertThat(testedProperties)
295    .doesNotContain(checkedProperty);
296    }
297    }
298    }