Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2966444
commit e60119a
Showing
2 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
...earch/src/test/java/org/hibernate/search/test/query/nullValues/IndexAndQueryNullTest.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,118 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat, Inc. | ||
* | ||
* 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, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY 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 | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
package org.hibernate.search.test.query.nullValues; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.queryParser.QueryParser; | ||
import org.apache.lucene.search.Query; | ||
|
||
import org.hibernate.Transaction; | ||
import org.hibernate.search.FullTextQuery; | ||
import org.hibernate.search.FullTextSession; | ||
import org.hibernate.search.Search; | ||
import org.hibernate.search.test.SearchTestCase; | ||
import org.hibernate.search.test.query.ProjectionToMapResultTransformer; | ||
|
||
/** | ||
* Tests for indexing and querying {@code null} values. See HSEARCh-115 | ||
* | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class IndexAndQueryNullTest extends SearchTestCase { | ||
|
||
// HSEARCH-115 | ||
|
||
public void testIndexAndSearchNull() throws Exception { | ||
Value fooValue = new Value( "foo" ); | ||
Value nullValue = new Value( null ); | ||
|
||
FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); | ||
Transaction tx = fullTextSession.beginTransaction(); | ||
session.save( fooValue ); | ||
session.save( nullValue ); | ||
tx.commit(); | ||
|
||
fullTextSession.clear(); | ||
tx = fullTextSession.beginTransaction(); | ||
|
||
searchKeywordWithExpectedNumberOfResults( fullTextSession, "foo", 1 ); | ||
searchKeywordWithExpectedNumberOfResults( fullTextSession, "__null", 1 ); | ||
|
||
tx.commit(); | ||
fullTextSession.close(); | ||
} | ||
|
||
public void testLuceneDocumentContainsNullToken() throws Exception { | ||
Value nullValue = new Value( null ); | ||
|
||
FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); | ||
Transaction tx = fullTextSession.beginTransaction(); | ||
session.save( nullValue ); | ||
tx.commit(); | ||
|
||
fullTextSession.clear(); | ||
tx = fullTextSession.beginTransaction(); | ||
|
||
QueryParser parser = new QueryParser( getTargetLuceneVersion(), "id", SearchTestCase.standardAnalyzer ); | ||
parser.setAllowLeadingWildcard( true ); | ||
Query query = parser.parse( "*" ); | ||
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Value.class ); | ||
fullTextQuery.setProjection( | ||
FullTextQuery.DOCUMENT | ||
); | ||
fullTextQuery.setResultTransformer( new ProjectionToMapResultTransformer() ); | ||
List mappedResults = fullTextQuery.list(); | ||
assertTrue( "Wrong result size", mappedResults.size() == 1 ); | ||
|
||
Map map = (Map) mappedResults.get( 0 ); | ||
Document document = (Document) map.get( FullTextQuery.DOCUMENT ); | ||
assertNotNull( document ); | ||
|
||
String indexedNullString = document.get( "value" ); | ||
assertEquals( "The null value should be indexed as __null", "__null", indexedNullString ); | ||
|
||
tx.commit(); | ||
fullTextSession.close(); | ||
} | ||
|
||
private void searchKeywordWithExpectedNumberOfResults(FullTextSession fullTextSession, String queryString, int expectedNumberOfResults) | ||
throws Exception { | ||
QueryParser parser = new QueryParser( getTargetLuceneVersion(), "value", SearchTestCase.standardAnalyzer ); | ||
Query query = parser.parse( queryString ); | ||
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Value.class ); | ||
@SuppressWarnings("unchecked") | ||
List<Value> valueList = fullTextQuery.list(); | ||
assertEquals( "Wrong number of results", expectedNumberOfResults, valueList.size() ); | ||
} | ||
|
||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class[] { | ||
Value.class, | ||
}; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
hibernate-search/src/test/java/org/hibernate/search/test/query/nullValues/Value.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,70 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as | ||
* indicated by the @author tags or express copyright attribution | ||
* statements applied by the authors. All third-party contributions are | ||
* distributed under license by Red Hat, Inc. | ||
* | ||
* 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, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY 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 | ||
* along with this distribution; if not, write to: | ||
* Free Software Foundation, Inc. | ||
* 51 Franklin Street, Fifth Floor | ||
* Boston, MA 02110-1301 USA | ||
*/ | ||
package org.hibernate.search.test.query.nullValues; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.Indexed; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
@Entity | ||
@Indexed | ||
public class Value { | ||
@Id | ||
@GeneratedValue | ||
private int id; | ||
|
||
@Field | ||
private String value; | ||
|
||
public Value() { | ||
} | ||
|
||
public Value(String value) { | ||
this.value = value; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
|