Author: epbernard
Date: 2006-09-06 14:42:50 -0400 (Wed, 06 Sep 2006)
New Revision: 10463
Added:
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Clock.java
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Document.java
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/LuceneTest.java
Log:
lucene test
Added: trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Clock.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Clock.java 2006-09-06
16:27:04 UTC (rev 10462)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Clock.java 2006-09-06
18:42:50 UTC (rev 10463)
@@ -0,0 +1,41 @@
+//$Id: $
+package org.hibernate.lucene.test;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.hibernate.lucene.Indexed;
+import org.hibernate.lucene.Text;
+import org.hibernate.lucene.Keyword;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Indexed
+public class Clock {
+ private Integer id;
+ private String brand;
+
+ public Clock(Integer id, String brand) {
+ this.id = id;
+ this.brand = brand;
+ }
+
+ @Text public String getBrand() {
+ return brand;
+ }
+
+ public void setBrand(String brand) {
+ this.brand = brand;
+ }
+
+ @Id @Keyword
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+}
Copied: trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Document.java (from
rev 10014, trunk/HibernateExt/metadata/src/test/org/hibernate/test/lucene/Document.java)
===================================================================
---
trunk/HibernateExt/metadata/src/test/org/hibernate/test/lucene/Document.java 2006-06-12
16:56:27 UTC (rev 10014)
+++
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Document.java 2006-09-06
18:42:50 UTC (rev 10463)
@@ -0,0 +1,72 @@
+//$Id$
+package org.hibernate.lucene.test;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+
+import org.hibernate.lucene.Indexed;
+import org.hibernate.lucene.Keyword;
+import org.hibernate.lucene.Text;
+import org.hibernate.lucene.Unstored;
+import org.hibernate.lucene.Boost;
+
+@Entity
+@Indexed(index = "Documents")
+public class Document {
+ private Long id;
+ private String title;
+ private String summary;
+ private String text;
+
+ Document() {
+ }
+
+ public Document(String title, String summary, String text) {
+ super();
+ this.summary = summary;
+ this.text = text;
+ this.title = title;
+ }
+
+ @Id
+ @GeneratedValue
+ @Keyword(id = true)
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ @Text
+ @Boost(2)
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ @Unstored(name = "Abstract")
+ public String getSummary() {
+ return summary;
+ }
+
+ public void setSummary(String summary) {
+ this.summary = summary;
+ }
+
+ @Lob
+ @Unstored
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+}
Property changes on:
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/Document.java
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Copied: trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/LuceneTest.java
(from rev 10014,
trunk/HibernateExt/metadata/src/test/org/hibernate/test/lucene/LuceneTest.java)
===================================================================
---
trunk/HibernateExt/metadata/src/test/org/hibernate/test/lucene/LuceneTest.java 2006-06-12
16:56:27 UTC (rev 10014)
+++
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/LuceneTest.java 2006-09-06
18:42:50 UTC (rev 10463)
@@ -0,0 +1,191 @@
+//$Id$
+package org.hibernate.lucene.test;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.hibernate.Session;
+import org.hibernate.event.PostDeleteEventListener;
+import org.hibernate.event.PostInsertEventListener;
+import org.hibernate.event.PostUpdateEventListener;
+import org.hibernate.lucene.Environment;
+import org.hibernate.lucene.event.LuceneEventListener;
+import org.hibernate.test.HATest;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Configuration;
+import org.testng.annotations.Test;
+
+/**
+ * @author Gavin King
+ */
+@Test(groups="lucene")
+public class LuceneTest extends HATest {
+
+
+ @Configuration(beforeTestClass = true)
+ protected void setUpIndex() throws Exception {
+ File sub = getBaseIndexDir();
+ sub.mkdir();
+ super.setUp();
+ }
+
+ private File getBaseIndexDir() {
+ File current = new File( "." );
+ File sub = new File( current, "indextemp" );
+ return sub;
+ }
+
+ @Configuration(afterTestClass = true)
+ protected void tearDownIndex() throws Exception {
+ File sub = getBaseIndexDir();
+ delete( sub );
+ }
+
+ private void delete(File sub) {
+ if ( sub.isDirectory() ) {
+ for ( File file : sub.listFiles() ) {
+ delete( file );
+ }
+ sub.delete();
+ }
+ else {
+ sub.delete();
+ }
+ }
+
+ @Test
+ public void testEventIntegration() throws Exception {
+
+
+ Session s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.persist(
+ new Document( "Hibernate in Action", "Object/relational mapping with
Hibernate", "blah blah blah" )
+ );
+ s.getTransaction().commit();
+ s.close();
+ IndexReader reader = IndexReader.open( new File( getBaseIndexDir(),
"Documents" ) );
+ try {
+ int num = reader.numDocs();
+ assertEquals( 1, num );
+ TermDocs docs = reader.termDocs( new Term( "Abstract", "Hibernate"
) );
+ org.apache.lucene.document.Document doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ docs = reader.termDocs( new Term( "Title", "Action" ) );
+ doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ assertEquals( "1", doc.getField( "id" ).stringValue() );
+ }
+ finally {
+ reader.close();
+ }
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ Document entity = (Document) s.get( Document.class, new Long( 1 ) );
+ entity.setSummary( "Object/relational mapping with EJB3" );
+ s.persist( new Document( "Seam in Action", "", "blah blah blah
blah" ) );
+ s.getTransaction().commit();
+ s.close();
+
+ reader = IndexReader.open( new File( getBaseIndexDir(), "Documents" ) );
+ try {
+ int num = reader.numDocs();
+ assertEquals( 2, num );
+ TermDocs docs = reader.termDocs( new Term( "Abstract", "EJB3" )
);
+ org.apache.lucene.document.Document doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ }
+ finally {
+ reader.close();
+ }
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.delete( entity );
+ s.getTransaction().commit();
+ s.close();
+
+ reader = IndexReader.open( new File( getBaseIndexDir(), "Documents" ) );
+ try {
+ int num = reader.numDocs();
+ assertEquals( 1, num );
+ TermDocs docs = reader.termDocs( new Term( "Title", "Seam" ) );
+ org.apache.lucene.document.Document doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ assertEquals( "2", doc.getField( "id" ).stringValue() );
+ }
+ finally {
+ reader.close();
+ }
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.delete( s.createCriteria( Document.class ).uniqueResult() );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ @Test(dependsOnMethods = "testEventIntegration")
+ public void testBoost() throws Exception {
+ Session s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.persist(
+ new Document( "Hibernate in Action", "Object and Relational",
"blah blah blah" )
+ );
+ s.persist(
+ new Document( "Object and Relational", "Hibernate in Action",
"blah blah blah" )
+ );
+ s.getTransaction().commit();
+ s.close();
+
+ IndexSearcher searcher = new IndexSearcher( new File( getBaseIndexDir(),
"Documents" ).getCanonicalPath() );
+ try {
+ QueryParser qp = new QueryParser( "id", new StandardAnalyzer() );
+ Query query = qp.parse( "title:Action OR Abstract:Action" );
+ Hits hits = searcher.search( query );
+ assertEquals( 2, hits.length() );
+ assertTrue( hits.score( 0 ) == 2 * hits.score( 1 ) );
+ assertEquals( "Hibernate in Action", hits.doc( 0 ).get( "title" )
);
+ }
+ finally {
+ if ( searcher != null ) searcher.close();
+ }
+
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ List list = s.createQuery( "from Document" ).list();
+ for ( Document document : (List<Document>) list ) {
+ s.delete( document );
+ }
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ protected Class[] getMappings() {
+ return new Class[]{Document.class};
+ }
+
+ protected void configure(org.hibernate.cfg.Configuration cfg) {
+ File sub = getBaseIndexDir();
+ cfg.setProperty( "hibernate.lucene.default.indexBase", sub.getAbsolutePath()
);
+ cfg.setProperty( "hibernate.lucene.Clock.directory_provider",
"RamDirectoryProvider" );
+ cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+ LuceneEventListener del = new LuceneEventListener();
+ cfg.getEventListeners().setPostCommitDeleteEventListeners( new
PostDeleteEventListener[]{del} );
+ cfg.getEventListeners().setPostCommitUpdateEventListeners( new
PostUpdateEventListener[]{del} );
+ cfg.getEventListeners().setPostCommitInsertEventListeners( new
PostInsertEventListener[]{del} );
+ }
+
+}
+
Property changes on:
trunk/HibernateExt/metadata/src/test/org/hibernate/lucene/test/LuceneTest.java
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:keywords
+ Author Date Id Revision
Name: svn:eol-style
+ native
Show replies by date