formatting and changed 'skipSettingNullResults' to simply 'skipNullResults'

This commit is contained in:
Mark Fisher
2010-06-10 19:06:43 +00:00
parent 070ae5ee96
commit afe1a49a4b
2 changed files with 113 additions and 128 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageHeaders;
import org.springframework.integration.message.MessageBuilder;
@@ -30,79 +31,63 @@ import org.springframework.integration.xml.xpath.XPathEvaluationType;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
/**
* @author Jonas Partner
* @since 2.0
*/
public class XPathHeaderEnricherTests {
@Test
public void simpleStringEvaluation() {
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
assertEquals("Wrong value for element two expression", "2", headers.get("two"));
}
@Test
public void testSimpleStringEvaluation(){
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
@Test
public void nullValuesSkippedByDefault() {
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
String docAsString = "<root><elementOne>1</elementOne></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertNull("value set for two when result was null", headers.get("two"));
}
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
assertEquals("Wrong value for element two expression", "2", headers.get("two"));
}
@Test
public void testDontSetNull(){
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
String docAsString = "<root><elementOne>1</elementOne></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertNull("value set for two when result was null",headers.get("two"));
}
@Test
public void testSetNull(){
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
String docAsString = "<root><elementOne>1</elementOne></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
enricher.setSkipSettingNullResults(false);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("no value set for two when result was null and skip null was false","" ,headers.get("two"));
}
@Test
public void testSetNumberEvaluation(){
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
Map<String, XPathEvaluationType> evalTypeMap = new HashMap<String,XPathEvaluationType>();
evalTypeMap.put("two", XPathEvaluationType.NUMBER_RESULT);
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
enricher.setEvaluationTypes(evalTypeMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
assertEquals("Wrong value for element two expression", 2.0, headers.get("two"));
}
@Test
public void notSkippingNullValues() {
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
String docAsString = "<root><elementOne>1</elementOne></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
enricher.setSkipNullResults(false);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("no value set for two when result was null and ignore null was false",
"", headers.get("two"));
}
@Test
public void numberEvaluationResult() {
Map<String, XPathExpression> expressionMap = new HashMap<String, XPathExpression>();
expressionMap.put("one", XPathExpressionFactory.createXPathExpression("/root/elementOne"));
expressionMap.put("two", XPathExpressionFactory.createXPathExpression("/root/elementTwo"));
Map<String, XPathEvaluationType> evalTypeMap = new HashMap<String, XPathEvaluationType>();
evalTypeMap.put("two", XPathEvaluationType.NUMBER_RESULT);
String docAsString = "<root><elementOne>1</elementOne><elementTwo>2</elementTwo></root>";
XPathHeaderEnricher enricher = new XPathHeaderEnricher(expressionMap);
enricher.setEvaluationTypes(evalTypeMap);
Message<?> result = enricher.transform(MessageBuilder.withPayload(docAsString).build());
MessageHeaders headers = result.getHeaders();
assertEquals("Wrong value for element one expression", "1", headers.get("one"));
assertEquals("Wrong value for element two expression", 2.0, headers.get("two"));
}
}