Permalink
Browse files

Fixed XPathQuery to behave properly when multiple elements are selected

Signed-off-by: gburgett <gordon.burgett@gmail.com>
  • Loading branch information...
1 parent 1dd47cb commit 6d95e3cab60b5b367cdbae921f67c662397b52ff @gburgett committed Jan 31, 2013
@@ -44,6 +44,8 @@
* The XPath expressions select elements or attributes in the row data which
* are convertible to the query's value type, and then the converted value
* type is compared to the given value to see if the row matches the query.
+ * <p/>
+ * The row is matched to the query if any one
* @author Gordon
*/
public class XPathQuery {
@@ -513,44 +515,52 @@ public ValueMatcher(XPathExpression<?> selector, Class<T> expectedType, Matcher<
@Override
protected boolean matchesSafely(Element item) {
- Object selected;
-
- selected = selector.evaluateFirst(item);
-
- if(selected == null){
- if(expectedType != null && conversionService != null &&
- conversionService.canConvert(null, expectedType)){
- try {
- return subMatcher.matches(conversionService.convert(null, expectedType));
- } catch (ConversionException ex) {
- Log log = LogFactory.getLog(getClass());
- log.warn("Unable to convert null to " + expectedType);
- return false;
+ boolean anyMatches = false;
+ for(Object selected : selector.evaluate(item)){
+ anyMatches = true;
+ System.out.println("selected: " + selected);
+ if(expectedType != null){
+ if(!expectedType.isAssignableFrom(selected.getClass())){
+ //need to convert
+ if(conversionService != null && conversionService.canConvert(selected.getClass(), expectedType)){
+ try{
+ selected = conversionService.convert(selected, expectedType);
+ }catch(ConversionException ex){
+ //if we can't convert then the data is in the wrong format
+ //and probably was not intended to be selected
+ continue;
+ }
+ }
+ else{
+ continue;
+ }
}
}
-
- return subMatcher.matches(null);
+
+ if(subMatcher.matches(selected)){
+ return true;
+ }
}
- if(expectedType != null){
- if(!expectedType.isAssignableFrom(selected.getClass())){
- //need to convert
- if(conversionService != null && conversionService.canConvert(selected.getClass(), expectedType)){
- try{
- selected = conversionService.convert(selected, expectedType);
- }catch(ConversionException ex){
- //if we can't convert then the data is in the wrong format
- //and probably was not intended to be selected
- return false;
- }
- }
- else{
- return false;
- }
+ if(anyMatches){
+ //we didn't match this row
+ return false;
+ }
+
+ //not a single match in all the selected nodes,
+ //check to see if we are matching null (as in, not exists)
+ if(expectedType != null && conversionService != null &&
+ conversionService.canConvert(null, expectedType)){
+ try {
+ return subMatcher.matches(conversionService.convert(null, expectedType));
+ } catch (ConversionException ex) {
+ //if we can't convert then the data is in the wrong format
+ //and probably was not intended to be selected
+ return false;
}
}
- return subMatcher.matches(selected);
+ return subMatcher.matches(null);
}
@Override
@@ -865,6 +865,31 @@ public void testExists_DoesntExist_DoesntMatch() throws Exception {
}//end testExists_DoesntExist_DoesntMatch
@Test
+ public void testEq_MatchesSecondOfTwoSelected_Matches() throws Exception {
+ System.out.println("testEq_MatchesSecondOfTwoSelected_Matches");
+
+ Element row = new Element("row");
+ row.addContent(new Element("data")
+ .setAttribute("val", "value"));
+ row.addContent(new Element("data")
+ .setAttribute("val", "value2"));
+
+ XPathExpression<Object> path = xpath.compile("data/@val");
+ XPathQuery query = XPathQuery.eq(path, "value2");
+
+ query.setConversionService(conversionService);
+
+ System.out.println(path.getExpression() + ": " + path.evaluateFirst(row));
+ System.out.println(query);
+
+ //act
+ boolean matches = query.getRowMatcher().matches(row);
+
+ //assert
+ assertTrue(matches);
+ }
+
+ @Test
public void testDissect_NoMatchingIndex_ReturnsAll() throws Exception {
System.out.println("testDissect_NoMatchingIndex_ReturnsAll");

0 comments on commit 6d95e3c

Please sign in to comment.