DATAJPA-1522 - Now also escaping the escape character.
Related tickets: DATAJPA-1519.
This commit is contained in:
committed by
Oliver Drotbohm
parent
3a748f1567
commit
25baa804f3
@@ -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<String> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -209,8 +209,25 @@ public class UserRepositoryFinderTests {
|
||||
User extra = new User("extra", "Matt_ew", "extra");
|
||||
userRepository.save(extra);
|
||||
|
||||
List<User> 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
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user