From 938d3896f85a27e8a334e78414ebab876351d683 Mon Sep 17 00:00:00 2001 From: Hardy Ferentschik Date: Mon, 3 Nov 2014 21:46:50 +0100 Subject: [PATCH] HSEARCH-1470 Improving error message for HSEARCH000131 and adding explicit tests --- .../search/util/logging/impl/Log.java | 2 +- .../test/spatial/MissingSpatialPOI.java | 71 ++++++++++++++++ .../test/spatial/SpatialIndexingTest.java | 80 ++++++++++++++++--- 3 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 orm/src/test/java/org/hibernate/search/test/spatial/MissingSpatialPOI.java diff --git a/engine/src/main/java/org/hibernate/search/util/logging/impl/Log.java b/engine/src/main/java/org/hibernate/search/util/logging/impl/Log.java index 11e5be912bc..17ca3121164 100644 --- a/engine/src/main/java/org/hibernate/search/util/logging/impl/Log.java +++ b/engine/src/main/java/org/hibernate/search/util/logging/impl/Log.java @@ -420,7 +420,7 @@ public interface Log extends BasicLogger { @Message(id = 128, value = "Interceptor enforces update of index data instead of index operation %2$s on instance of class %1$s") void forceUpdateOnIndexOperationViaInterception(Class entityClass, WorkType type); - @Message(id = 131, value = "The field used for the spatial query is not using SpatialFieldBridge: %1$s.%2$s") + @Message(id = 131, value = "The field '%1$s#%2$s' used for the spatial query is not configured as spatial field. Check the proper use of @Spatial respectively SpatialFieldBridge") SearchException targetedFieldNotSpatial(String className, String fieldName); @Message(id = 133, value = "@ClassBridge implementation '%1$s' should implement either org.hibernate.search.bridge.FieldBridge, org.hibernate.search.bridge.TwoWayStringBridge or org.hibernate.search.bridge.StringBridge") diff --git a/orm/src/test/java/org/hibernate/search/test/spatial/MissingSpatialPOI.java b/orm/src/test/java/org/hibernate/search/test/spatial/MissingSpatialPOI.java new file mode 100644 index 00000000000..162756de185 --- /dev/null +++ b/orm/src/test/java/org/hibernate/search/test/spatial/MissingSpatialPOI.java @@ -0,0 +1,71 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.search.test.spatial; + +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.search.annotations.Field; +import org.hibernate.search.annotations.Index; +import org.hibernate.search.annotations.Indexed; +import org.hibernate.search.annotations.Latitude; +import org.hibernate.search.annotations.Longitude; +import org.hibernate.search.annotations.Store; + +/** + * @author Hardy Ferentschik + */ +@Entity +@Indexed +public class MissingSpatialPOI { + @Id + Integer id; + + @Field(store = Store.YES) + String name; + + @Field(store = Store.YES, index = Index.YES) + String type; + + @Latitude + double latitude; + + @Longitude + double longitude; + + public MissingSpatialPOI(Integer id, String name, double latitude, double longitude, String type) { + this.id = id; + this.name = name; + this.latitude = latitude; + this.longitude = longitude; + this.type = type; + } + + public MissingSpatialPOI() { + } + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + + public String getType() { + return type; + } + +} diff --git a/orm/src/test/java/org/hibernate/search/test/spatial/SpatialIndexingTest.java b/orm/src/test/java/org/hibernate/search/test/spatial/SpatialIndexingTest.java index 3228f676310..d48f1266798 100644 --- a/orm/src/test/java/org/hibernate/search/test/spatial/SpatialIndexingTest.java +++ b/orm/src/test/java/org/hibernate/search/test/spatial/SpatialIndexingTest.java @@ -20,6 +20,7 @@ import org.hibernate.search.FullTextQuery; import org.hibernate.search.FullTextSession; import org.hibernate.search.Search; +import org.hibernate.search.exception.SearchException; import org.hibernate.search.query.dsl.QueryBuilder; import org.hibernate.search.query.dsl.Unit; import org.hibernate.search.spatial.DistanceSortField; @@ -27,6 +28,7 @@ import org.hibernate.search.testsupport.TestForIssue; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Hibernate Search spatial : unit tests on indexing POIs in with Grid and Grid+Distance @@ -59,6 +61,9 @@ public void createAndIndexTestData() throws Exception { fullTextSession.save( new NonGeoPOI( 5, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "" ) ); fullTextSession.save( new NonGeoPOI( 6, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "" ) ); + // MissingSpatialPOI + fullTextSession.save( new MissingSpatialPOI( 1, "Distance to 24,32 : 0", 24.0d, 32.0d, "" ) ); + // Event SimpleDateFormat dateFormat = new SimpleDateFormat( "d M yyyy" ); Date date = dateFormat.parse( "10 9 1976" ); @@ -129,12 +134,12 @@ public void testDistanceProjection() throws Exception { Object[] fourthResult = (Object[]) results.get( 3 ); Object[] fifthResult = (Object[]) results.get( 4 ); Object[] sixthResult = (Object[]) results.get( 5 ); - Assert.assertEquals( ((Double)firstResult[1]), 0.0, 0.0001 ); - Assert.assertEquals( ((Double)secondResult[1]), 10.1582, 0.0001 ); - Assert.assertEquals( ((Double)thirdResult[1]), 11.1195, 0.0001 ); - Assert.assertEquals( ((Double)fourthResult[1]), 15.0636, 0.0001 ); - Assert.assertEquals( ((Double)fifthResult[1]), 22.239, 0.001 ); - Assert.assertEquals( ((Double)sixthResult[1]), 24.446, 0.001 ); + Assert.assertEquals( ( (Double) firstResult[1] ), 0.0, 0.0001 ); + Assert.assertEquals( ( (Double) secondResult[1] ), 10.1582, 0.0001 ); + Assert.assertEquals( ( (Double) thirdResult[1] ), 11.1195, 0.0001 ); + Assert.assertEquals( ( (Double) fourthResult[1] ), 15.0636, 0.0001 ); + Assert.assertEquals( ( (Double) fifthResult[1] ), 22.239, 0.001 ); + Assert.assertEquals( ( (Double) sixthResult[1] ), 24.446, 0.001 ); } @Test @@ -149,7 +154,7 @@ public void testDistanceSort() throws Exception { .within( 100, Unit.KM ).ofLatitude( centerLatitude ).andLongitude( centerLongitude ).createQuery(); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( luceneQuery, POI.class ); - Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" )); + Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); @@ -177,7 +182,7 @@ public void testNonGeoDistanceSort() throws Exception { org.apache.lucene.search.Query luceneQuery = builder.all().createQuery(); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( luceneQuery, NonGeoPOI.class ); - Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" )); + Sort distanceSort = new Sort( new DistanceSortField( centerLatitude, centerLongitude, "location" ) ); hibQuery.setSort( distanceSort ); hibQuery.setProjection( FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE ); hibQuery.setSpatialParameters( centerLatitude, centerLongitude, "location" ); @@ -186,6 +191,46 @@ public void testNonGeoDistanceSort() throws Exception { hibQuery.list(); } + @Test + @TestForIssue(jiraKey = "HSEARCH-1470") + public void testSpatialQueryOnNonSpatialConfiguredEntityThrowsException() throws Exception { + final QueryBuilder builder = fullTextSession.getSearchFactory() + .buildQueryBuilder().forEntity( MissingSpatialPOI.class ).get(); + + try { + builder.spatial() + .onDefaultCoordinates() + .within( 1, Unit.KM ) + .ofLatitude( 0d ) + .andLongitude( 0d ) + .createQuery(); + fail( "Building an invalid spatial query should fail" ); + } + catch (SearchException e) { + assertTrue( "Wrong error message: " + e.getMessage(), e.getMessage().startsWith( "HSEARCH000131" ) ); + } + } + + @Test + @TestForIssue(jiraKey = "HSEARCH-1470") + public void testSpatialQueryOnWrongFieldThrowsException() throws Exception { + final QueryBuilder builder = fullTextSession.getSearchFactory() + .buildQueryBuilder().forEntity( POI.class ).get(); + + try { + builder.spatial() + .onCoordinates( "foo" ) + .within( 1, Unit.KM ) + .ofLatitude( 0d ) + .andLongitude( 0d ) + .createQuery(); + fail( "Building an invalid spatial query should fail" ); + } + catch (SearchException e) { + assertTrue( "Wrong error message " + e.getMessage(), e.getMessage().startsWith( "HSEARCH000131" ) ); + } + } + @Test public void testSpatialAnnotationOnFieldLevel() throws Exception { //Point center = Point.fromDegrees( 24, 31.5 ); // 50.79 km fromBoundingCircle 24.32 @@ -354,15 +399,23 @@ public void testSpatialAnnotationOnClassLevelRangeMode() throws Exception { double endOfTheWorldLatitude = 0.0d; double endOfTheWorldLongitude = 180.0d; - org.apache.lucene.search.Query luceneQuery3 = builder.spatial().onCoordinates( "location" ) - .within( 112, Unit.KM ).ofLatitude( endOfTheWorldLatitude ).andLongitude( endOfTheWorldLongitude ).createQuery(); + org.apache.lucene.search.Query luceneQuery3 = builder.spatial() + .onCoordinates( "location" ) + .within( 112, Unit.KM ) + .ofLatitude( endOfTheWorldLatitude ) + .andLongitude( endOfTheWorldLongitude ) + .createQuery(); org.hibernate.Query hibQuery3 = fullTextSession.createFullTextQuery( luceneQuery3, RangeHotel.class ); List results3 = hibQuery3.list(); Assert.assertEquals( 2, results3.size() ); - org.apache.lucene.search.Query luceneQuery4 = builder.spatial().onCoordinates( "location" ) - .within( 100000, Unit.KM ).ofLatitude( endOfTheWorldLatitude ).andLongitude( endOfTheWorldLongitude ).createQuery(); + org.apache.lucene.search.Query luceneQuery4 = builder.spatial() + .onCoordinates( "location" ) + .within( 100000, Unit.KM ) + .ofLatitude( endOfTheWorldLatitude ) + .andLongitude( endOfTheWorldLongitude ) + .createQuery(); org.hibernate.Query hibQuery4 = fullTextSession.createFullTextQuery( luceneQuery4, RangeHotel.class ); List results4 = hibQuery4.list(); @@ -429,7 +482,8 @@ protected Class[] getAnnotatedClasses() { RangeEvent.class, Restaurant.class, NonGeoPOI.class, - GetterUser.class + GetterUser.class, + MissingSpatialPOI.class }; }