Permalink
Browse files

HSEARCH-1049 Test cases for the inclusion of an id in the includePath…

…s attribute of IndexedEmbedded
  • Loading branch information...
1 parent 60f3684 commit 7b72c41af19546a1fc8bbe93ab2bf08c1badbc62 @DavideD committed Feb 29, 2012
@@ -0,0 +1,55 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.hibernate.search.test.embedded.path.id;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.DocumentId;
+
+/**
+ * @author "Davide D'Alto"
+ */
+@Entity
+class DocumentEntity {
+
+ @Id
+ @GeneratedValue
+ long id;
+
+ @DocumentId
+ String documentId;
+
+ @OneToOne
+ @ContainedIn
+ EntityC c;
+
+ public DocumentEntity() {
+ }
+
+ public DocumentEntity(String documentId) {
+ this.documentId = documentId;
+ }
+
+}
@@ -0,0 +1,54 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.hibernate.search.test.embedded.path.id;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+/**
+ * @author Davide D'Alto
+ */
+@Entity
+@Indexed
+class EntityA {
+
+ @Id
+ @GeneratedValue
+ public int id;
+
+ @OneToOne
+ @IndexedEmbedded(depth = 0, includePaths = { "indexed.id", "indexed.document.documentId" })
+ public EntityB b;
+
+ public EntityA() {
+ }
+
+ public EntityA(EntityB b) {
+ this.b = b;
+ this.b.a = this;
+ }
+
+}
@@ -0,0 +1,66 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.hibernate.search.test.embedded.path.id;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+/**
+ * @author Davide D'Alto
+ */
+@Entity
+class EntityB {
+
+ @Id
+ @GeneratedValue
+ public int id;
+
+ @OneToOne
+ @ContainedIn
+ public EntityA a;
+
+ @OneToOne
+ @IndexedEmbedded
+ public EntityC indexed;
+
+ @OneToOne
+ @IndexedEmbedded
+ public EntityC skipped;
+
+ public EntityB() {
+ }
+
+ public EntityB(EntityC indexed, EntityC skipped) {
+ this.indexed = indexed;
+ indexed.b = this;
+
+ if ( skipped != null ) {
+ this.skipped = skipped;
+ skipped.b = this;
+ }
+ }
+
+}
@@ -0,0 +1,66 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.hibernate.search.test.embedded.path.id;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+/**
+ * @author Davide D'Alto
+ */
+@Entity
+class EntityC {
+
+ @Id
+ public String id;
+
+ @OneToOne(mappedBy = "indexed")
+ @ContainedIn
+ public EntityB b;
+
+ @OneToOne(mappedBy = "skipped")
+ @ContainedIn
+ public EntityB b2;
+
+ @OneToOne
+ @IndexedEmbedded
+ public DocumentEntity document;
+
+ @Field
+ public String field;
+
+ @Field
+ public String skipped = "skipped";
+
+ public EntityC() {
+ }
+
+ public EntityC(String id, String indexed) {
+ this.id = id;
+ this.field = indexed;
+ }
+
+}
@@ -0,0 +1,129 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
+ * as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * 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,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package org.hibernate.search.test.embedded.path.id;
+
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.lucene.search.Query;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.SearchException;
+import org.hibernate.search.query.dsl.QueryBuilder;
+import org.hibernate.search.test.SearchTestCase;
+
+/**
+ * {@link org.hibernate.search.annotations.IndexedEmbedded#includePaths()}
+ * should consider fields annotated with {@link javax.persistence.Id}
+ * or {@link org.hibernate.search.annotations.DocumentId}.
+ *
+ * @author Davide D'Alto
+ */
+public class IdIncludedInPathCaseEmbeddedTest extends SearchTestCase {
+
+ private Session s = null;
+ private EntityA entityA = null;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ EntityC indexedC = new EntityC( "indexedId", "indexed" );
+ EntityC skippedC = new EntityC( "skippedId", "indexed" );
+
+ DocumentEntity documentEntity = new DocumentEntity( "indexedDocumentId" );
+ documentEntity.c = indexedC;
+ indexedC.document = documentEntity;
+
+ EntityB indexedB = new EntityB( indexedC, skippedC );
+
+ entityA = new EntityA( indexedB );
+ s = openSession();
+ persistEntity( s, documentEntity, indexedC, skippedC, indexedB, entityA );
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ s.clear();
+
+ deleteAll( s, getAnnotatedClasses() );
+ s.close();
+ super.tearDown();
+ }
+
+ public void testIdAttributeIndexedIfInPath() throws Exception {
+ List<EntityA> result = search( s, "b.indexed.id", "indexedId" );
+
+ Assert.assertEquals( 1, result.size() );
+ Assert.assertEquals( entityA.id, result.get( 0 ).id );
+ }
+
+ public void testDocumentIdIsIndexedIfInPath() throws Exception {
+ List<EntityA> result = search( s, "b.indexed.document.documentId", "indexedDocumentId" );
+
+ Assert.assertEquals( 1, result.size() );
+ Assert.assertEquals( entityA.id, result.get( 0 ).id );
+ }
+
+ public void testEmbeddedNotIndexedIfNotInPath() throws Exception {
+ try {
+ search( s, "b.skipped.skippedId", "skippedId" );
+ fail( "Should not index embedded property if not in path and not in depth limit" );
+ }
+ catch ( SearchException e ) {
+ }
+ }
+
+ private List<EntityA> search(Session s, String field, String value) {
+ FullTextSession session = Search.getFullTextSession( s );
+ QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( EntityA.class ).get();
+ Query query = queryBuilder.keyword().onField( field ).matching( value ).createQuery();
+ @SuppressWarnings("unchecked")
+ List<EntityA> result = session.createFullTextQuery( query ).list();
+ return result;
+ }
+
+ private void deleteAll(Session s, Class<?>... classes) {
+ Transaction tx = s.beginTransaction();
+ for ( Class<?> each : classes ) {
+ List<?> list = s.createCriteria( each ).list();
+ for ( Object object : list ) {
+ s.delete( object );
+ }
+ }
+ tx.commit();
+ }
+
+ private void persistEntity(Session s, Object... entities) {
+ Transaction tx = s.beginTransaction();
+ for ( Object entity : entities ) {
+ s.persist( entity );
+ }
+ tx.commit();
+ }
+
+ @Override
+ protected Class<?>[] getAnnotatedClasses() {
+ return new Class<?>[] { EntityA.class, EntityB.class, EntityC.class, DocumentEntity.class };
+ }
+}

0 comments on commit 7b72c41

Please sign in to comment.