DATAJPA-1522 - Now also escaping the escape character.
Related tickets: DATAJPA-1519.
This commit is contained in:
committed by
Oliver Drotbohm
parent
170092f47c
commit
d15f5efc56
@@ -19,6 +19,7 @@ import lombok.Value;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
@@ -43,11 +44,11 @@ public class EscapeCharacter {
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
public String escape(String value) {
|
||||
public String escape(@Nullable String value) {
|
||||
|
||||
return value == null //
|
||||
? null //
|
||||
: TO_REPLACE.stream() //
|
||||
: Stream.concat(Stream.of(String.valueOf(escapeCharacter)), TO_REPLACE.stream()) //
|
||||
.reduce(value, (it, character) -> it.replace(character, this.escapeCharacter + character));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,43 @@
|
||||
/*
|
||||
* 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.assertj.core.api.Assertions.*;
|
||||
|
||||
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")).isEqualTo("alpha");
|
||||
}
|
||||
|
||||
@Test // DATAJPA-1522
|
||||
public void wildcardGetsEscaped() {
|
||||
assertThat(EscapeCharacter.of('x').escape("alp_ha")).isEqualTo("alpx_ha");
|
||||
}
|
||||
|
||||
@Test // DATAJPA-1522
|
||||
public void escapeCharacterGetsEscaped() {
|
||||
assertThat(EscapeCharacter.of('x').escape("axlpx_ha")).isEqualTo("axxlpxxx_ha");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user