### Question:
VariableFieldExpression implements Expression<String, Document> { @Override public String evaluate(final Document target) { try { final String result = xpath.value(expression, target); return result != null && result.trim().length() > 0 ? result : null; } catch (final Exception exception) { throw new RuntimeException(exception); } } VariableFieldExpression(final String specs); @Override String evaluate(final Document target); @Override String specs(); @Override String toString();  }### Answer:
@Test public void noTargetField() { final Document document = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a'); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "a"); expression.xpath = new OXPath(); assertNull(expression.evaluate(document)); }
@Test public void noTargetSubField() { final Document document = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a'); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME); expression.xpath = new OXPath(); assertNull(expression.evaluate(document)); }
@Test public void selectorWithoutIndicatorsPattern() { final String expectedValue = "This is the expected value"; final Document document = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a', expectedValue); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "a"); expression.xpath = new OXPath(); assertEquals(expectedValue, expression.evaluate(document)); }
@Test public void selectorWithoutIndicatorsPatternAndMultipleMatchingValues() { final String expectedValue = "This is the expected value"; final Document document = newRecord(A_VARIABLE_FIELD_NAME, '#', '#', 'a', expectedValue, "another value", "still another value"); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "a"); expression.xpath = new OXPath(); assertEquals(expectedValue, expression.evaluate(document)); }
@Test public void indicatorsPatternWithLiterals() { final String expectedValue = "This is the expected value"; final Document document = newRecord(A_VARIABLE_FIELD_NAME, '1', '2', 'a', expectedValue); final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "(12)a"); expression.xpath = new OXPath(); assertEquals(expectedValue, expression.evaluate(document)); }
@Test public void indicatorsPatternWithGlob() { final String expectedValue = "This is the expected value"; final Document document = newRecord(A_VARIABLE_FIELD_NAME, '1', '2', 'a', expectedValue); final String [] matchingPatterns = {"??", "1?", "?2"}; for (final String matchingPattern : matchingPatterns) { final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "(" + matchingPattern + ")a"); System.out.println(expression); expression.xpath = new OXPath(); assertEquals(expectedValue, expression.evaluate(document)); } }
@Test public void indicatorsPatternDoesntMatch() { final Document document = newRecord(A_VARIABLE_FIELD_NAME, '1', '2', 'a', "A value"); final String [] unmatchingPatterns = {"?3", "#?", "##", "44", "21"}; for (final String unmatchingPattern : unmatchingPatterns) { final VariableFieldExpression expression = new VariableFieldExpression(A_VARIABLE_FIELD_NAME + "(" + unmatchingPattern + ")a"); expression.xpath = new OXPath(); assertNull(expression.evaluate(document)); } }