[hibernate-commits] Hibernate SVN: r19045 - in search/trunk: hibernate-search and 10 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Mon Mar 22 09:07:45 EDT 2010
Author: hardy.ferentschik
Date: 2010-03-22 09:07:43 -0400 (Mon, 22 Mar 2010)
New Revision: 19045
Added:
search/trunk/hibernate-search-testing/
search/trunk/hibernate-search-testing/pom.xml
search/trunk/hibernate-search-testing/src/
search/trunk/hibernate-search-testing/src/main/
search/trunk/hibernate-search-testing/src/main/java/
search/trunk/hibernate-search-testing/src/main/java/org/
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/JPATestCase.java
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/SearchTestCase.java
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/TestCase.java
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/jpa/
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/AnalyzerUtils.java
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/FullTextSessionBuilder.java
search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/textbuilder/
search/trunk/hibernate-search-testing/src/main/resources/
search/trunk/hibernate-search-testing/src/test/
search/trunk/hibernate-search-testing/src/test/resources/
Modified:
search/trunk/hibernate-search/pom.xml
search/trunk/pom.xml
Log:
HSEARCH-468 Added hibernate-search-testing at the cost of code duplication
Modified: search/trunk/hibernate-search/pom.xml
===================================================================
--- search/trunk/hibernate-search/pom.xml 2010-03-22 12:45:33 UTC (rev 19044)
+++ search/trunk/hibernate-search/pom.xml 2010-03-22 13:07:43 UTC (rev 19045)
@@ -23,8 +23,9 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
-->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -40,7 +41,6 @@
<description>Hibernate Search</description>
<dependencies>
- <!-- REQUIRED DEPENDECIES -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
@@ -58,34 +58,12 @@
<artifactId>lucene-core</artifactId>
</dependency>
<dependency>
- <groupId>javax.transaction</groupId>
- <artifactId>jta</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.activemq</groupId>
- <artifactId>activemq-core</artifactId>
- </dependency>
-
- <!-- OPTIONAL DEPENDECIES -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-annotations</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-common</artifactId>
- <optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
- <optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
@@ -108,32 +86,44 @@
<optional>true</optional>
</dependency>
<dependency>
- <groupId>javax.jms</groupId>
- <artifactId>jms</artifactId>
- <scope>provided</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
<groupId>jgroups</groupId>
<artifactId>jgroups</artifactId>
<optional>true</optional>
</dependency>
<dependency>
- <groupId>javax.annotation</groupId>
- <artifactId>jsr250-api</artifactId>
- <optional>true</optional>
+ <groupId>javax.jms</groupId>
+ <artifactId>jms</artifactId>
+ <scope>provided</scope>
</dependency>
- <!-- TEST DEPENDECIES -->
<dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
- <defaultGoal>test</defaultGoal>
+ <testResources>
+ <testResource>
+ <filtering>true</filtering>
+ <directory>src/test/resources</directory>
+ <includes>
+ <include>**/*.properties</include>
+ <include>**/*.xml</include>
+ </includes>
+ </testResource>
+ </testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -147,19 +137,19 @@
<value>${basedir}/target</value>
</property>
<!--
- Following is the default jgroups mcast address. If you find the testsuite runs very slowly,
- there may be problems with multicast on the interface JGroups uses by default on
- your machine. You can try to resolve setting 'jgroups.bind_addr' as a system-property
- to the jvm launching maven and setting the value to an interface where you know multicast works
- -->
+ Following is the default jgroups mcast address. If you find the testsuite runs very slowly,
+ there may be problems with multicast on the interface JGroups uses by default on
+ your machine. You can try to resolve setting 'jgroups.bind_addr' as a system-property
+ to the jvm launching maven and setting the value to an interface where you know multicast works
+ -->
<property>
<name>jgroups.bind_addr</name>
<value>127.0.0.1</value>
</property>
<!-- There are problems with multicast and IPv6 on some OS/JDK combos, so we tell Java
- to use IPv4. If you have problems with multicast when running the tests you can
- try setting this to 'false', although typically that won't be helpful.
- -->
+ to use IPv4. If you have problems with multicast when running the tests you can
+ try setting this to 'false', although typically that won't be helpful.
+ -->
<property>
<name>java.net.preferIPv4Stack</name>
<value>true</value>
@@ -192,14 +182,12 @@
</format>
<format>
<formatName>html_single</formatName>
- <stylesheetResource>classpath:/xslt/org/hibernate/jdocbook/xslt/xhtml-single.xsl
- </stylesheetResource>
+ <stylesheetResource>classpath:/xslt/org/hibernate/jdocbook/xslt/xhtml-single.xsl </stylesheetResource>
<finalName>index.html</finalName>
</format>
<format>
<formatName>html</formatName>
- <stylesheetResource>classpath:/xslt/org/hibernate/jdocbook/xslt/xhtml.xsl
- </stylesheetResource>
+ <stylesheetResource>classpath:/xslt/org/hibernate/jdocbook/xslt/xhtml.xsl </stylesheetResource>
<finalName>index.html</finalName>
</format>
</formats>
@@ -215,6 +203,10 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<stylesheetfile>${basedir}/src/main/javadoc/jdstyle.css</stylesheetfile>
@@ -233,9 +225,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
- <goals>deploy javadoc:javadoc org.jboss.maven.plugins:maven-jdocbook-plugin:2.2.0:resources
- org.jboss.maven.plugins:maven-jdocbook-plugin:2.2.0:generate assembly:assembly
- </goals>
+ <goals>deploy javadoc:javadoc
+ org.jboss.maven.plugins:maven-jdocbook-plugin:2.2.0:resources
+ org.jboss.maven.plugins:maven-jdocbook-plugin:2.2.0:generate
+ assembly:assembly </goals>
</configuration>
</plugin>
<plugin>
@@ -264,15 +257,5 @@
</configuration>
</plugin>
</plugins>
- <testResources>
- <testResource>
- <filtering>true</filtering>
- <directory>src/test/resources</directory>
- <includes>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- </testResource>
- </testResources>
</build>
</project>
Property changes on: search/trunk/hibernate-search-testing
___________________________________________________________________
Name: svn:ignore
+ target
Added: search/trunk/hibernate-search-testing/pom.xml
===================================================================
--- search/trunk/hibernate-search-testing/pom.xml (rev 0)
+++ search/trunk/hibernate-search-testing/pom.xml 2010-03-22 13:07:43 UTC (rev 19045)
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- $Id:$ -->
+<!--
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, 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
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <artifactId>hibernate-search-parent</artifactId>
+ <groupId>org.hibernate</groupId>
+ <version>3.2.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+
+ <artifactId>hibernate-search-testing</artifactId>
+
+ <name>Hibernate Search Testing</name>
+ <description>Hibernate Search Testing Utilities</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>hibernate-search</artifactId>
+ <version>${pom.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ </dependencies>
+</project>
Copied: search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/JPATestCase.java (from rev 19033, search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/jpa/JPATestCase.java)
===================================================================
--- search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/JPATestCase.java (rev 0)
+++ search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/JPATestCase.java 2010-03-22 13:07:43 UTC (rev 19045)
@@ -0,0 +1,138 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, 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.jpa;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.ArrayList;import java.util.Arrays;
+import java.io.InputStream;
+import java.io.IOException;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+import org.hibernate.cfg.Environment;
+import org.hibernate.ejb.AvailableSettings;
+import org.hibernate.ejb.HibernatePersistence;
+import org.hibernate.search.store.RAMDirectoryProvider;
+import org.hibernate.search.test.SearchTestCase;
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.util.Version;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public abstract class JPATestCase extends junit.framework.TestCase {
+ protected EntityManagerFactory factory;
+
+ public JPATestCase() {
+ super();
+ }
+
+ public JPATestCase(String name) {
+ super( name );
+ }
+
+ public void setUp() {
+ factory = new HibernatePersistence().createEntityManagerFactory( getConfig() );
+ }
+
+ public void tearDown() {
+ factory.close();
+ }
+
+ public abstract Class[] getAnnotatedClasses();
+
+ public String[] getEjb3DD() {
+ return new String[]{};
+ }
+
+ public Map<Class, String> getCachedClasses() {
+ return new HashMap<Class, String>();
+ }
+
+ public Map<String, String> getCachedCollections() {
+ return new HashMap<String, String>();
+ }
+
+ public static Properties loadProperties() {
+ Properties props = new Properties();
+ InputStream stream = Persistence.class.getResourceAsStream( "/hibernate.properties" );
+ if ( stream != null ) {
+ try {
+ props.load( stream );
+ }
+ catch (Exception e) {
+ throw new RuntimeException( "could not load hibernate.properties" );
+ }
+ finally {
+ try {
+ stream.close();
+ }
+ catch (IOException ioe) {
+ }
+ }
+ }
+ props.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
+ return props;
+ }
+
+ public Map getConfig() {
+ Map config = loadProperties();
+ ArrayList<Class> classes = new ArrayList<Class>();
+
+ classes.addAll( Arrays.asList( getAnnotatedClasses() ) );
+ config.put( AvailableSettings.LOADED_CLASSES, classes );
+ for ( Map.Entry<Class, String> entry : getCachedClasses().entrySet() ) {
+ config.put(
+ AvailableSettings.CLASS_CACHE_PREFIX + "." + entry.getKey().getName(),
+ entry.getValue()
+ );
+ }
+ for ( Map.Entry<String, String> entry : getCachedCollections().entrySet() ) {
+ config.put(
+ AvailableSettings.COLLECTION_CACHE_PREFIX + "." + entry.getKey(),
+ entry.getValue()
+ );
+ }
+ if ( getEjb3DD().length > 0 ) {
+ ArrayList<String> dds = new ArrayList<String>();
+ dds.addAll( Arrays.asList( getEjb3DD() ) );
+ config.put( AvailableSettings.XML_FILE_NAMES, dds );
+ }
+
+ //Search config
+ config.put( "hibernate.search.default.directory_provider", RAMDirectoryProvider.class.getName() );
+ config.put( org.hibernate.search.Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+
+ return config;
+ }
+
+ public static Version getTargetLuceneVersion() {
+ return SearchTestCase.getTargetLuceneVersion();
+ }
+
+}
+
Copied: search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/SearchTestCase.java (from rev 19033, search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/SearchTestCase.java)
===================================================================
--- search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/SearchTestCase.java (rev 0)
+++ search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/SearchTestCase.java 2010-03-22 13:07:43 UTC (rev 19045)
@@ -0,0 +1,198 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, 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;
+
+import java.io.File;
+import java.io.InputStream;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.KeywordAnalyzer;
+import org.apache.lucene.analysis.SimpleAnalyzer;
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.Version;
+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;
+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.SearchFactory;
+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();
+
+ public static Analyzer standardAnalyzer = new StandardAnalyzer( getTargetLuceneVersion() );
+ public static Analyzer stopAnalyzer = new StopAnalyzer( getTargetLuceneVersion() );
+ public static Analyzer simpleAnalyzer = new SimpleAnalyzer();
+ public static Analyzer keywordAnalyzer = new KeywordAnalyzer();
+
+ private static File indexDir;
+
+ private SearchFactory searchFactory;
+
+ 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 );
+ searchFactory = null;
+ }
+
+ 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 mandate 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 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() );
+ 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[] { };
+ }
+
+ protected static File getIndexDir() {
+ return indexDir;
+ }
+
+ public static Version getTargetLuceneVersion() {
+ return Version.LUCENE_29;
+ }
+
+}
Copied: search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/TestCase.java (from rev 19033, search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/TestCase.java)
===================================================================
--- search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/TestCase.java (rev 0)
+++ search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/TestCase.java 2010-03-22 13:07:43 UTC (rev 19045)
@@ -0,0 +1,189 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, 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;
+
+import java.io.InputStream;
+
+import org.apache.lucene.analysis.StopAnalyzer;
+
+import org.hibernate.HibernateException;
+import org.hibernate.Interceptor;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.search.store.RAMDirectoryProvider;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.event.FlushEventListener;
+import org.hibernate.event.def.DefaultFlushEventListener;
+import org.hibernate.search.event.FullTextIndexEventListener;
+
+/**
+ * A modified base class for tests without annotations.
+ *
+ * @author Hardy Ferentschik
+ */
+public abstract class TestCase extends junit.framework.TestCase {
+
+ protected static SessionFactory sessions;
+ protected static Configuration cfg;
+ protected static Dialect dialect;
+ protected static Class lastTestClass;
+ protected Session session;
+
+ public TestCase() {
+ super();
+ }
+
+ public TestCase(String x) {
+ super( x );
+ }
+
+ protected void buildSessionFactory(String[] xmlFiles) throws Exception {
+
+ if ( getSessions() != null ) {
+ getSessions().close();
+ }
+ try {
+ setCfg( new Configuration() );
+ configure( cfg );
+ if ( recreateSchema() ) {
+ cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
+ }
+ for ( String xmlFile : xmlFiles ) {
+ InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( xmlFile );
+ getCfg().addInputStream( is );
+ }
+ setDialect( Dialect.getDialect() );
+ setSessions( getCfg().buildSessionFactory() );
+ }
+ catch ( Exception e ) {
+ e.printStackTrace();
+ throw e;
+ }
+ }
+
+ protected void setUp() throws Exception {
+ if ( getSessions() == null || getSessions().isClosed() || lastTestClass != getClass() ) {
+ buildSessionFactory( getXmlFiles() );
+ lastTestClass = getClass();
+ }
+ }
+
+ protected void runTest() throws Throwable {
+ try {
+ super.runTest();
+ if ( session != null && session.isOpen() ) {
+ if ( session.isConnected() ) {
+ session.connection().rollback();
+ }
+ session.close();
+ session = null;
+ fail( "unclosed session" );
+ }
+ else {
+ session = null;
+ }
+ }
+ catch ( Throwable e ) {
+ try {
+ if ( session != null && session.isOpen() ) {
+ if ( session.isConnected() ) {
+ session.connection().rollback();
+ }
+ session.close();
+ }
+ }
+ catch ( Exception ignore ) {
+ }
+ try {
+ if ( sessions != null ) {
+ sessions.close();
+ sessions = null;
+ }
+ }
+ catch ( Exception ignore ) {
+ }
+ throw e;
+ }
+ }
+
+ public Session openSession() throws HibernateException {
+ session = getSessions().openSession();
+ return session;
+ }
+
+ public Session openSession(Interceptor interceptor) throws HibernateException {
+ session = getSessions().openSession( interceptor );
+ return session;
+ }
+
+ protected String[] getXmlFiles() {
+ return new String[] { };
+ }
+
+ protected void setSessions(SessionFactory sessions) {
+ TestCase.sessions = sessions;
+ }
+
+ protected SessionFactory getSessions() {
+ return sessions;
+ }
+
+ protected void setDialect(Dialect dialect) {
+ TestCase.dialect = dialect;
+ }
+
+ protected Dialect getDialect() {
+ return dialect;
+ }
+
+ protected static void setCfg(Configuration cfg) {
+ TestCase.cfg = cfg;
+ }
+
+ protected static Configuration getCfg() {
+ return cfg;
+ }
+
+ protected void configure(Configuration cfg) {
+ //needs to register all event listeners:
+ cfg.setListener( "post-update", "org.hibernate.search.event.FullTextIndexEventListener" );
+ cfg.setListener( "post-insert", "org.hibernate.search.event.FullTextIndexEventListener" );
+ cfg.setListener( "post-delete", "org.hibernate.search.event.FullTextIndexEventListener" );
+ cfg.setListener( "post-collection-recreate", "org.hibernate.search.event.FullTextIndexEventListener" );
+ cfg.setListener( "post-collection-remove", "org.hibernate.search.event.FullTextIndexEventListener" );
+ cfg.setListener( "post-collection-update", "org.hibernate.search.event.FullTextIndexEventListener" );
+
+ cfg.setListeners( "flush", new FlushEventListener[]{new DefaultFlushEventListener(), new FullTextIndexEventListener()} );
+
+ cfg.setProperty( "hibernate.search.default.directory_provider", RAMDirectoryProvider.class.getName() );
+ cfg.setProperty( org.hibernate.search.Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+ }
+
+ protected boolean recreateSchema() {
+ return true;
+ }
+}
Copied: search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/AnalyzerUtils.java (from rev 19033, search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/util/AnalyzerUtils.java)
===================================================================
--- search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/AnalyzerUtils.java (rev 0)
+++ search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/AnalyzerUtils.java 2010-03-22 13:07:43 UTC (rev 19045)
@@ -0,0 +1,131 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, 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.util;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.Token;
+import org.apache.lucene.analysis.TokenStream;
+import org.slf4j.Logger;
+
+import org.hibernate.search.util.LoggerFactory;
+
+/**
+ * Helper class to test analyzers. Taken and modified from <i>Lucene in Action</i>.
+ *
+ * @author Hardy Ferentschik
+ */
+public class AnalyzerUtils {
+
+ public static final Logger log = LoggerFactory.make();
+
+ public static Token[] tokensFromAnalysis(Analyzer analyzer, String field, String text) throws IOException {
+ TokenStream stream = analyzer.tokenStream( field, new StringReader( text ) );
+ List<Token> tokenList = new ArrayList<Token>();
+ Token reusableToken = new Token();
+ while ( true ) {
+
+ Token token = stream.next( reusableToken );
+ if ( token == null ) {
+ break;
+ }
+
+ tokenList.add( ( Token ) token.clone() );
+ }
+
+ return tokenList.toArray( new Token[tokenList.size()] );
+ }
+
+ public static void displayTokens(Analyzer analyzer, String field, String text) throws IOException {
+ Token[] tokens = tokensFromAnalysis( analyzer, field, text );
+
+ for ( Token token : tokens ) {
+ log.debug( "[" + getTermText( token ) + "] " );
+ }
+ }
+
+ public static void displayTokensWithPositions(Analyzer analyzer, String field, String text) throws IOException {
+ Token[] tokens = tokensFromAnalysis( analyzer, field, text );
+
+ int position = 0;
+
+ for ( Token token : tokens ) {
+ int increment = token.getPositionIncrement();
+
+ if ( increment > 0 ) {
+ position = position + increment;
+ System.out.println();
+ System.out.print( position + ": " );
+ }
+
+ log.debug( "[" + getTermText( token ) + "] " );
+ }
+ }
+
+ public static void displayTokensWithFullDetails(Analyzer analyzer, String field, String text) throws IOException {
+ Token[] tokens = tokensFromAnalysis( analyzer, field, text );
+ StringBuilder builder = new StringBuilder();
+ int position = 0;
+
+ for ( Token token : tokens ) {
+ int increment = token.getPositionIncrement();
+
+ if ( increment > 0 ) {
+ position = position + increment;
+ builder.append( "\n" ).append( position ).append( ": " );
+ }
+
+ builder.append( "[" )
+ .append( getTermText( token ) )
+ .append( ":" )
+ .append( token.startOffset() )
+ .append( "->" )
+ .append(
+ token.endOffset()
+ )
+ .append( ":" )
+ .append( token.type() )
+ .append( "] " );
+ log.debug( builder.toString() );
+ }
+ }
+
+ public static void assertTokensEqual(Token[] tokens, String[] strings) {
+ Assert.assertEquals( strings.length, tokens.length );
+
+ for ( int i = 0; i < tokens.length; i++ ) {
+ Assert.assertEquals( "index " + i, strings[i], getTermText( tokens[i] ) );
+ }
+ }
+
+ public static String getTermText(Token token) {
+ return new String( token.termBuffer(), 0, token.termLength() );
+ }
+}
Copied: search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/FullTextSessionBuilder.java (from rev 19033, search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/util/FullTextSessionBuilder.java)
===================================================================
--- search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/FullTextSessionBuilder.java (rev 0)
+++ search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/FullTextSessionBuilder.java 2010-03-22 13:07:43 UTC (rev 19045)
@@ -0,0 +1,158 @@
+/* $Id$
+ *
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, 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.util;
+
+import java.io.File;
+
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.AnnotationConfiguration;
+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.
+ * This is meant to use only ram-based index and databases, for those test
+ * which need to use several differently configured SessionFactories.
+ *
+ * @author Sanne Grinovero
+ * @author Hardy Ferentschik
+ */
+public class FullTextSessionBuilder {
+
+ private static final Logger log = org.hibernate.search.util.LoggerFactory.make();
+
+ private static final File indexDir;
+
+ private AnnotationConfiguration cfg;
+ private SessionFactory sessionFactory;
+ private boolean usingFileSystem = false;
+
+ 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" );
+
+ //cache:
+ cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
+ cfg.setProperty( Environment.CACHE_PROVIDER,
+ org.hibernate.cache.HashtableCacheProvider.class.getCanonicalName() );
+ cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
+
+ //search specific:
+ cfg.setProperty( org.hibernate.search.Environment.ANALYZER_CLASS,
+ StopAnalyzer.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() );
+ usingFileSystem = false;
+ }
+ else {
+ cfg.setProperty( "hibernate.search.default.directory_provider",
+ FSDirectoryProvider.class.getName() );
+ usingFileSystem = true;
+ }
+ return this;
+ }
+
+ /**
+ * Override before building any parameter, or add new ones.
+ * @param key Property name.
+ * @param value Property value.
+ * @return the same builder (this).
+ */
+ public FullTextSessionBuilder setProperty(String key, String value) {
+ cfg.setProperty( key, value );
+ return this;
+ }
+
+ /**
+ * Adds classes to the SessionFactory being built.
+ * @param annotatedClass The annotated class to add to the configuration.
+ * @return the same builder (this)
+ */
+ public FullTextSessionBuilder addAnnotatedClass(Class annotatedClass) {
+ cfg.addAnnotatedClass( annotatedClass );
+ return this;
+ }
+
+ /**
+ * @return a new FullTextSession based upon the built configuration.
+ */
+ public FullTextSession openFullTextSession() {
+ if ( sessionFactory == null ) {
+ build();
+ }
+ Session session = sessionFactory.openSession();
+ return Search.getFullTextSession( session );
+ }
+
+ /**
+ * Closes the SessionFactory.
+ * Make sure you close all sessions first
+ */
+ public void close() {
+ if ( sessionFactory == null ) {
+ throw new java.lang.IllegalStateException( "sessionFactory not yet built" );
+ }
+ sessionFactory.close();
+ if ( usingFileSystem ) {
+ FileHelper.delete( indexDir );
+ }
+ sessionFactory = null;
+ }
+
+ /**
+ * Builds the sessionFactory as configured so far.
+ */
+ public FullTextSessionBuilder build() {
+ sessionFactory = cfg.buildSessionFactory();
+ return this;
+ }
+
+}
Copied: search/trunk/hibernate-search-testing/src/main/java/org/hibernate/search/test/util/textbuilder (from rev 19033, search/trunk/hibernate-search/src/test/java/org/hibernate/search/test/util/textbuilder)
Modified: search/trunk/pom.xml
===================================================================
--- search/trunk/pom.xml 2010-03-22 12:45:33 UTC (rev 19044)
+++ search/trunk/pom.xml 2010-03-22 13:07:43 UTC (rev 19045)
@@ -38,6 +38,7 @@
<modules>
<module>hibernate-search</module>
+ <module>hibernate-search-testing</module>
<module>hibernate-search-archetype</module>
</modules>
@@ -280,7 +281,7 @@
</dependencyManagement>
<build>
- <defaultGoal>test</defaultGoal>
+ <defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -307,25 +308,6 @@
</archive>
</configuration>
</plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-source-plugin</artifactId>
- <version>2.1</version>
- <executions>
- <execution>
- <id>attach-sources</id>
- <phase>verify</phase>
- <goals>
- <goal>jar-no-fork</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-javadoc-plugin</artifactId>
- <version>2.5</version>
- </plugin>
</plugins>
<pluginManagement>
<plugins>
@@ -394,6 +376,20 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.5</version>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ <version>2.1</version>
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <phase>verify</phase>
+ <goals>
+ <goal>jar-no-fork</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</pluginManagement>
</build>
@@ -408,8 +404,9 @@
</repository>
<snapshotRepository>
<id>snapshots.jboss.org</id>
- <name>JBoss Snapshot Repository</name>
- <url>dav:https://snapshots.jboss.org/maven2</url>
+ <name>JBoss Snapshot Repository</name>
+ <url>file:///Users/hardy/tmp/snapshot</url>
+ <!--url>dav:https://snapshots.jboss.org/maven2</url-->
</snapshotRepository>
</distributionManagement>
More information about the hibernate-commits
mailing list