|
| 1 | +/** |
| 2 | + * Copyright 2016-2019 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 | + * http://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 | +package issue.gh105; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mybatis.dynamic.sql.SqlBuilder.*; |
| 20 | + |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +import static issue.gh105.PersonDynamicSqlSupport.*; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.mybatis.dynamic.sql.render.RenderingStrategy; |
| 27 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 28 | + |
| 29 | +public class Issue105Test { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testFuzzySearchBothPresent() { |
| 33 | + String fName = "Fred"; |
| 34 | + String lName = "Flintstone"; |
| 35 | + |
| 36 | + SelectStatementProvider selectStatement = select(id, firstName, lastName) |
| 37 | + .from(person) |
| 38 | + .where(firstName, isLike(fName).when(Objects::nonNull).then(s -> "%" + s + "%")) |
| 39 | + .and(lastName, isLike(lName).when(Objects::nonNull).then(s -> "%" + s + "%")) |
| 40 | + .build() |
| 41 | + .render(RenderingStrategy.MYBATIS3); |
| 42 | + |
| 43 | + String expected = "select person_id, first_name, last_name" |
| 44 | + + " from Person" |
| 45 | + + " where first_name like #{parameters.p1}" |
| 46 | + + " and last_name like #{parameters.p2}"; |
| 47 | + |
| 48 | + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); |
| 49 | + assertThat(selectStatement.getParameters().get("p1")).isEqualTo("%Fred%"); |
| 50 | + assertThat(selectStatement.getParameters().get("p2")).isEqualTo("%Flintstone%"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testFuzzySearchFirstNameNull() { |
| 55 | + String fName = null; |
| 56 | + String lName = "Flintstone"; |
| 57 | + |
| 58 | + SelectStatementProvider selectStatement = select(id, firstName, lastName) |
| 59 | + .from(person) |
| 60 | + .where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards)) |
| 61 | + .and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards)) |
| 62 | + .build() |
| 63 | + .render(RenderingStrategy.MYBATIS3); |
| 64 | + |
| 65 | + String expected = "select person_id, first_name, last_name" |
| 66 | + + " from Person" |
| 67 | + + " where last_name like #{parameters.p1}"; |
| 68 | + |
| 69 | + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); |
| 70 | + assertThat(selectStatement.getParameters().get("p1")).isEqualTo("%Flintstone%"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testFuzzySearchLastNameNull() { |
| 75 | + String fName = "Fred"; |
| 76 | + String lName = null; |
| 77 | + |
| 78 | + SelectStatementProvider selectStatement = select(id, firstName, lastName) |
| 79 | + .from(person) |
| 80 | + .where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards)) |
| 81 | + .and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards)) |
| 82 | + .build() |
| 83 | + .render(RenderingStrategy.MYBATIS3); |
| 84 | + |
| 85 | + String expected = "select person_id, first_name, last_name" |
| 86 | + + " from Person" |
| 87 | + + " where first_name like #{parameters.p1}"; |
| 88 | + |
| 89 | + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); |
| 90 | + assertThat(selectStatement.getParameters().get("p1")).isEqualTo("%Fred%"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testFuzzySearchBothNull() { |
| 95 | + String fName = null; |
| 96 | + String lName = null; |
| 97 | + |
| 98 | + SelectStatementProvider selectStatement = select(id, firstName, lastName) |
| 99 | + .from(person) |
| 100 | + .where(firstName, isLike(fName).when(Objects::nonNull).then(SearchUtils::addWildcards)) |
| 101 | + .and(lastName, isLike(lName).when(Objects::nonNull).then(SearchUtils::addWildcards)) |
| 102 | + .build() |
| 103 | + .render(RenderingStrategy.MYBATIS3); |
| 104 | + |
| 105 | + String expected = "select person_id, first_name, last_name" |
| 106 | + + " from Person"; |
| 107 | + |
| 108 | + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); |
| 109 | + assertThat(selectStatement.getParameters().size()).isEqualTo(0); |
| 110 | + } |
| 111 | +} |
0 commit comments