KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > expression > XPathExpressionTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
33  * @version $Revision: 426415 $
34  */

35 public class XPathExpressionTest extends TestCase {
36
37     public void testXPathUsingJaxen() throws Exception JavaDoc {
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 JavaDoc {
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         // These are way too complex for XMLBeans! :)
49
//assertExpression(new XMLBeansStringXPathExpression("$name"), "James", "<foo><bar xyz='cheese'/></foo>");
50
//assertExpression(new XMLBeansStringXPathExpression("foo/bar/text()"), "cheese", "<foo><bar>cheese</bar></foo>");
51
}
52
53     protected void assertExpression(Expression expression, String JavaDoc expected, String JavaDoc 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 JavaDoc value = expression.evaluate(exchange, message);
59         assertEquals("Expression: " + expression, expected, value);
60     }
61
62 }
63
Popular Tags