Package org.hibernate.search.test

Source Code of org.hibernate.search.test.SearchTestCase

//$Id: SearchTestCase.java 16478 2009-04-29 15:29:34Z hardy.ferentschik $
package org.hibernate.search.test;

import java.io.File;
import java.io.InputStream;

import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.store.Directory;
import org.slf4j.Logger;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.impl.SessionFactoryImpl;
import org.hibernate.search.Environment;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.event.FullTextIndexEventListener;
import org.hibernate.search.store.RAMDirectoryProvider;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
* Base class for Hibernate Search unit tests.
*
* @author Emmanuel Bernard
*/
public abstract class SearchTestCase extends TestCase {

  private static final Logger log = org.hibernate.search.util.LoggerFactory.make();

  private static File indexDir;

  static {
    String buildDir = System.getProperty( "build.dir" );
    if ( buildDir == null ) {
      buildDir = ".";
    }
    File current = new File( buildDir );
    indexDir = new File( current, "indextemp" );
    log.debug( "Using {} as index directory.", indexDir.getAbsolutePath() );
  }

  protected void setUp() throws Exception {
    buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
    ensureIndexesAreEmpty();
  }

  protected void tearDown() throws Exception {
    SchemaExport export = new SchemaExport( cfg );
    export.drop( false, true );
  }

  @SuppressWarnings("unchecked")
  protected Directory getDirectory(Class clazz) {
    return getLuceneEventListener().getSearchFactoryImplementor().getDirectoryProviders( clazz )[0].getDirectory();
  }

  private FullTextIndexEventListener getLuceneEventListener() {
    PostInsertEventListener[] listeners = ( ( SessionFactoryImpl ) getSessions() ).getEventListeners()
        .getPostInsertEventListeners();
    FullTextIndexEventListener listener = null;
    //FIXME this sucks since we mandante the event listener use
    for ( PostInsertEventListener candidate : listeners ) {
      if ( candidate instanceof FullTextIndexEventListener ) {
        listener = ( FullTextIndexEventListener ) candidate;
        break;
      }
    }
    if ( listener == null ) {
      throw new HibernateException( "Lucene event listener not initialized" );
    }
    return listener;
  }

  protected void ensureIndexesAreEmpty() {
    if ( "jms".equals( getCfg().getProperty( "hibernate.search.worker.backend" ) ) ) {
      log.debug( "JMS based test. Skipping index emptying" );
      return;
    }
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx;
    tx = s.beginTransaction();
    for ( Class clazz : getMappings() ) {
      if ( clazz.getAnnotation( Indexed.class ) != null ) {
        s.purgeAll( clazz );
      }
    }
    tx.commit();
    s.close();
  }

  protected void configure(Configuration cfg) {
    cfg.setProperty( "hibernate.search.default.directory_provider", RAMDirectoryProvider.class.getName() );
    cfg.setProperty( "hibernate.search.default.indexBase", indexDir.getAbsolutePath() );
    cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
    cfg.setProperty( "hibernate.search.default.transaction.merge_factor", "100" );
    cfg.setProperty( "hibernate.search.default.batch.max_buffered_docs", "1000" );
  }

  protected File getBaseIndexDir() {
    return indexDir;
  }

  protected void buildSessionFactory(Class[] classes, String[] packages, String[] xmlFiles) throws Exception {
    if ( getSessions() != null ) {
      getSessions().close();
    }
    try {
      setCfg( new AnnotationConfiguration() );
      configure( cfg );
      if ( recreateSchema() ) {
        cfg.setProperty( org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create-drop" );
      }
      for ( String aPackage : packages ) {
        ( ( AnnotationConfiguration ) getCfg() ).addPackage( aPackage );
      }
      for ( Class aClass : classes ) {
        ( ( AnnotationConfiguration ) getCfg() ).addAnnotatedClass( aClass );
      }
      for ( String xmlFile : xmlFiles ) {
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
        getCfg().addInputStream( is );
      }
      setDialect( Dialect.getDialect() );
      setSessions( getCfg().buildSessionFactory( /*new TestInterceptor()*/ ) );
    }
    catch ( Exception e ) {
      e.printStackTrace();
      throw e;
    }
  }

  protected abstract Class[] getMappings();

  protected String[] getAnnotatedPackages() {
    return new String[] { };
  }
}
TOP

Related Classes of org.hibernate.search.test.SearchTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.