1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.parsing;
17
18 import java.util.Properties;
19
20 import org.assertj.core.api.Assertions;
21 import org.junit.jupiter.api.Test;
22
23 class PropertyParserTest {
24
25 @Test
26 void replaceToVariableValue() {
27 Properties props = new Properties();
28 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
29 props.setProperty("key", "value");
30 props.setProperty("tableName", "members");
31 props.setProperty("orderColumn", "member_id");
32 props.setProperty("a:b", "c");
33 Assertions.assertThat(PropertyParser.parse("${key}", props)).isEqualTo("value");
34 Assertions.assertThat(PropertyParser.parse("${key:aaaa}", props)).isEqualTo("value");
35 Assertions.assertThat(PropertyParser.parse("SELECT * FROM ${tableName:users} ORDER BY ${orderColumn:id}", props))
36 .isEqualTo("SELECT * FROM members ORDER BY member_id");
37
38 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "false");
39 Assertions.assertThat(PropertyParser.parse("${a:b}", props)).isEqualTo("c");
40
41 props.remove(PropertyParser.KEY_ENABLE_DEFAULT_VALUE);
42 Assertions.assertThat(PropertyParser.parse("${a:b}", props)).isEqualTo("c");
43
44 }
45
46 @Test
47 void notReplace() {
48 Properties props = new Properties();
49 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
50 Assertions.assertThat(PropertyParser.parse("${key}", props)).isEqualTo("${key}");
51 Assertions.assertThat(PropertyParser.parse("${key}", null)).isEqualTo("${key}");
52
53 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "false");
54 Assertions.assertThat(PropertyParser.parse("${a:b}", props)).isEqualTo("${a:b}");
55
56 props.remove(PropertyParser.KEY_ENABLE_DEFAULT_VALUE);
57 Assertions.assertThat(PropertyParser.parse("${a:b}", props)).isEqualTo("${a:b}");
58
59 }
60
61 @Test
62 void applyDefaultValue() {
63 Properties props = new Properties();
64 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
65 Assertions.assertThat(PropertyParser.parse("${key:default}", props)).isEqualTo("default");
66 Assertions.assertThat(PropertyParser.parse("SELECT * FROM ${tableName:users} ORDER BY ${orderColumn:id}", props))
67 .isEqualTo("SELECT * FROM users ORDER BY id");
68 Assertions.assertThat(PropertyParser.parse("${key:}", props)).isEmpty();
69 Assertions.assertThat(PropertyParser.parse("${key: }", props)).isEqualTo(" ");
70 Assertions.assertThat(PropertyParser.parse("${key::}", props)).isEqualTo(":");
71 }
72
73 @Test
74 void applyCustomSeparator() {
75 Properties props = new Properties();
76 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
77 props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
78 Assertions.assertThat(PropertyParser.parse("${key?:default}", props)).isEqualTo("default");
79 Assertions
80 .assertThat(PropertyParser.parse(
81 "SELECT * FROM ${schema?:prod}.${tableName == null ? 'users' : tableName} ORDER BY ${orderColumn}", props))
82 .isEqualTo("SELECT * FROM prod.${tableName == null ? 'users' : tableName} ORDER BY ${orderColumn}");
83 Assertions.assertThat(PropertyParser.parse("${key?:}", props)).isEmpty();
84 Assertions.assertThat(PropertyParser.parse("${key?: }", props)).isEqualTo(" ");
85 Assertions.assertThat(PropertyParser.parse("${key?::}", props)).isEqualTo(":");
86 }
87
88 }