Author: hardy.ferentschik
Date: 2008-10-29 12:52:19 -0400 (Wed, 29 Oct 2008)
New Revision: 15438
Added:
search/trunk/src/test/org/hibernate/search/test/worker/duplication/
search/trunk/src/test/org/hibernate/search/test/worker/duplication/EmailAddress.java
search/trunk/src/test/org/hibernate/search/test/worker/duplication/Person.java
search/trunk/src/test/org/hibernate/search/test/worker/duplication/SpecialPerson.java
search/trunk/src/test/org/hibernate/search/test/worker/duplication/WorkDuplicationTest.java
Log:
Added test case for HSEARCH-257
Added:
search/trunk/src/test/org/hibernate/search/test/worker/duplication/EmailAddress.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/worker/duplication/EmailAddress.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/worker/duplication/EmailAddress.java 2008-10-29
16:52:19 UTC (rev 15438)
@@ -0,0 +1,57 @@
+// $Id:$
+package org.hibernate.search.test.worker.duplication;
+
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Store;
+
+/**
+ * Test entity for HSEARCH-257.
+ *
+ * @author Hardy Ferentschik
+ */
+@Entity
+public class EmailAddress implements Serializable {
+ @Id
+ @GeneratedValue
+ @DocumentId
+ private int id;
+
+ private boolean isDefaultAddress;
+
+ @Field(store = Store.YES, index = Index.NO)
+ private String address;
+
+ public EmailAddress() {
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getAddress() {
+ return address;
+ }
+
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ public boolean isDefaultAddress() {
+ return isDefaultAddress;
+ }
+
+ public void setDefaultAddress(boolean isDefault) {
+ isDefaultAddress = isDefault;
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/worker/duplication/Person.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/worker/duplication/Person.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/worker/duplication/Person.java 2008-10-29
16:52:19 UTC (rev 15438)
@@ -0,0 +1,82 @@
+// $Id:$
+package org.hibernate.search.test.worker.duplication;
+
+import javax.persistence.DiscriminatorColumn;
+import javax.persistence.DiscriminatorType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.Table;
+import javax.persistence.OneToOne;
+import javax.persistence.JoinColumn;
+import javax.persistence.CascadeType;
+import javax.persistence.FetchType;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+
+/**
+ * Test entity for HSEARCH-257.
+ *
+ * @author Marina Vatkina
+ * @author Hardy Ferentschik
+ */
+@Entity
+@Table
+@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
+@DiscriminatorColumn(name = "DISC", discriminatorType =
DiscriminatorType.STRING)
+public class Person {
+
+ @Id
+ @GeneratedValue
+ @DocumentId
+ private int id;
+
+ @Field(index = Index.TOKENIZED, name = "Content")
+ private String name;
+
+ @OneToOne(fetch = FetchType.EAGER, cascade = {
+ CascadeType.MERGE,
+ CascadeType.PERSIST
+ })
+ @JoinColumn(name = "DEFAULT_EMAILADDRESS_FK")
+ private EmailAddress defaultEmailAddress;
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * This function return the value of defaultEmailAddress.
+ *
+ * @return the defaultEmailAddress
+ */
+
+ public EmailAddress getDefaultEmailAddress() {
+ return defaultEmailAddress;
+ }
+
+ /**
+ * This function sets the value of the defaultEmailAddress.
+ *
+ * @param defaultEmailAddress the defaultEmailAddress to set
+ */
+ protected void setDefaultEmailAddress(EmailAddress defaultEmailAddress) {
+ this.defaultEmailAddress = defaultEmailAddress;
+ }
+}
Added:
search/trunk/src/test/org/hibernate/search/test/worker/duplication/SpecialPerson.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/worker/duplication/SpecialPerson.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/worker/duplication/SpecialPerson.java 2008-10-29
16:52:19 UTC (rev 15438)
@@ -0,0 +1,127 @@
+// $Id:$
+package org.hibernate.search.test.worker.duplication;
+
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.CascadeType;
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.JoinColumn;
+import javax.persistence.OneToMany;
+
+import org.hibernate.annotations.Cascade;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+/**
+ * Test entity for HSEARCH-257.
+ *
+ * @author Hardy Ferentschik
+ */
+@Entity
+@Indexed
+@DiscriminatorValue("SpecialPerson")
+public class SpecialPerson extends Person {
+
+ @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
+ @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
+ @JoinColumn(name = "SPECIALPERSON_FK")
+ @IndexedEmbedded
+ private Set<EmailAddress> emailAddressSet = new HashSet<EmailAddress>();
+
+ public Set<EmailAddress> getEmailAddressSet() {
+ return emailAddressSet;
+ }
+
+ public void setEmailAddressSet(Set<EmailAddress> emailAddresses) {
+ EmailAddress defaultVal = getDefaultEmailAddressFromList( emailAddresses );
+
+ super.setDefaultEmailAddress( defaultVal );
+
+ emailAddressSet = emailAddresses;
+ }
+
+ /**
+ * This function add the provided emailAddress to the existing set.
+ *
+ * @param emailAddress EmailAddress to add the the set
+ */
+ public void addEmailAddress(EmailAddress emailAddress) {
+ if ( emailAddress != null ) {
+ if ( emailAddressSet == null ) {
+ emailAddressSet = new HashSet<EmailAddress>();
+ }
+
+ // We cannot add another default address to the list. Check if
+ // default
+ // address has been set before.
+ if ( emailAddress.isDefaultAddress() ) {
+ // Replace old default address with new one.
+ processDefaultEmailAddress( emailAddress, emailAddressSet );
+
+ super.setDefaultEmailAddress( emailAddress );
+ }
+ else {
+ emailAddressSet.add( emailAddress );
+ }
+ }
+ }
+
+ private void processDefaultEmailAddress(EmailAddress defaultVal,
+ Set<EmailAddress> list) {
+ if ( defaultVal != null ) {
+ boolean addToList = true;
+
+ for ( EmailAddress aList : list ) {
+
+ if ( defaultVal.equals( aList ) ) {
+ aList.setDefaultAddress( true );
+ addToList = false;
+ }
+ else if ( aList.isDefaultAddress() ) {
+ // Reset default value.
+ aList.setDefaultAddress( false );
+ }
+ }
+
+ // Add Email Address to the list if list does not contain it.
+ if ( addToList ) {
+ list.add( defaultVal );
+ }
+ }
+ }
+
+ private EmailAddress getDefaultEmailAddressFromList(
+ Set<EmailAddress> list) {
+ EmailAddress address = null;
+ EmailAddress firstAddressInList = null;
+ boolean found = false;
+
+ if ( list != null ) {
+ for ( EmailAddress aList : list ) {
+ address = aList;
+
+ if ( address != null ) {
+ if ( firstAddressInList == null ) {
+ firstAddressInList = address;
+ }
+
+ if ( address.isDefaultAddress() ) {
+ found = true;
+ break;
+ }
+ }
+ }
+
+ if ( !found && firstAddressInList != null ) {
+ // If default address was not found we set the first one as
+ // default.
+ firstAddressInList.setDefaultAddress( true );
+ address = firstAddressInList;
+ }
+ }
+
+ return address;
+ }
+}
Added:
search/trunk/src/test/org/hibernate/search/test/worker/duplication/WorkDuplicationTest.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/worker/duplication/WorkDuplicationTest.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/worker/duplication/WorkDuplicationTest.java 2008-10-29
16:52:19 UTC (rev 15438)
@@ -0,0 +1,91 @@
+// $Id:$
+package org.hibernate.search.test.worker.duplication;
+
+import java.util.List;
+
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.TopDocs;
+
+import org.hibernate.Transaction;
+import org.hibernate.search.FullTextQuery;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.reader.ReaderProvider;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.test.SearchTestCase;
+
+/**
+ * Testcase for HSEARCH-257.
+ */
+public class WorkDuplicationTest extends SearchTestCase {
+
+ /**
+ * This test assures that HSEARCH-257. Before the fix SEarch would issue another
<code>AddLuceneWork</code> after
+ * the <code>DeleteLuceneWork</code>. This lead to the fact that after the
deletion there was still a Lucene document
+ * in the index.
+ *
+ * @throws Exception in case the test fails.
+ */
+ public void testNoWorkDuplication() throws Exception {
+
+ FullTextSession s = org.hibernate.search.Search.getFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ // create new customer
+ SpecialPerson person = new SpecialPerson();
+ person.setName( "Joe Smith" );
+
+ EmailAddress emailAddress = new EmailAddress();
+ emailAddress.setAddress( "foo(a)foobar.com" );
+ emailAddress.setDefaultAddress(true);
+
+ person.addEmailAddress( emailAddress );
+
+ // persist the customer
+ s.persist( person );
+ tx.commit();
+
+ // search if the record made it into the index
+ tx = s.beginTransaction();
+ String searchQuery = "Joe";
+ QueryParser parser = new QueryParser( "Content", new StandardAnalyzer() );
+ Query luceneQuery = parser.parse( searchQuery );
+ FullTextQuery query = s.createFullTextQuery( luceneQuery );
+ List results = query.list();
+ assertTrue( "We should have a hit", results.size() == 1 );
+ tx.commit();
+
+ // Now try to delete
+ tx = s.beginTransaction();
+ int id = person.getId();
+ person = ( SpecialPerson ) s.get( SpecialPerson.class, id );
+ s.delete( person );
+ tx.commit();
+
+ // Search and the record via Lucene directly
+ tx = s.beginTransaction();
+
+ DirectoryProvider directoryProvider = s.getSearchFactory().getDirectoryProviders(
SpecialPerson.class )[0];
+ ReaderProvider readerProvider = s.getSearchFactory().getReaderProvider();
+ IndexReader reader = readerProvider.openReader( directoryProvider );
+ IndexSearcher searcher = new IndexSearcher( reader );
+
+ try {
+ // we have to test using Lucene directly.
+ TopDocs topDocs = searcher.search( luceneQuery, null, 1 );
+ assertTrue( "We should have no hit", topDocs.totalHits == 0 );
+ }
+ finally {
+ readerProvider.closeReader( reader );
+ }
+ tx.commit();
+ s.close();
+ }
+
+ protected Class[] getMappings() {
+ return new Class[] { Person.class, EmailAddress.class, SpecialPerson.class };
+ }
+}
Property changes on:
search/trunk/src/test/org/hibernate/search/test/worker/duplication/WorkDuplicationTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Show replies by date