|
| 1 | +/* |
| 2 | + * Copyright 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 | + |
| 17 | +package org.springframework.data.jdbc.repository.query; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.*; |
| 20 | + |
| 21 | +import java.sql.Types; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Nested; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.data.relational.core.dialect.Escaper; |
| 27 | +import org.springframework.data.relational.core.query.ValueFunction; |
| 28 | +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for the {@link EscapingParameterSource}. |
| 32 | + * |
| 33 | + * @author Jens Schauder |
| 34 | + */ |
| 35 | +class EscapingParameterSourceTest { |
| 36 | + |
| 37 | + MapSqlParameterSource delegate = new MapSqlParameterSource(); |
| 38 | + Escaper escaper = Escaper.of('x'); |
| 39 | + EscapingParameterSource escapingParameterSource = new EscapingParameterSource(delegate, escaper); |
| 40 | + |
| 41 | + @Nested |
| 42 | + class EmptyParameterSource { |
| 43 | + |
| 44 | + @Test |
| 45 | + void getParameterNames() { |
| 46 | + assertThat(escapingParameterSource.getParameterNames()).isEmpty(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void hasValue() { |
| 51 | + assertThat(escapingParameterSource.hasValue("one")).isFalse(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void getNonExistingValue() { |
| 56 | + assertThatIllegalArgumentException().isThrownBy(() -> escapingParameterSource.getValue("two")); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Nested |
| 62 | + class NonEmptyParameterSource { |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + void before() { |
| 66 | + delegate.addValue("one", 1, Types.INTEGER); |
| 67 | + delegate.registerTypeName("one", "integer"); |
| 68 | + delegate.addValue("needsEscaping", (ValueFunction<String>) escaper -> escaper.escape("a%a") + "%", Types.VARCHAR); |
| 69 | + delegate.registerTypeName("needsEscaping", "varchar"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void getParameterNames() { |
| 74 | + assertThat(escapingParameterSource.getParameterNames()).containsExactlyInAnyOrder("one", "needsEscaping"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void hasValue() { |
| 79 | + assertThat(escapingParameterSource.hasValue("one")).isTrue(); |
| 80 | + assertThat(escapingParameterSource.hasValue("two")).isFalse(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void getNonExistingValue() { |
| 85 | + assertThatIllegalArgumentException().isThrownBy(() -> escapingParameterSource.getValue("two")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void getValue() { |
| 90 | + assertThat(escapingParameterSource.getValue("one")).isEqualTo(1); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void getEscapedValue() { |
| 95 | + assertThat(escapingParameterSource.getValue("needsEscaping")).isEqualTo("ax%a%"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void getSqlType() { |
| 100 | + |
| 101 | + assertThat(escapingParameterSource.getSqlType("one")).isEqualTo(Types.INTEGER); |
| 102 | + assertThat(escapingParameterSource.getSqlType("needsEscaping")).isEqualTo(Types.VARCHAR); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void getTypeName() { |
| 107 | + |
| 108 | + assertThat(escapingParameterSource.getTypeName("one")).isEqualTo("integer"); |
| 109 | + assertThat(escapingParameterSource.getTypeName("needsEscaping")).isEqualTo("varchar"); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments