Skip to content

Commit

Permalink
HSEARCH-115 Adding first testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Nov 4, 2010
1 parent 2966444 commit e60119a
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
@@ -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,
};
}
}
@@ -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;
}
}


0 comments on commit e60119a

Please sign in to comment.