Author: sannegrinovero
Date: 2009-05-17 21:44:29 -0400 (Sun, 17 May 2009)
New Revision: 16579
Modified:
search/trunk/src/test/java/org/hibernate/search/test/SearchTestCase.java
search/trunk/src/test/java/org/hibernate/search/test/analyzer/AnalyzerTest.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBackendTest.java
search/trunk/src/test/java/org/hibernate/search/test/query/ScrollableResultsTest.java
search/trunk/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java
Log:
review of some test utilities
Modified: search/trunk/src/test/java/org/hibernate/search/test/SearchTestCase.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/SearchTestCase.java 2009-05-17
14:03:07 UTC (rev 16578)
+++ search/trunk/src/test/java/org/hibernate/search/test/SearchTestCase.java 2009-05-18
01:44:29 UTC (rev 16579)
@@ -9,6 +9,7 @@
import org.slf4j.Logger;
import org.hibernate.HibernateException;
+import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
@@ -18,6 +19,7 @@
import org.hibernate.search.Environment;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
+import org.hibernate.search.SearchFactory;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.event.FullTextIndexEventListener;
import org.hibernate.search.store.RAMDirectoryProvider;
@@ -33,6 +35,8 @@
private static final Logger log = org.hibernate.search.util.LoggerFactory.make();
private static File indexDir;
+
+ private SearchFactory searchFactory;
static {
String buildDir = System.getProperty( "build.dir" );
@@ -52,10 +56,10 @@
protected void tearDown() throws Exception {
SchemaExport export = new SchemaExport( cfg );
export.drop( false, true );
+ searchFactory = null;
}
- @SuppressWarnings("unchecked")
- protected Directory getDirectory(Class clazz) {
+ protected Directory getDirectory(Class<?> clazz) {
return getLuceneEventListener().getSearchFactoryImplementor().getDirectoryProviders(
clazz )[0].getDirectory();
}
@@ -84,7 +88,7 @@
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx;
tx = s.beginTransaction();
- for ( Class clazz : getMappings() ) {
+ for ( Class<?> clazz : getMappings() ) {
if ( clazz.getAnnotation( Indexed.class ) != null ) {
s.purgeAll( clazz );
}
@@ -92,6 +96,16 @@
tx.commit();
s.close();
}
+
+ protected SearchFactory getSearchFactory() {
+ if ( searchFactory == null ) {
+ Session session = openSession();
+ FullTextSession fullTextSession = Search.getFullTextSession( session );
+ searchFactory = fullTextSession.getSearchFactory();
+ fullTextSession.close();
+ }
+ return searchFactory;
+ }
protected void configure(Configuration cfg) {
cfg.setProperty( "hibernate.search.default.directory_provider",
RAMDirectoryProvider.class.getName() );
@@ -105,7 +119,7 @@
return indexDir;
}
- protected void buildSessionFactory(Class[] classes, String[] packages, String[]
xmlFiles) throws Exception {
+ protected void buildSessionFactory(Class<?>[] classes, String[] packages, String[]
xmlFiles) throws Exception {
if ( getSessions() != null ) {
getSessions().close();
}
@@ -118,7 +132,7 @@
for ( String aPackage : packages ) {
( ( AnnotationConfiguration ) getCfg() ).addPackage( aPackage );
}
- for ( Class aClass : classes ) {
+ for ( Class<?> aClass : classes ) {
( ( AnnotationConfiguration ) getCfg() ).addAnnotatedClass( aClass );
}
for ( String xmlFile : xmlFiles ) {
@@ -134,7 +148,7 @@
}
}
- protected abstract Class[] getMappings();
+ protected abstract Class<?>[] getMappings();
protected String[] getAnnotatedPackages() {
return new String[] { };
Modified: search/trunk/src/test/java/org/hibernate/search/test/analyzer/AnalyzerTest.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/analyzer/AnalyzerTest.java 2009-05-17
14:03:07 UTC (rev 16578)
+++
search/trunk/src/test/java/org/hibernate/search/test/analyzer/AnalyzerTest.java 2009-05-18
01:44:29 UTC (rev 16579)
@@ -157,7 +157,7 @@
session.close();
}
- protected Class[] getMappings() {
+ protected Class<?>[] getMappings() {
return new Class[] { MyEntity.class, Article.class };
}
}
Modified:
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBackendTest.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBackendTest.java 2009-05-17
14:03:07 UTC (rev 16578)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/CustomBackendTest.java 2009-05-18
01:44:29 UTC (rev 16579)
@@ -26,8 +26,9 @@
FullTextSessionBuilder builder = new FullTextSessionBuilder();
FullTextSession ftSession = builder
.setProperty( "hibernate.search.worker.backend", name )
- .build();
+ .openFullTextSession();
SearchFactoryImpl searchFactory = (SearchFactoryImpl) ftSession.getSearchFactory();
+ ftSession.close();
assertEquals( backendType, searchFactory.getBackendQueueProcessorFactory().getClass()
);
builder.close();
}
Modified:
search/trunk/src/test/java/org/hibernate/search/test/query/ScrollableResultsTest.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/query/ScrollableResultsTest.java 2009-05-17
14:03:07 UTC (rev 16578)
+++
search/trunk/src/test/java/org/hibernate/search/test/query/ScrollableResultsTest.java 2009-05-18
01:44:29 UTC (rev 16579)
@@ -28,11 +28,12 @@
public void setUp() throws Exception {
super.setUp();
builder = new FullTextSessionBuilder();
- sess = builder
+ builder
.addAnnotatedClass( AlternateBook.class )
.addAnnotatedClass( Employee.class )
.setProperty( "hibernate.default_batch_fetch_size", "10" )
.build();
+ sess = builder.openFullTextSession();
Transaction tx = sess.beginTransaction();
//create some entities to query:
for ( int i = 0; i < 324; i++ ) {
Modified:
search/trunk/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java 2009-05-17
14:03:07 UTC (rev 16578)
+++
search/trunk/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java 2009-05-18
01:44:29 UTC (rev 16579)
@@ -1,6 +1,8 @@
// $Id$
package org.hibernate.search.test.util;
+import java.io.File;
+
import org.apache.lucene.analysis.StopAnalyzer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
@@ -8,7 +10,10 @@
import org.hibernate.cfg.Environment;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
+import org.hibernate.search.store.FSDirectoryProvider;
import org.hibernate.search.store.RAMDirectoryProvider;
+import org.hibernate.search.util.FileHelper;
+import org.slf4j.Logger;
/**
* Use the builder pattern to provide a SessionFactory.
@@ -20,10 +25,23 @@
*/
public class FullTextSessionBuilder {
+ private static final Logger log = org.hibernate.search.util.LoggerFactory.make();
+
+ private static File indexDir;
+
private AnnotationConfiguration cfg;
private SessionFactory sessionFactory;
- private Session session;
+ 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() );
+ }
+
public FullTextSessionBuilder() {
cfg = new AnnotationConfiguration();
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
@@ -37,11 +55,26 @@
//search specific:
cfg.setProperty( org.hibernate.search.Environment.ANALYZER_CLASS,
StopAnalyzer.class.getName() );
- cfg.setProperty( "hibernate.search.default.directory_provider",
- RAMDirectoryProvider.class.getName() );
+ useRAMDirectoryProvider( true );
}
/**
+ * @param use if true, use indexes in RAM otherwise use FSDirectoryProvider
+ * @return the same builder (this).
+ */
+ public FullTextSessionBuilder useRAMDirectoryProvider(boolean use) {
+ if ( use ) {
+ cfg.setProperty( "hibernate.search.default.directory_provider",
+ RAMDirectoryProvider.class.getName() );
+ }
+ else {
+ cfg.setProperty( "hibernate.search.default.directory_provider",
+ FSDirectoryProvider.class.getName() );
+ }
+ return this;
+ }
+
+ /**
* Override before building any parameter, or add new ones.
* @param key Property name.
* @param value Property value.
@@ -63,28 +96,35 @@
}
/**
- * Creates a new FullTextSession based upon the configuration built so far.
- * @return new FullTextSession based upon the configuration built so far.
+ * @return a new FullTextSession based upon the built configuration.
*/
- public FullTextSession build() {
- if ( session != null || sessionFactory != null ) {
- throw new java.lang.IllegalStateException( "session is open already" );
+ public FullTextSession openFullTextSession() {
+ if ( sessionFactory == null ) {
+ build();
}
- sessionFactory = cfg.buildSessionFactory();
- session = sessionFactory.openSession();
+ Session session = sessionFactory.openSession();
return Search.getFullTextSession( session );
}
/**
- * Closes the provided FullTextSession and the SessionFactory
+ * Closes the SessionFactory.
+ * Make sure you close all sessions first
*/
public void close() {
- if ( session == null || sessionFactory == null ) {
- throw new java.lang.IllegalStateException( "session not yet built" );
+ if ( sessionFactory == null ) {
+ throw new java.lang.IllegalStateException( "sessionFactory not yet built"
);
}
- session.close();
- session = null;
sessionFactory.close();
+ FileHelper.delete( indexDir );
sessionFactory = null;
}
+
+ /**
+ * Builds the sessionFactory as configured so far.
+ */
+ public FullTextSessionBuilder build() {
+ sessionFactory = cfg.buildSessionFactory();
+ return this;
+ }
+
}