From 25baa804f3c06cf8da3e3450855eaa59e85256c1 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Mon, 1 Apr 2019 15:18:42 +0200 Subject: [PATCH] DATAJPA-1522 - Now also escaping the escape character. Related tickets: DATAJPA-1519. --- .../jpa/repository/query/EscapeCharacter.java | 5 +-- .../repository/UserRepositoryFinderTests.java | 21 ++++++++- .../query/EscapeCharacterUnitTests.java | 44 +++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/springframework/data/jpa/repository/query/EscapeCharacterUnitTests.java diff --git a/src/main/java/org/springframework/data/jpa/repository/query/EscapeCharacter.java b/src/main/java/org/springframework/data/jpa/repository/query/EscapeCharacter.java index cf7d29445..e382276b0 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/EscapeCharacter.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/EscapeCharacter.java @@ -18,7 +18,6 @@ package org.springframework.data.jpa.repository.query; import lombok.Value; import java.util.Arrays; -import java.util.List; /** * A value type encapsulating an escape character for LIKE queries and the actually usage of it in escaping @@ -30,8 +29,6 @@ import java.util.List; @Value(staticConstructor = "of") public class EscapeCharacter { - private static final List TO_REPLACE = Arrays.asList("_", "%"); - char escapeCharacter; /** @@ -48,7 +45,7 @@ public class EscapeCharacter { String result = value; - for (String toReplace : TO_REPLACE) { + for (String toReplace : Arrays.asList(String.valueOf(escapeCharacter), "_", "%")) { result = result.replace(toReplace, escapeCharacter + toReplace); } diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java index b99b53a39..550aa7668 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java @@ -209,8 +209,25 @@ public class UserRepositoryFinderTests { User extra = new User("extra", "Matt_ew", "extra"); userRepository.save(extra); - List result = userRepository.findContainingEscaped("att_"); - assertThat(result, containsInAnyOrder(extra)); + assertThat(userRepository.findContainingEscaped("att_"), contains(extra)); + } + + @Test // DATAJPA-1522 + public void escapingInLikeSpelsInThePresenceOfEscapeCharacters() { + + User withEscapeCharacter = userRepository.save(new User("extra", "Matt\\xew", "extra1")); + userRepository.save(new User("extra", "Matt\\_ew", "extra2")); + + assertThat(userRepository.findContainingEscaped("att\\x"), contains(withEscapeCharacter)); + } + + @Test // DATAJPA-1522 + public void escapingInLikeSpelsInThePresenceOfEscapedWildcards() { + + userRepository.save(new User("extra", "Matt\\xew", "extra1")); + User withEscapedWildcard = userRepository.save(new User("extra", "Matt\\_ew", "extra2")); + + assertThat(userRepository.findContainingEscaped("att\\_"), contains(withEscapedWildcard)); } @Test // DATAJPA-829 diff --git a/src/test/java/org/springframework/data/jpa/repository/query/EscapeCharacterUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/EscapeCharacterUnitTests.java new file mode 100644 index 000000000..47f42ef33 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/query/EscapeCharacterUnitTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2019 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 + * + * http://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.jpa.repository.query; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Unit tests for {@link EscapeCharacter}. + * + * @author Jens Schauder + */ +public class EscapeCharacterUnitTests { + + @Test // DATAJPA-1522 + public void nothingToEscape() { + assertThat(EscapeCharacter.of('x').escape("alpha"), is("alpha")); + } + + @Test // DATAJPA-1522 + public void wildcardGetsEscaped() { + assertThat(EscapeCharacter.of('x').escape("alp_ha"), is("alpx_ha")); + } + + @Test // DATAJPA-1522 + public void escapeCharacterGetsEscaped() { + assertThat(EscapeCharacter.of('x').escape("axlpx_ha"), is("axxlpxxx_ha")); + } +}