01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.synapse.util;
21:
22: import junit.framework.TestCase;
23: import org.apache.axiom.om.OMFactory;
24: import org.apache.axiom.soap.SOAPHeaderBlock;
25: import org.apache.synapse.MessageContext;
26: import org.apache.synapse.TestMessageContext;
27: import org.apache.synapse.util.xpath.SynapseXPath;
28: import org.apache.synapse.core.axis2.Axis2MessageContext;
29: import org.apache.synapse.mediators.TestUtils;
30:
31: import java.util.HashMap;
32:
33: /**
34: *
35: */
36: public class SynapseXPathTest extends TestCase {
37:
38: String message = "This is XPath test";
39:
40: public void testAbsoluteXPath() throws Exception {
41: SynapseXPath xpath = new SynapseXPath("//test");
42: MessageContext ctx = TestUtils.getTestContext("<test>"
43: + message + "</test>");
44: assertEquals(xpath.getStringValue(ctx), message);
45: }
46:
47: public void testBodyRelativeXPath() throws Exception {
48: SynapseXPath xpath = new SynapseXPath("$body/test");
49: MessageContext ctx = TestUtils.getTestContext("<test>"
50: + message + "</test>");
51: assertEquals(xpath.getStringValue(ctx), message);
52: }
53:
54: public void testHeaderRelativeXPath() throws Exception {
55: SynapseXPath xpath = new SynapseXPath("$header/t:test");
56: xpath.addNamespace("t", "http://test");
57: MessageContext ctx = TestUtils.getTestContext("<test>"
58: + message + "</test>");
59: OMFactory fac = ctx.getEnvelope().getOMFactory();
60: SOAPHeaderBlock block = ctx.getEnvelope().getHeader()
61: .addHeaderBlock("test",
62: fac.createOMNamespace("http://test", "t"));
63: block.setText(message);
64: assertEquals(xpath.getStringValue(ctx), message);
65: }
66:
67: public void testContextProperties() throws Exception {
68: SynapseXPath xpath = new SynapseXPath("$ctx:test");
69: MessageContext synCtx = new TestMessageContext();
70: synCtx.setProperty("test", message);
71: assertEquals(xpath.evaluate(synCtx), message);
72: }
73:
74: public void testAxis2ContextProperties() throws Exception {
75: SynapseXPath xpath = new SynapseXPath("$axis2:test");
76: HashMap props = new HashMap();
77: Axis2MessageContext synCtx = TestUtils.getAxis2MessageContext(
78: "<test/>", props);
79: synCtx.getAxis2MessageContext().setProperty("test", message);
80: synCtx.setProperty("test", message);
81: assertEquals(xpath.evaluate(synCtx), message);
82: }
83:
84: }
|