OPEN - issue INT-309: XPath Message Selector

Added XPAth MessageSelector namespace to follow
This commit is contained in:
Jonas Partner
2008-09-29 16:07:44 +00:00
parent 039679b173
commit d03bb7ae5a
6 changed files with 420 additions and 3 deletions

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.selector;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;
/**
*
* @author Jonas Partner
*
*/
public class BooleanTestXpathMessageSelectorTests {
@Test
public void testWithSimpleString(){
BooleanTestXPathMessageSelector selector = new BooleanTestXPathMessageSelector("boolean(/one/two)");
assertTrue(selector.accept(new StringMessage("<one><two/></one>")) ) ;
assertFalse(selector.accept(new StringMessage("<one><three/></one>")) ) ;
}
@Test
public void testWithDocument() throws Exception{
BooleanTestXPathMessageSelector selector = new BooleanTestXPathMessageSelector("boolean(/one/two)");
assertTrue(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<one><two/></one>"))) ) ;
assertFalse(selector.accept(new GenericMessage<Document>(XmlTestUtil.getDocumentForString("<one><three/></one>"))) ) ;
}
@Test
public void testWithNamespace(){
BooleanTestXPathMessageSelector selector = new BooleanTestXPathMessageSelector("boolean(/ns1:one/ns1:two)","ns1", "www.example.org");
assertTrue(selector.accept(new StringMessage("<ns1:one xmlns:ns1='www.example.org'><ns1:two/></ns1:one>")) ) ;
assertFalse(selector.accept(new StringMessage("<ns2:one xmlns:ns2='www.example2.org'><ns1:two xmlns:ns1='www.example.org' /></ns2:one>")) ) ;
}
@Test
public void testWithXPathExpressionProvided(){
XPathExpression xpathExpression = XPathExpressionFactory.createXPathExpression("boolean(/one/two)");
BooleanTestXPathMessageSelector selector = new BooleanTestXPathMessageSelector(xpathExpression);
assertTrue(selector.accept(new StringMessage("<one><two/></one>")) ) ;
assertFalse(selector.accept(new StringMessage("<one><three/></one>")) ) ;
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.selector;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.integration.message.StringMessage;
/**
*
* @author Jonas Partner
*
*/
public class StringValueTestXPathMessageSelectorTests {
@Test
public void testMatchWithSimpleString() {
StringValueTestXPathMessageSelector selector = new StringValueTestXPathMessageSelector("/one/two",
"red");
assertTrue(selector.accept(new StringMessage("<one><two>red</two></one>")));
}
@Test
public void testNoMatchWithSimpleString() {
StringValueTestXPathMessageSelector selector = new StringValueTestXPathMessageSelector("/one/two",
"red");
assertFalse(selector.accept(new StringMessage("<one><two>yellow</two></one>")));
}
@Test
public void testMatchWithSimpleStringAndNamespace() {
StringValueTestXPathMessageSelector selector = new StringValueTestXPathMessageSelector("/ns1:one/ns1:two","ns1","www.example.org",
"red");
assertTrue(selector.accept(new StringMessage("<ns1:one xmlns:ns1='www.example.org'><ns1:two>red</ns1:two></ns1:one>")));
}
@Test
public void testCaseSensitiveByDefault() {
StringValueTestXPathMessageSelector selector = new StringValueTestXPathMessageSelector("/ns1:one/ns1:two","ns1","www.example.org",
"red");
assertFalse(selector.accept(new StringMessage("<ns1:one xmlns:ns1='www.example.org'><ns1:two>RED</ns1:two></ns1:one>")));
}
@Test
public void testNotCaseSensitive() {
StringValueTestXPathMessageSelector selector = new StringValueTestXPathMessageSelector("/ns1:one/ns1:two","ns1","www.example.org",
"red");
selector.setCaseSensitive(false);
assertTrue(selector.accept(new StringMessage("<ns1:one xmlns:ns1='www.example.org'><ns1:two>RED</ns1:two></ns1:one>")));
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.xml.util;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
@@ -25,11 +26,10 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.springframework.xml.transform.StringResult;
/**
* Utility class for XML related testing
*
@@ -38,7 +38,10 @@ import org.springframework.xml.transform.StringResult;
public class XmlTestUtil {
public static Document getDocumentForString(String strDoc) throws Exception {
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
builder.setNamespaceAware(true);
return builder.newDocumentBuilder().parse(
new InputSource(new StringReader(strDoc)));
}