[MNT-23094] Prevent ArrayIndexOutOfBoundsException when sorting by empty field

This commit is contained in:
Tiago Salvado
2022-07-05 11:22:44 +01:00
parent 318dd573b9
commit 421cad511e
2 changed files with 21 additions and 1 deletions
@@ -179,7 +179,14 @@ public class AlfrescoCollatableTextFieldType extends StrField
else if (withLocale.startsWith("\u0000"))
{
String[] parts = withLocale.split("\u0000");
return parts[2];
if (parts != null && parts.length == 3)
{
return parts[2];
}
else
{
return withLocale;
}
}
else
{
@@ -173,4 +173,17 @@ public class AlfrescoCollatableTextFieldTypeTest
verify(mockCollator).compare("NotNull1", "NotNull2");
assertEquals("Expected result to be obtained from collator.", comparisonResult, result);
}
@Test
public void testMNT23094()
{
// Set up the document to have an encoded value for the field.
when(mockDocTerms.get(DOC)).thenReturn(new BytesRef("\u0000"));
when(mockDocsWithField.get(DOC)).thenReturn(false);
// Call the method under test.
textSortFieldComparator.compareBottom(DOC);
verify(mockCollator).compare(BOTTOM_STRING, "\u0000");
}
}