我们做下测试:
源码如下:
List<ParameterMapping> params = Collections.emptyList();
BoundSql boundSql = new BoundSql(new Configuration(), "some sql", params, new Object());
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
boundSql.setAdditionalParameter("map", map);
Person bean = new Person();
bean.id = 1;
boundSql.setAdditionalParameter("person", bean);
String[] array = new String[] {"User1", "User2"};
boundSql.setAdditionalParameter("array", array);
assertFalse(boundSql.hasAdditionalParameter("pet"));
assertFalse(boundSql.hasAdditionalParameter("pet.name"));
assertTrue(boundSql.hasAdditionalParameter("map"));
assertTrue(boundSql.hasAdditionalParameter("map.key1"));
assertTrue(boundSql.hasAdditionalParameter("map.key2"), "should return true even if the child property does not exists.");
assertTrue(boundSql.hasAdditionalParameter("person"));
assertTrue(boundSql.hasAdditionalParameter("person.id"));
assertTrue(boundSql.hasAdditionalParameter("person.name"), "should return true even if the child property does not exists.");
assertTrue(boundSql.hasAdditionalParameter("array[0]"));
assertTrue(boundSql.hasAdditionalParameter("array[99]"), "should return true even if the element does not exists.");
`
二。cache CacheBuilder
private <T> T unwrap(Cache cache) {
Field field;
try {
field = cache.getClass().getDeclaredField("delegate");
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
}
try {
field.setAccessible(true);
return (T) field.get(cache);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
} finally {
field.setAccessible(false);
}
}
测试代码:
@Test
void testInitializing() {
InitializingCache cache = unwrap(new CacheBuilder("test").implementation(InitializingCache.class).build());
Assertions.assertThat(cache.initialized).isTrue();
}
测试结果:
三。ResultMappingTest:
这里结果映射运行:配置的结果的Mapping的“
void shouldThrowErrorWhenBothResultMapAndNestedSelectAreSet() {
Assertions.assertThrows(IllegalStateException.class, () -> {
new ResultMapping.Builder(configuration, "prop")
.nestedQueryId("nested query ID")
.nestedResultMapId("nested resultMap")
.build();
});
}
问题4:嵌套查询的问题:
@Test
void shouldFailWithAMissingColumnInNetstedSelect() {
Assertions.assertThrows(IllegalStateException.class, () -> new ResultMapping.Builder(configuration, "prop")
.nestedQueryId("nested query ID")
.build());
}