View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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  }