forked from hibernate/hibernate-search
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-1161 Throw a more specific exception type on searching on mea…
…ningless input
- Loading branch information
Showing
5 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
hibernate-search-engine/src/main/java/org/hibernate/search/errors/EmptyQueryException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors | ||
* as indicated by the @authors tag. All rights reserved. | ||
* See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, | ||
* modify, copy, or redistribute it subject to the terms and conditions | ||
* of the GNU Lesser General Public License, v. 2.1. | ||
* This program is distributed in the hope that it will be useful, but WITHOUT A | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | ||
* You should have received a copy of the GNU Lesser General Public License, | ||
* v.2.1 along with this distribution; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
package org.hibernate.search.errors; | ||
|
||
import org.hibernate.search.SearchException; | ||
|
||
|
||
/** | ||
* This Exception is thrown when an empty TermQuery (keyword query) is created, | ||
* or if any string query only returns whitespace after applying Analyzers. | ||
* | ||
* Applications should validate user input before running such a Query; | ||
* | ||
* @see org.hibernate.search.util.AnalyzerUtils | ||
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc. | ||
*/ | ||
public class EmptyQueryException extends SearchException { | ||
|
||
public EmptyQueryException() { | ||
super(); | ||
} | ||
|
||
public EmptyQueryException(String message) { | ||
super( message ); | ||
} | ||
|
||
public EmptyQueryException(String message, Throwable cause) { | ||
super( message, cause ); | ||
} | ||
|
||
public EmptyQueryException(Throwable cause) { | ||
super( cause ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...te-search-engine/src/test/java/org/hibernate/search/test/dsl/EmptyQueryExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors | ||
* as indicated by the @authors tag. All rights reserved. | ||
* See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* This copyrighted material is made available to anyone wishing to use, | ||
* modify, copy, or redistribute it subject to the terms and conditions | ||
* of the GNU Lesser General Public License, v. 2.1. | ||
* This program is distributed in the hope that it will be useful, but WITHOUT A | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | ||
* You should have received a copy of the GNU Lesser General Public License, | ||
* v.2.1 along with this distribution; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
* MA 02110-1301, USA. | ||
*/ | ||
package org.hibernate.search.test.dsl; | ||
|
||
import org.hibernate.search.annotations.DocumentId; | ||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.Indexed; | ||
import org.hibernate.search.backend.spi.Work; | ||
import org.hibernate.search.backend.spi.WorkType; | ||
import org.hibernate.search.engine.spi.SearchFactoryImplementor; | ||
import org.hibernate.search.errors.EmptyQueryException; | ||
import org.hibernate.search.query.dsl.QueryBuilder; | ||
import org.hibernate.search.test.programmaticmapping.TestingSearchFactoryHolder; | ||
import org.hibernate.search.test.util.ManualTransactionContext; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
|
||
/** | ||
* Verifies an EmptyQueryException is thrown when appropriate. | ||
* | ||
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc. | ||
*/ | ||
public class EmptyQueryExceptionTest { | ||
|
||
@Rule | ||
public TestingSearchFactoryHolder sfHolder = new TestingSearchFactoryHolder( Book.class ); | ||
|
||
@Rule | ||
public ExpectedException exceptions = ExpectedException.none(); | ||
|
||
@Test | ||
public void verifyExceptionOnNonMeaningfullQueries() { | ||
final SearchFactoryImplementor searchFactory = sfHolder.getSearchFactory(); | ||
|
||
Book book = new Book(); | ||
book.title = "Empty Book"; | ||
book.text = "The question is, does an empty book have 'space' tokens in it?"; | ||
|
||
Work work = new Work( book, book.title, WorkType.ADD, false ); | ||
ManualTransactionContext tc = new ManualTransactionContext(); | ||
searchFactory.getWorker().performWork( work, tc ); | ||
tc.end(); | ||
|
||
QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity( Book.class ).get(); | ||
|
||
exceptions.expect( EmptyQueryException.class ); | ||
|
||
queryBuilder.keyword().onField( "text" ).matching( " " ).createQuery(); | ||
// Hence the answer is: a program won't be able to tell you. | ||
} | ||
|
||
@Indexed | ||
static class Book { | ||
@DocumentId | ||
String title; | ||
|
||
@Field | ||
String text; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters