Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz913763 - search term contains % should be escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Feb 22, 2013
1 parent 6f2482f commit 9922873
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion zanata-model/src/main/java/org/zanata/model/HTextFlow.java
Expand Up @@ -94,7 +94,7 @@
))
@Setter
@NoArgsConstructor
@ToString(of = {"resId", "revision", "contents", "comment", "obsolete"})
@ToString(of = {"resId", "revision", "comment", "obsolete"})
@Slf4j
public class HTextFlow extends HTextContainer implements Serializable, ITextFlowHistory, HasSimpleComment, HasContents
{
Expand Down
Expand Up @@ -57,6 +57,7 @@ private FilterConstraintToQuery(FilterConstraints constraints)
if (hasSearch)
{
String term = constraints.isCaseSensitive() ? constraints.getSearchString() : constraints.getSearchString().toLowerCase();
term = term.replaceAll("%", "\\\\%"); // we need to escape % in database query
searchString = "%" + term + "%";
}
}
Expand Down
Expand Up @@ -105,7 +105,7 @@ public GetProjectTransUnitListsResult execute(GetProjectTransUnitLists action, E
{
filterConstraints.ignoreTarget();
}
// TODO handle exception thrown by search service

List<HTextFlow> matchingFlows = textFlowSearchServiceImpl.findTextFlows(action.getWorkspaceId(), action.getDocumentPaths(), filterConstraints);
log.info("Returned {} results for search", matchingFlows.size());

Expand Down
Expand Up @@ -270,4 +270,18 @@ public void testSetParametersForQueryWithSearchCaseSensitive()
verify(query).setParameter(FilterConstraintToQuery.SEARCH_NAMED_PARAM, "%FiLe%");
verifyNoMoreInteractions(query);
}

@Test
public void testSetParametersForQueryWithSearchTermAsPercent()
{
FilterConstraints constraints = FilterConstraints.filterBy("% blah blah %").caseSensitive(true);
FilterConstraintToQuery constraintToQuery = FilterConstraintToQuery.filterInSingleDocument(constraints, documentId);

constraintToQuery.setQueryParameters(query, hLocale);

verify(query).setParameter(FilterConstraintToQuery.DOC_ID_NAMED_PARAM, documentId.getId());
verify(query).setParameter(FilterConstraintToQuery.LOCALE_NAMED_PARAM, hLocale.getId());
verify(query).setParameter(FilterConstraintToQuery.SEARCH_NAMED_PARAM, "%\\% blah blah \\%%");
verifyNoMoreInteractions(query);
}
}

0 comments on commit 9922873

Please sign in to comment.