//$Id: InheritanceTest.java 15602 2008-11-20 14:17:21Z hardy.ferentschik $
package org.hibernate.search.test.inheritance;
import java.util.List;
import java.io.Serializable;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.RangeQuery;
import org.apache.lucene.search.TermQuery;
import org.slf4j.Logger;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.util.LoggerFactory;
import org.hibernate.search.test.SearchTestCase;
/**
* @author Emmanuel Bernard
*/
public class InheritanceTest extends SearchTestCase {
private static final Logger log = LoggerFactory.make();
protected void setUp() throws Exception {
super.setUp();
}
public void testSearchUnindexClass() throws Exception {
createTestData();
QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
Query query = parser.parse( "Elephant" );
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
try {
org.hibernate.Query hibQuery = s.createFullTextQuery( query, String.class );
hibQuery.list();
tx.commit();
fail();
}
catch ( IllegalArgumentException iae ) {
log.debug( "success" );
}
tx = s.beginTransaction();
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
assertItsTheElephant( hibQuery.list() );
tx.commit();
s.close();
}
public void testInheritance() throws Exception {
createTestData();
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
Query query = parser.parse( "Elephant" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
assertItsTheElephant( hibQuery.list() );
query = parser.parse( "Elephant" );
hibQuery = s.createFullTextQuery( query);
assertItsTheElephant( hibQuery.list() );
query = parser.parse( "hasSweatGlands:false" );
hibQuery = s.createFullTextQuery( query, Animal.class, Mammal.class );
assertItsTheElephant( hibQuery.list() );
query = parser.parse( "Elephant OR White Pointer" );
hibQuery = s.createFullTextQuery( query, Being.class );
List result = hibQuery.list();
assertNotNull( result );
assertEquals( "Query filtering on superclass return mapped subclasses", 2, result.size() );
query = new RangeQuery( new Term( "weight", "04000" ), new Term( "weight", "05000" ), true );
hibQuery = s.createFullTextQuery( query, Animal.class );
assertItsTheElephant( hibQuery.list() );
query = parser.parse( "Elephant" );
hibQuery = s.createFullTextQuery( query, Being.class );
assertItsTheElephant( hibQuery.list() );
tx.commit();
s.close();
}
public void testPolymorphicQueries() throws Exception {
createTestData();
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
Query query = parser.parse( "Elephant" );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
assertItsTheElephant( hibQuery.list() );
hibQuery = s.createFullTextQuery( query, Animal.class );
assertItsTheElephant( hibQuery.list() );
hibQuery = s.createFullTextQuery( query, Being.class );
assertItsTheElephant( hibQuery.list() );
hibQuery = s.createFullTextQuery( query, Object.class );
assertItsTheElephant( hibQuery.list() );
hibQuery = s.createFullTextQuery( query, Serializable.class );
assertItsTheElephant( hibQuery.list() );
hibQuery = s.createFullTextQuery(
query, Mammal.class, Animal.class, Being.class, Object.class, Serializable.class
);
assertItsTheElephant( hibQuery.list() );
tx.commit();
s.close();
}
public void testSubclassInclusion() throws Exception {
createTestData();
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
Query query = new TermQuery( new Term( "numberOfEggs", "2" ) );
org.hibernate.Query hibQuery = s.createFullTextQuery( query, Eagle.class );
List result = hibQuery.list();
assertNotNull( result );
assertEquals( "Wrong number of hits. There should be two birds.", 1, result.size() );
query = new TermQuery( new Term( "numberOfEggs", "2" ) );
hibQuery = s.createFullTextQuery( query, Bird.class );
result = hibQuery.list();
assertNotNull( result );
assertEquals( "Wrong number of hits. There should be two birds.", 2, result.size() );
query = new TermQuery( new Term( "numberOfEggs", "2" ) );
hibQuery = s.createFullTextQuery( query, Mammal.class );
result = hibQuery.list();
assertNotNull( result );
assertEquals( "Wrong number of hits. There should be two birds.", 0, result.size() );
try {
query = new TermQuery( new Term( "numberOfEggs", "2" ) );
hibQuery = s.createFullTextQuery( query, String.class );
hibQuery.list();
fail();
}
catch ( IllegalArgumentException iae ) {
log.debug( "success" );
}
tx.commit();
s.close();
}
/**
* Tests that purging the index of a class also purges the index of the subclasses. See also HSEARCH-262.
*
* @throws Exception in case the test fails.
*/
public void testPurgeIndex() throws Exception {
createTestData();
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
assertNumberOfAnimals( s, 5 );
tx.commit();
tx = s.beginTransaction();
s.purgeAll( Serializable.class );
tx.commit();
tx = s.beginTransaction();
assertNumberOfAnimals( s, 3 );
tx.commit();
tx = s.beginTransaction();
s.purgeAll( Bird.class );
tx.commit();
tx = s.beginTransaction();
assertNumberOfAnimals( s, 1 );
tx.commit();
tx = s.beginTransaction();
s.purgeAll( Object.class );
tx.commit();
tx = s.beginTransaction();
assertNumberOfAnimals( s, 0 );
tx.commit();
s.close();
}
/**
* Tests that purging the an uninexed class triggers an exception.
*
* @throws Exception in case the test fails.
*/
public void testPurgeUnIndexClass() throws Exception {
createTestData();
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
assertNumberOfAnimals( s, 5 );
tx.commit();
tx = s.beginTransaction();
try {
s.purgeAll( String.class );
tx.commit();
fail();
}
catch ( IllegalArgumentException iae ) {
log.debug( "Success" );
}
s.close();
}
private void assertNumberOfAnimals(FullTextSession s, int count) throws Exception {
QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
Query query = parser.parse( "Elephant OR White Pointer OR Chimpanzee OR Dove or Eagle" );
List result = s.createFullTextQuery( query, Animal.class ).list();
assertNotNull( result );
assertEquals( "Wrong number of hits. There should be one elephant and one shark.", count, result.size() );
}
private void createTestData() {
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
Fish shark = new Fish();
shark.setName( "White Pointer" );
shark.setNumberOfDorsalFins( 2 );
shark.setWeight( 1500 );
s.save( shark );
Mammal elephant = new Mammal();
elephant.setName( "Elephant" );
elephant.setHasSweatGlands( false );
elephant.setWeight( 4500 );
s.save( elephant );
Mammal chimp = new Mammal();
chimp.setName( "Chimpanzee" );
chimp.setHasSweatGlands( true );
chimp.setWeight( 50 );
s.save( chimp );
Bird dove = new Bird();
dove.setName( "Dove" );
dove.setNumberOfEggs( 2 );
s.save( dove );
Eagle eagle = new Eagle();
eagle.setName( "Bald Eagle" );
eagle.setNumberOfEggs( 2 );
eagle.setWingYype( Eagle.WingType.BROAD );
s.save( eagle );
tx.commit();
s.clear();
}
private void assertItsTheElephant(List result) {
assertNotNull( result );
assertEquals( "Wrong number of results", 1, result.size() );
assertTrue( "Wrong result type", result.get( 0 ) instanceof Mammal );
Mammal mammal = ( Mammal ) result.get( 0 );
assertEquals( "Wrong animal name", "Elephant", mammal.getName() );
}
protected Class[] getMappings() {
return new Class[] {
Animal.class,
Mammal.class,
Fish.class,
Bird.class,
Eagle.class
};
}
}