-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-785 - Expose topDocs to integration layer, and make HSQueryIm…
…pl Serializable
- Loading branch information
Showing
5 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
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
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
90 changes: 90 additions & 0 deletions
90
hibernate-search/src/test/java/org/hibernate/search/test/engine/QuerySerializationTest.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,90 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2011 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.engine; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.TermQuery; | ||
import org.apache.lucene.search.TopDocs; | ||
import org.hibernate.Session; | ||
import org.hibernate.search.FullTextSession; | ||
import org.hibernate.search.Search; | ||
import org.hibernate.search.engine.SearchFactoryImplementor; | ||
import org.hibernate.search.query.engine.spi.DocumentExtractor; | ||
import org.hibernate.search.query.engine.spi.HSQuery; | ||
import org.hibernate.search.test.AlternateDocument; | ||
import org.hibernate.search.test.Document; | ||
import org.hibernate.search.test.SearchTestCase; | ||
import org.hibernate.search.test.SerializationTestHelper; | ||
import org.junit.Test; | ||
|
||
/** | ||
* This test is meant to verify that HSQuery implementation is able to | ||
* be serialized, deserialized and then still perform the query. | ||
* a | ||
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc. | ||
*/ | ||
public class QuerySerializationTest extends SearchTestCase { | ||
|
||
@Test | ||
public void testQueryObjectIsSerializable() throws IOException, ClassNotFoundException { | ||
Session s = getSessions().openSession(); | ||
s.getTransaction().begin(); | ||
Document document = | ||
new Document( "Hibernate OGM in Action", "Cloud mapping with Hibernate", "blah blah cloud blah cloud" ); | ||
s.persist( document ); | ||
s.getTransaction().commit(); | ||
s.close(); | ||
|
||
TermQuery query = new TermQuery( new Term( "Abstract", "hibernate" ) ); | ||
|
||
FullTextSession fullTextSession = Search.getFullTextSession( s ); | ||
SearchFactoryImplementor searchFactory = (SearchFactoryImplementor) fullTextSession.getSearchFactory(); | ||
|
||
//this is *not* the standard way to create a Query: | ||
HSQuery hsQuery = searchFactory.createHSQuery().luceneQuery( query ).targetedEntities( new ArrayList() ); | ||
int size = extractResultSize(hsQuery); | ||
|
||
assertEquals( "Should have found a match", 1, size ); | ||
|
||
HSQuery hsQueryDuplicate = (HSQuery) SerializationTestHelper.duplicateBySerialization(hsQuery); | ||
hsQueryDuplicate.afterDeserialise(searchFactory); | ||
int sizeOfDuplicate = extractResultSize(hsQueryDuplicate); | ||
|
||
assertEquals( "Should have found a match", 1, sizeOfDuplicate ); | ||
} | ||
|
||
private int extractResultSize(HSQuery hsQuery) { | ||
DocumentExtractor documentExtractor = hsQuery.queryDocumentExtractor(); | ||
TopDocs topDocs = documentExtractor.getTopDocs(); | ||
documentExtractor.close(); | ||
return topDocs.totalHits; | ||
} | ||
|
||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class[] { | ||
Document.class, | ||
AlternateDocument.class | ||
}; | ||
} | ||
|
||
} |