Skip to content

Commit

Permalink
HSEARCH-2470 Test the lack of support for projection on one-way field…
Browse files Browse the repository at this point in the history
… bridges
  • Loading branch information
yrodiere authored and Sanne committed Nov 23, 2016
1 parent 73a8d51 commit ff2a9d4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Expand Up @@ -129,10 +129,10 @@ public static void populateResult(String fieldName,
}
else {
if ( store == Store.NO ) {
throw new SearchException( "Projecting an unstored field: " + fieldName );
throw log.projectingNonStoredField( fieldName );
}
else {
throw new SearchException( "FieldBridge is not a TwoWayFieldBridge: " + fieldBridge.getClass() );
throw log.projectingFieldWithoutTwoWayFieldBridge( fieldName, fieldBridge.getClass() );
}
}
}
Expand Down
Expand Up @@ -994,4 +994,10 @@ public interface Log extends BasicLogger {
+ "gracefully for index '%s'.")
void timedOutWaitingShutdownOfReaderProvider(String indexName);

@Message(id = 323, value = "The field '%1$s' is not stored.")
SearchException projectingNonStoredField(String fieldName);

@Message(id = 324, value = "The fieldBridge for field '%1$s' is an instance of '%2$s', which does not implement TwoWayFieldBridge. Projected fields must have a TwoWayFieldBridge.")
SearchException projectingFieldWithoutTwoWayFieldBridge(String fieldName, Class<?> fieldBridgeClass);

}
Expand Up @@ -21,10 +21,12 @@
import org.hibernate.search.backend.spi.Work;
import org.hibernate.search.backend.spi.WorkType;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.StringBridge;
import org.hibernate.search.bridge.TwoWayFieldBridge;
import org.hibernate.search.bridge.builtin.LongBridge;
import org.hibernate.search.engine.ProjectionConstants;
import org.hibernate.search.engine.integration.impl.ExtendedSearchIntegrator;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.hibernate.search.query.engine.spi.EntityInfo;
import org.hibernate.search.testsupport.TestForIssue;
Expand All @@ -36,6 +38,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* This test verifies the correct projection rules (like which FieldBridge)
Expand All @@ -50,6 +53,9 @@
@TestForIssue(jiraKey = "HSEARCH-1786")
public class ProjectionConversionTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Rule
public SearchFactoryHolder sfHolder = new SearchFactoryHolder( ExampleEntity.class );

Expand All @@ -60,13 +66,17 @@ public void storeTestData() {
entity.id = 1l;
entity.someInteger = 5;
entity.longEncodedAsText = 20l;
entity.unstoredField = "unstoredField";
entity.customBridgedKeyword = "lowercase-keyword";
entity.customOneWayBridgedKeyword = "lowercase-keyword";

ExampleEntity embedded = new ExampleEntity();
embedded.id = 2l;
embedded.someInteger = 6;
embedded.longEncodedAsText = 21l;
embedded.unstoredField = "unstoredFieldEmbedded";
embedded.customBridgedKeyword = "another-lowercase-keyword";
embedded.customOneWayBridgedKeyword = "another-lowercase-keyword";

ConflictingMappedType second = new ConflictingMappedType();
second.id = "a string";
Expand Down Expand Up @@ -106,11 +116,30 @@ public void projectingUnknownField() {
projectionTestHelper( "someNonExistingField", null );
}

@Test
public void projectingUnstoredField() {
thrown.expect( SearchException.class );
thrown.expectMessage( "HSEARCH000323" );
thrown.expectMessage( "unstoredField" );

projectionTestHelper( "unstoredField", null );
}

@Test
public void projectionWithCustomBridge() {
projectionTestHelper( "customBridgedKeyword", "lowercase-keyword" );
}

@Test
public void projectionWithCustomOneWayBridge() {
thrown.expect( SearchException.class );
thrown.expectMessage( "HSEARCH000324" );
thrown.expectMessage( "customOneWayBridgedKeyword" );
thrown.expectMessage( CustomOneWayBridge.class.getName() );

projectionTestHelper( "customOneWayBridgedKeyword", "lowercase-keyword" );
}

@Test
public void projectingEmbeddedIdByPropertyName() {
projectionTestHelper( "embedded.id", Long.valueOf( 2l ) );
Expand All @@ -126,6 +155,16 @@ public void projectingEmbeddedWithCustomBridge() {
projectionTestHelper( "embedded.customBridgedKeyword", "another-lowercase-keyword" );
}

@Test
public void projectingEmbeddedWithCustomOneWayBridge() {
thrown.expect( SearchException.class );
thrown.expectMessage( "HSEARCH000324" );
thrown.expectMessage( "embedded.customOneWayBridgedKeyword" );
thrown.expectMessage( CustomOneWayBridge.class.getName() );

projectionTestHelper( "embedded.customOneWayBridgedKeyword", "another-lowercase-keyword" );
}

@Test
public void projectingNotIncludedEmbeddedField() {
projectionTestHelper( "embedded.someInteger", null );
Expand Down Expand Up @@ -194,10 +233,17 @@ public static class ExampleEntity {
@Field(store = Store.YES) @FieldBridge(impl = LongBridge.class)
Long longEncodedAsText;

@Field(store = Store.NO)
String unstoredField;

@Field(store = Store.YES) @FieldBridge(impl = CustomTwoWayBridge.class)
String customBridgedKeyword;

@IndexedEmbedded(includePaths = { "id", "stringTypedId", "customBridgedKeyword" }, includeEmbeddedObjectId = true)
@Field(store = Store.YES) @FieldBridge(impl = CustomOneWayBridge.class)
String customOneWayBridgedKeyword;

@IndexedEmbedded(includePaths = { "id", "stringTypedId", "customBridgedKeyword", "customOneWayBridgedKeyword" },
includeEmbeddedObjectId = true)
ExampleEntity embedded;

@IndexedEmbedded(includeEmbeddedObjectId = true)
Expand Down Expand Up @@ -236,4 +282,18 @@ public String objectToString(Object object) {
}

}

public static class CustomOneWayBridge implements org.hibernate.search.bridge.FieldBridge, StringBridge {

@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
luceneOptions.addFieldToDocument( name, String.valueOf( value ).toUpperCase( Locale.ENGLISH ), document );
}

@Override
public String objectToString(Object object) {
return String.valueOf( object );
}

}
}

0 comments on commit ff2a9d4

Please sign in to comment.