1 17 package org.apache.servicemix.expression; 18 19 import org.apache.servicemix.expression.Expression; 20 import org.apache.servicemix.expression.JaxenStringXPathExpression; 21 import org.apache.servicemix.expression.XMLBeansStringXPathExpression; 22 import org.apache.servicemix.jbi.jaxp.StringSource; 23 import org.apache.servicemix.jbi.messaging.InOnlyImpl; 24 import org.apache.servicemix.jbi.messaging.MessageExchangeImpl; 25 import org.apache.servicemix.jbi.messaging.NormalizedMessageImpl; 26 27 import javax.jbi.messaging.MessagingException; 28 import javax.jbi.messaging.NormalizedMessage; 29 30 import junit.framework.TestCase; 31 32 35 public class XPathExpressionTest extends TestCase { 36 37 public void testXPathUsingJaxen() throws Exception { 38 assertExpression(new JaxenStringXPathExpression("foo/bar"), "cheese", "<foo><bar>cheese</bar></foo>"); 39 assertExpression(new JaxenStringXPathExpression("foo/bar/@xyz"), "cheese", "<foo><bar xyz='cheese'/></foo>"); 40 assertExpression(new JaxenStringXPathExpression("$name"), "James", "<foo><bar xyz='cheese'/></foo>"); 41 assertExpression(new JaxenStringXPathExpression("foo/bar/text()"), "cheese", "<foo><bar>cheese</bar></foo>"); 42 } 43 44 public void testXPathUsingXMLBeans() throws Exception { 45 assertExpression(new XMLBeansStringXPathExpression("foo/bar"), "cheese", "<foo><bar>cheese</bar></foo>"); 46 assertExpression(new XMLBeansStringXPathExpression("foo/bar/@xyz"), "cheese", "<foo><bar xyz='cheese'/></foo>"); 47 48 } 52 53 protected void assertExpression(Expression expression, String expected, String xml) throws MessagingException { 54 MessageExchangeImpl exchange = new InOnlyImpl("dummy"); 55 NormalizedMessage message = new NormalizedMessageImpl(exchange); 56 message.setProperty("name", "James"); 57 message.setContent(new StringSource(xml)); 58 Object value = expression.evaluate(exchange, message); 59 assertEquals("Expression: " + expression, expected, value); 60 } 61 62 } 63 | Popular Tags |