|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 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 | + * http://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 org.hamcrest.core.Is; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.util.Properties; |
| 23 | + |
| 24 | +public class PropertyParserTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void replaceToVariableValue() { |
| 28 | + Properties props = new Properties(); |
| 29 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true"); |
| 30 | + props.setProperty("key", "value"); |
| 31 | + props.setProperty("tableName", "members"); |
| 32 | + props.setProperty("orderColumn", "member_id"); |
| 33 | + props.setProperty("a:b", "c"); |
| 34 | + Assert.assertThat(PropertyParser.parse("${key}", props), Is.is("value")); |
| 35 | + Assert.assertThat(PropertyParser.parse("${key:aaaa}", props), Is.is("value")); |
| 36 | + Assert.assertThat(PropertyParser.parse("SELECT * FROM ${tableName:users} ORDER BY ${orderColumn:id}", props), Is.is("SELECT * FROM members ORDER BY member_id")); |
| 37 | + |
| 38 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "false"); |
| 39 | + Assert.assertThat(PropertyParser.parse("${a:b}", props), Is.is("c")); |
| 40 | + |
| 41 | + props.remove(PropertyParser.KEY_ENABLE_DEFAULT_VALUE); |
| 42 | + Assert.assertThat(PropertyParser.parse("${a:b}", props), Is.is("c")); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void notReplace() { |
| 48 | + Properties props = new Properties(); |
| 49 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true"); |
| 50 | + Assert.assertThat(PropertyParser.parse("${key}", props), Is.is("${key}")); |
| 51 | + Assert.assertThat(PropertyParser.parse("${key}", null), Is.is("${key}")); |
| 52 | + |
| 53 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "false"); |
| 54 | + Assert.assertThat(PropertyParser.parse("${a:b}", props), Is.is("${a:b}")); |
| 55 | + |
| 56 | + props.remove(PropertyParser.KEY_ENABLE_DEFAULT_VALUE); |
| 57 | + Assert.assertThat(PropertyParser.parse("${a:b}", props), Is.is("${a:b}")); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void applyDefaultValue() { |
| 63 | + Properties props = new Properties(); |
| 64 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true"); |
| 65 | + Assert.assertThat(PropertyParser.parse("${key:default}", props), Is.is("default")); |
| 66 | + Assert.assertThat(PropertyParser.parse("SELECT * FROM ${tableName:users} ORDER BY ${orderColumn:id}", props), Is.is("SELECT * FROM users ORDER BY id")); |
| 67 | + Assert.assertThat(PropertyParser.parse("${key:}", props), Is.is("")); |
| 68 | + Assert.assertThat(PropertyParser.parse("${key: }", props), Is.is(" ")); |
| 69 | + Assert.assertThat(PropertyParser.parse("${key::}", props), Is.is(":")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void applyCustomSeparator() { |
| 74 | + Properties props = new Properties(); |
| 75 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true"); |
| 76 | + props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:"); |
| 77 | + Assert.assertThat(PropertyParser.parse("${key?:default}", props), Is.is("default")); |
| 78 | + Assert.assertThat(PropertyParser.parse("SELECT * FROM ${schema?:prod}.${tableName == null ? 'users' : tableName} ORDER BY ${orderColumn}", props), Is.is("SELECT * FROM prod.${tableName == null ? 'users' : tableName} ORDER BY ${orderColumn}")); |
| 79 | + Assert.assertThat(PropertyParser.parse("${key?:}", props), Is.is("")); |
| 80 | + Assert.assertThat(PropertyParser.parse("${key?: }", props), Is.is(" ")); |
| 81 | + Assert.assertThat(PropertyParser.parse("${key?::}", props), Is.is(":")); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments