Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-1358 containsCollectionRole needs to verify whether the speci…
…fied role starts with any of the registered collection roles (not just equality match). This is needed for nested embedded collections. Also adding test case.
- Loading branch information
1 parent
eeb0603
commit b987fa6
Showing
6 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2013, 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.event.autoindexembeddable; | ||
|
||
import javax.persistence.Embedded; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
|
||
import org.hibernate.search.annotations.DocumentId; | ||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.Indexed; | ||
import org.hibernate.search.annotations.IndexedEmbedded; | ||
import org.hibernate.search.annotations.Store; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
@Entity | ||
@Indexed | ||
public class Book { | ||
|
||
private Long id; | ||
private String title; | ||
private EmbeddableCategories embeddableCategories; | ||
|
||
@Id | ||
@DocumentId | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@Field(store = Store.YES) | ||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
@Embedded | ||
@IndexedEmbedded | ||
public EmbeddableCategories getEmbeddableCategories() { | ||
if ( embeddableCategories == null ) { | ||
embeddableCategories = new EmbeddableCategories(); | ||
} | ||
return embeddableCategories; | ||
} | ||
|
||
public void setEmbeddableCategories(EmbeddableCategories categories) { | ||
this.embeddableCategories = categories; | ||
} | ||
} | ||
|
||
|
47 changes: 47 additions & 0 deletions
47
orm/src/test/java/org/hibernate/search/test/event/autoindexembeddable/CategoriesBridge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2013, 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.event.autoindexembeddable; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.hibernate.search.bridge.FieldBridge; | ||
import org.hibernate.search.bridge.LuceneOptions; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
public class CategoriesBridge implements FieldBridge { | ||
@Override | ||
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { | ||
@SuppressWarnings("unchecked") | ||
Map<Long, String> categoriesValue = (Map<Long, String>) value; | ||
for ( String s : categoriesValue.values() ) { | ||
luceneOptions.addFieldToDocument( name, s, document ); | ||
} | ||
} | ||
} | ||
|
||
|
61 changes: 61 additions & 0 deletions
61
...c/test/java/org/hibernate/search/test/event/autoindexembeddable/EmbeddableCategories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2013, 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.event.autoindexembeddable; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.persistence.ElementCollection; | ||
import javax.persistence.Embeddable; | ||
import javax.persistence.MapKeyColumn; | ||
|
||
import org.hibernate.search.annotations.Field; | ||
import org.hibernate.search.annotations.FieldBridge; | ||
import org.hibernate.search.annotations.IndexedEmbedded; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
@Embeddable | ||
public class EmbeddableCategories { | ||
|
||
private Map<Long, String> categories; | ||
|
||
@ElementCollection | ||
@IndexedEmbedded | ||
@MapKeyColumn | ||
@Field(bridge = @FieldBridge(impl = CategoriesBridge.class)) | ||
public Map<Long, String> getCategories() { | ||
if ( categories == null ) { | ||
categories = new HashMap<Long, String>(); | ||
} | ||
return categories; | ||
} | ||
|
||
public void setCategories(Map<Long, String> categories) { | ||
this.categories = categories; | ||
} | ||
} | ||
|
||
|
131 changes: 131 additions & 0 deletions
131
...rnate/search/test/event/autoindexembeddable/EventBasedEmbeddableCollectionUpdateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* Copyright (c) 2013, 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.event.autoindexembeddable; | ||
|
||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
|
||
import org.apache.lucene.analysis.standard.StandardAnalyzer; | ||
import org.apache.lucene.queryParser.MultiFieldQueryParser; | ||
import org.apache.lucene.queryParser.ParseException; | ||
import org.apache.lucene.queryParser.QueryParser; | ||
import org.hibernate.search.jpa.FullTextEntityManager; | ||
import org.hibernate.search.jpa.FullTextQuery; | ||
import org.hibernate.search.jpa.Search; | ||
import org.hibernate.search.test.TestConstants; | ||
import org.hibernate.search.test.jpa.JPATestCase; | ||
import org.hibernate.search.test.util.TestForIssue; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Hardy Ferentschik | ||
*/ | ||
@TestForIssue(jiraKey = "HSEARCH-1358") | ||
public class EventBasedEmbeddableCollectionUpdateTest extends JPATestCase { | ||
|
||
private EntityManager entityManager; | ||
|
||
@Before | ||
public void setUp() { | ||
super.setUp(); | ||
entityManager = factory.createEntityManager(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager( entityManager ); | ||
fullTextEntityManager.purgeAll( Book.class ); | ||
fullTextEntityManager.flushToIndexes(); | ||
fullTextEntityManager.close(); | ||
super.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testUpdateOfEmbeddedElementCollectionTriggersIndexUpdate() throws Exception { | ||
indexBookAndEnsureItIsIndexed(); | ||
|
||
// find and update book by adding "Bar" into Embedded ElementCollection | ||
entityManager.getTransaction().begin(); | ||
|
||
Book book = entityManager.find( Book.class, 1234L ); | ||
book.getEmbeddableCategories().getCategories().remove( 12L ); | ||
book.getEmbeddableCategories().getCategories().put( 13L, "Bar" ); | ||
|
||
entityManager.persist( book ); | ||
entityManager.getTransaction().commit(); | ||
|
||
assertEquals( | ||
"Foo should have been removed by indexed update", | ||
0, | ||
search( "embeddableCategories.categories:Foo" ).size() | ||
); | ||
assertEquals( | ||
"Bar should have been added by indexed update", | ||
1, | ||
search( "embeddableCategories.categories:Bar" ).size() | ||
); | ||
} | ||
|
||
@Override | ||
public Class[] getAnnotatedClasses() { | ||
return new Class[] { }; // configured in persistence.xml | ||
} | ||
|
||
private void indexBookAndEnsureItIsIndexed() throws ParseException { | ||
entityManager.getTransaction().begin(); | ||
|
||
Book book = new Book(); | ||
book.setId( 1234L ); | ||
book.getEmbeddableCategories().getCategories().put( 12L, "Foo" ); | ||
|
||
entityManager.persist( book ); | ||
entityManager.getTransaction().commit(); | ||
|
||
assertEquals( | ||
"Foo should have been added during indexing", | ||
1, | ||
search( "embeddableCategories.categories:Foo" ).size() | ||
); | ||
assertEquals( "Bar was not yet added", 0, search( "embeddableCategories.categories:Bar" ).size() ); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private List<Book> search(String searchQuery) throws ParseException { | ||
QueryParser parser = new MultiFieldQueryParser( | ||
TestConstants.getTargetLuceneVersion(), | ||
new String[] { }, | ||
new StandardAnalyzer( TestConstants.getTargetLuceneVersion() ) | ||
); | ||
FullTextQuery query = Search.getFullTextEntityManager( entityManager ) | ||
.createFullTextQuery( parser.parse( searchQuery ), Book.class ); | ||
return (List<Book>) query.getResultList(); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters