Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-3099 Validate indexNullAs and test negative cases
- Loading branch information
Showing
12 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
engine/src/main/java/org/hibernate/search/engine/cfg/spi/ValidateUtils.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,70 @@ | ||
/* | ||
* 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.engine.cfg.spi; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.time.DateTimeException; | ||
import java.time.ZoneId; | ||
import java.util.UUID; | ||
|
||
import org.hibernate.search.engine.logging.impl.Log; | ||
import org.hibernate.search.util.common.logging.impl.LoggerFactory; | ||
|
||
public final class ValidateUtils { | ||
|
||
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
||
private ValidateUtils() { | ||
// Private constructor, do not use | ||
} | ||
|
||
public static void validateCharacter(String value) { | ||
if ( value != null && value.length() != 1 ) { | ||
throw log.invalidStringForType( value, Character.class, null ); | ||
} | ||
} | ||
|
||
public static void validateEnum(String value, Class<? extends Enum> enumType) { | ||
if ( value == null ) { | ||
return; | ||
} | ||
|
||
try { | ||
Enum.valueOf( enumType, value ); | ||
} | ||
catch (IllegalArgumentException ex) { | ||
throw log.invalidStringForEnum( value, enumType, ex ); | ||
} | ||
} | ||
|
||
public static void validateUUID(String value) { | ||
if ( value == null ) { | ||
return; | ||
} | ||
|
||
try { | ||
UUID.fromString( value ); | ||
} | ||
catch (IllegalArgumentException ex) { | ||
throw log.invalidStringForType( value, UUID.class, ex ); | ||
} | ||
} | ||
|
||
public static void validateZoneId(String value) { | ||
if ( value == null ) { | ||
return; | ||
} | ||
|
||
try { | ||
ZoneId.of( value ); | ||
} | ||
catch (DateTimeException ex) { | ||
throw log.invalidStringForType( value, ZoneId.class, ex ); | ||
} | ||
} | ||
|
||
} |
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
70 changes: 70 additions & 0 deletions
70
...g/hibernate/search/integrationtest/mapper/pojo/mapping/definition/IndexNullAsErrorIT.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,70 @@ | ||
/* | ||
* 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.integrationtest.mapper.pojo.mapping.definition; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.util.Optional; | ||
|
||
import org.hibernate.search.integrationtest.mapper.pojo.testsupport.types.PropertyTypeDescriptor; | ||
import org.hibernate.search.integrationtest.mapper.pojo.testsupport.types.expectations.DefaultValueBridgeExpectations; | ||
import org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper; | ||
import org.hibernate.search.util.common.SearchException; | ||
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock; | ||
import org.hibernate.search.util.impl.test.SubTest; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class IndexNullAsErrorIT<V, F> { | ||
|
||
private static final String FIELD_NAME = DefaultValueBridgeExpectations.TYPE_WITH_VALUE_BRIDGE_FIELD_NAME; | ||
private static final String FIELD_INDEXNULLAS_NAME = DefaultValueBridgeExpectations.TYPE_WITH_VALUE_BRIDGE_FIELD_INDEXNULLAS_NAME; | ||
|
||
@Parameterized.Parameters(name = "{0}") | ||
public static Object[] types() { | ||
return PropertyTypeDescriptor.getAll().stream() | ||
// do not test types that do not have a default value bridge | ||
.filter( type -> type.getDefaultValueBridgeExpectations().isPresent() ) | ||
// TODO do not test primitive types | ||
.filter( type -> type.getDefaultValueBridgeExpectations().get().isNullTranslatedAsNull() ) | ||
.map( type -> new Object[] { type, type.getDefaultValueBridgeExpectations() } ) | ||
.toArray(); | ||
} | ||
|
||
@Rule | ||
public BackendMock backendMock = new BackendMock( "stubBackend" ); | ||
|
||
@Rule | ||
public JavaBeanMappingSetupHelper setupHelper = new JavaBeanMappingSetupHelper( MethodHandles.lookup() ); | ||
|
||
private DefaultValueBridgeExpectations<V, F> expectations; | ||
|
||
public IndexNullAsErrorIT(PropertyTypeDescriptor<V> typeDescriptor, Optional<DefaultValueBridgeExpectations<V, F>> expectations) { | ||
this.expectations = expectations.get(); | ||
} | ||
|
||
@Test | ||
public void testParsingException() { | ||
SubTest.expectException( () -> | ||
setupHelper.withBackendMock( backendMock ).withConfiguration( c -> c | ||
.addEntityType( expectations.getTypeWithValueBridge1() ) | ||
.programmaticMapping() | ||
.type( expectations.getTypeWithValueBridge1() ).indexed() | ||
.property( FIELD_NAME ).genericField( FIELD_NAME ) | ||
.property( FIELD_NAME ).genericField( FIELD_INDEXNULLAS_NAME ).indexNullAs( expectations.getUnparsableNullAsValue() ) | ||
).setup() | ||
) | ||
.assertThrown() | ||
.isInstanceOf( SearchException.class ) | ||
.hasMessageContaining( "HSEARCH0005" ) | ||
.hasMessageContaining( expectations.getTypeWithValueBridge1().getSimpleName() ); | ||
|
||
} | ||
} |
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
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
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
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
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
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
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
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
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