Skip to content

Commit

Permalink
HSEARCH-2188 Cleanup and add tests for NumericFieldUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
gfouquet authored and gsmet committed Apr 11, 2016
1 parent 3662880 commit a5ad4f5
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 10 deletions.
Expand Up @@ -117,16 +117,15 @@ public static boolean requiresNumericRangeQuery(Object value) {
if ( value == null ) {
return false;
}
final Class<?> numericClass = value.getClass();
return Double.class.isAssignableFrom( numericClass ) ||
Long.class.isAssignableFrom( numericClass ) ||
Integer.class.isAssignableFrom( numericClass ) ||
Float.class.isAssignableFrom( numericClass ) ||
Calendar.class.isAssignableFrom( numericClass ) ||
return value instanceof Double ||
value instanceof Long ||
value instanceof Integer ||
value instanceof Float ||
value instanceof Calendar ||
( JavaTimeBridgeProvider.isActive() && (
java.time.Instant.class.isAssignableFrom( numericClass ) ||
java.time.Year.class.isAssignableFrom( numericClass ) ||
java.time.Duration.class.isAssignableFrom( numericClass )
));
value instanceof java.time.Instant ||
value instanceof java.time.Year ||
value instanceof java.time.Duration
));
}
}
@@ -0,0 +1,118 @@
/*
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.test.bridge.util.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import org.hibernate.search.bridge.util.impl.NumericFieldUtils;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.testsupport.TestForIssue;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* @author Gregory Fouquet
*/
@TestForIssue(jiraKey = "HSEARCH-2188")
public class NumericFieldUtilsTest {
@Test
public void testShouldRequireNumericRangeQuery() {
Object[] numericValues = {
10,
20L,
30.5f,
40.5d,
nowCalendar()
};

for ( Object val : numericValues ) {
assertTrue(
"Value of type " + val.getClass() + " should require numeric range query",
NumericFieldUtils.requiresNumericRangeQuery( val )
);
}
}

private Calendar nowCalendar() {
return GregorianCalendar.getInstance( TimeZone.getTimeZone( "UTC" ), Locale.ENGLISH );
}

@Test
public void testShouldNotRequireNumericRangeQuery() {
assertFalse(
"null value should not require numeric range query",
NumericFieldUtils.requiresNumericRangeQuery( null )
);

Object[] nonNumericValues = {"", new Date(), BigDecimal.ONE};

for ( Object val : nonNumericValues ) {
assertFalse(
"Value of type '" + val.getClass() + "' should not require numeric range query",
NumericFieldUtils.requiresNumericRangeQuery( val )
);
}
}

@Test
public void testShouldCreateExactMatchQuery() {
Object[] numericValues = {
10,
20L,
30.5f,
40.5d,
nowCalendar(),
new Date()
};

for ( Object val : numericValues ) {
try {
NumericFieldUtils.createExactMatchQuery( "numField", val );
}
catch (SearchException e) {
fail( "Should create exact match query for value of type " + val.getClass() );
}
}
}

@Test
public void testShouldNotCreateExactMatchQuery() {
SearchException nullEx = null;
try {
NumericFieldUtils.createExactMatchQuery( "nonNumField", null );
}
catch (SearchException e) {
nullEx = e;
}

assertNotNull( "Should not create exact match query for null value", nullEx );

Object[] nonNumericValues = {"", BigDecimal.ONE};

for ( Object val : nonNumericValues ) {
SearchException caught = null;
try {
NumericFieldUtils.createExactMatchQuery( "nonNumField", val );
}
catch (SearchException e) {
caught = e;
}

assertNotNull( "Should not create exact match query for value of type " + val.getClass(), caught );
}
}
}

0 comments on commit a5ad4f5

Please sign in to comment.