From 00d23e65ec7a8ffb3448a81254e8558c3ef9c491 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Fri, 1 Dec 2023 15:53:41 +0100 Subject: [PATCH] Properly delegate method calls for EscapingParameterSource. Closes #1681 See #1682 --- .../query/EscapingParameterSource.java | 16 +++ .../query/EscapingParameterSourceTest.java | 112 ++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/EscapingParameterSourceTest.java diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/EscapingParameterSource.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/EscapingParameterSource.java index b13aac24..5f3643f0 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/EscapingParameterSource.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/EscapingParameterSource.java @@ -51,4 +51,20 @@ class EscapingParameterSource implements SqlParameterSource { } return value; } + + + @Override + public int getSqlType(String paramName) { + return parameterSource.getSqlType(paramName); + } + + @Override + public String getTypeName(String paramName) { + return parameterSource.getTypeName(paramName); + } + + @Override + public String[] getParameterNames() { + return parameterSource.getParameterNames(); + } } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/EscapingParameterSourceTest.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/EscapingParameterSourceTest.java new file mode 100644 index 00000000..07776f72 --- /dev/null +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/EscapingParameterSourceTest.java @@ -0,0 +1,112 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.data.jdbc.repository.query; + +import static org.assertj.core.api.Assertions.*; + +import java.sql.Types; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.data.relational.core.dialect.Escaper; +import org.springframework.data.relational.core.query.ValueFunction; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; + +/** + * Tests for the {@link EscapingParameterSource}. + * + * @author Jens Schauder + */ +class EscapingParameterSourceTest { + + MapSqlParameterSource delegate = new MapSqlParameterSource(); + Escaper escaper = Escaper.of('x'); + EscapingParameterSource escapingParameterSource = new EscapingParameterSource(delegate, escaper); + + @Nested + class EmptyParameterSource { + + @Test + void getParameterNames() { + assertThat(escapingParameterSource.getParameterNames()).isEmpty(); + } + + @Test + void hasValue() { + assertThat(escapingParameterSource.hasValue("one")).isFalse(); + } + + @Test + void getNonExistingValue() { + assertThatIllegalArgumentException().isThrownBy(() -> escapingParameterSource.getValue("two")); + } + + } + + @Nested + class NonEmptyParameterSource { + + @BeforeEach + void before() { + delegate.addValue("one", 1, Types.INTEGER); + delegate.registerTypeName("one", "integer"); + delegate.addValue("needsEscaping", (ValueFunction) escaper -> escaper.escape("a%a") + "%", Types.VARCHAR); + delegate.registerTypeName("needsEscaping", "varchar"); + } + + @Test + void getParameterNames() { + assertThat(escapingParameterSource.getParameterNames()).containsExactlyInAnyOrder("one", "needsEscaping"); + } + + @Test + void hasValue() { + assertThat(escapingParameterSource.hasValue("one")).isTrue(); + assertThat(escapingParameterSource.hasValue("two")).isFalse(); + } + + @Test + void getNonExistingValue() { + assertThatIllegalArgumentException().isThrownBy(() -> escapingParameterSource.getValue("two")); + } + + @Test + void getValue() { + assertThat(escapingParameterSource.getValue("one")).isEqualTo(1); + } + + @Test + void getEscapedValue() { + assertThat(escapingParameterSource.getValue("needsEscaping")).isEqualTo("ax%a%"); + } + + @Test + void getSqlType() { + + assertThat(escapingParameterSource.getSqlType("one")).isEqualTo(Types.INTEGER); + assertThat(escapingParameterSource.getSqlType("needsEscaping")).isEqualTo(Types.VARCHAR); + } + + @Test + void getTypeName() { + + assertThat(escapingParameterSource.getTypeName("one")).isEqualTo("integer"); + assertThat(escapingParameterSource.getTypeName("needsEscaping")).isEqualTo("varchar"); + } + } +}