Additional ExtractionFn null-handling adjustments.

Followup to comments on #2771.
This commit is contained in:
Gian Merlino 2016-04-01 18:35:24 -07:00
parent 18b9ea62cf
commit e060a9f283
3 changed files with 35 additions and 3 deletions

View File

@ -60,8 +60,13 @@ public class MatchingDimExtractionFn extends DimExtractionFn
@Override
public String apply(String dimValue)
{
Matcher matcher = pattern.matcher(Strings.nullToEmpty(dimValue));
return matcher.find() ? Strings.emptyToNull(dimValue) : null;
if (Strings.isNullOrEmpty(dimValue)) {
// We'd return null whether or not the pattern matched
return null;
}
Matcher matcher = pattern.matcher(dimValue);
return matcher.find() ? dimValue : null;
}
@JsonProperty("expr")

View File

@ -104,6 +104,20 @@ public class JavaScriptExtractionFnTest
}
}
@Test
public void testJavascriptIsNull()
{
String function = "function(x) { if (x == null) { return 'yes'; } else { return 'no' } }";
ExtractionFn extractionFn = new JavaScriptExtractionFn(function, false);
Assert.assertEquals("yes", extractionFn.apply((String) null));
Assert.assertEquals("yes", extractionFn.apply((Object) null));
Assert.assertEquals("yes", extractionFn.apply(""));
Assert.assertEquals("no", extractionFn.apply("abc"));
Assert.assertEquals("no", extractionFn.apply(new Object()));
Assert.assertEquals("no", extractionFn.apply(1));
}
@Test
public void testJavaScriptPorterStemmer()
{

View File

@ -116,12 +116,24 @@ public class RegexDimExtractionFnTest
}
@Test
public void testMissingValueReplacementFromNullAndEmpty()
public void testMissingValueReplacementWhenPatternDoesNotMatchNull()
{
String regex = "(bob)";
ExtractionFn extractionFn = new RegexDimExtractionFn(regex, true, "NO MATCH");
Assert.assertEquals("NO MATCH", extractionFn.apply(""));
Assert.assertEquals("NO MATCH", extractionFn.apply(null));
Assert.assertEquals("NO MATCH", extractionFn.apply("abc"));
Assert.assertEquals("bob", extractionFn.apply("bob"));
}
@Test
public void testMissingValueReplacementWhenPatternMatchesNull()
{
String regex = "^()$";
ExtractionFn extractionFn = new RegexDimExtractionFn(regex, true, "NO MATCH");
Assert.assertEquals(null, extractionFn.apply(""));
Assert.assertEquals(null, extractionFn.apply(null));
Assert.assertEquals("NO MATCH", extractionFn.apply("abc"));
}
@Test
@ -133,6 +145,7 @@ public class RegexDimExtractionFnTest
Assert.assertEquals(null, extractionFn.apply(""));
Assert.assertEquals(null, extractionFn.apply("abc"));
Assert.assertEquals(null, extractionFn.apply("123"));
Assert.assertEquals("bob", extractionFn.apply("bobby"));
}
@Test