summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/test/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolverTestCase.java
blob: d2b0775361753e1e450778e44f16f2677449e418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.tuscany.sca.policy.xml;

import static org.apache.tuscany.sca.policy.xml.PolicyXPathFunction.normalize;

import java.util.Collections;
import java.util.Iterator;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * 
 */
public class PolicyXPathFunctionResolverTestCase {
    private static XPath xpath;

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        XPathFactory factory = XPathFactory.newInstance();
        xpath = factory.newXPath();
        xpath.setNamespaceContext(new NamespaceContextImpl());
        xpath.setXPathFunctionResolver(new PolicyXPathFunctionResolver(xpath.getNamespaceContext()));
    }

    @Test
    public void testIntentsRef() throws Exception {
        InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
        String str = "//sca:composite/sca:component[IntentRefs('sca:confidentiality')]";
        str = normalize(str, "sca");
        // Test the rewrite of xpath so that the self:node() is passed into the SCA function
        XPathExpression exp = xpath.compile(str);
        Object result = exp.evaluate(xml, XPathConstants.NODESET);
        Assert.assertTrue(result instanceof NodeList);
        NodeList nodes = (NodeList)result;
        Assert.assertEquals(1, nodes.getLength());
    }
    
    @Test
    public void testIntentsRef2() throws Exception {
        InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
        String str = " //sca:composite/sca:component[sca:IntentRefs('sca:confidentiality')]";
        str = normalize(str, "sca");
        // Test the rewrite of xpath so that the self:node() is passed into the SCA function
        XPathExpression exp = xpath.compile(str);
        Object result = exp.evaluate(xml, XPathConstants.NODESET);
        Assert.assertTrue(result instanceof NodeList);
        NodeList nodes = (NodeList)result;
        Assert.assertEquals(1, nodes.getLength());
    }
    
    @Test
    public void testIntentsRef3() throws Exception {
        InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
        String str = "   IntentRefs('sca:confidentiality')  ";
        str = normalize(str, "sca");
        // Test the rewrite of xpath so that the self:node() is passed into the SCA function
        XPathExpression exp = xpath.compile(str);
        Object result = exp.evaluate(xml, XPathConstants.NODESET);
        Assert.assertTrue(result instanceof NodeList);
        NodeList nodes = (NodeList)result;
        Assert.assertEquals(1, nodes.getLength());
    }
    @Test
    public void testURIRef() throws Exception {
        InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
        XPathExpression exp = xpath.compile(normalize("sca:composite/sca:component[sca:URIRef('AddServiceComponent')]","sca"));
        Object result = exp.evaluate(xml, XPathConstants.NODESET);
        Assert.assertTrue(result instanceof NodeList);
        NodeList nodes = (NodeList)result;
        // Assert.assertEquals(1, nodes.getLength());
    }
    
    @Test
    public void testInterfaceRef() throws Exception {
        InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
        XPathExpression exp = xpath.compile(normalize("//sca:composite/sca:component/sca:service[sca:InterfaceRef('AddService')]","sca"));
        Object result = exp.evaluate(xml, XPathConstants.NODESET);
        Assert.assertTrue(result instanceof NodeList);
        NodeList nodes = (NodeList)result;
        // Assert.assertEquals(1, nodes.getLength());
    }

    @Test
    public void testOperationRef() throws Exception {
        InputSource xml = new InputSource(getClass().getResourceAsStream("Calculator.composite"));
        XPathExpression exp = xpath.compile(normalize("//sca:composite/sca:component/sca:reference[sca:OperationRef('AddService/add')]","sca"));
        Object result = exp.evaluate(xml, XPathConstants.NODESET);
        Assert.assertTrue(result instanceof NodeList);
        NodeList nodes = (NodeList)result;
        // Assert.assertEquals(1, nodes.getLength());
    }


    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    private static class NamespaceContextImpl implements NamespaceContext {

        private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912";

        public String getNamespaceURI(String prefix) {
            if ("sca".equals(prefix)) {
                return SCA11_NS;
            } else {
                return null;
            }
        }

        public String getPrefix(String namespaceURI) {
            if (SCA11_NS.equals(namespaceURI)) {
                return "sca";
            }
            return null;
        }

        public Iterator getPrefixes(String namespaceURI) {
            if (SCA11_NS.equals(namespaceURI)) {
                return Collections.singleton("sca").iterator();
            }
            return null;
        }

    }

}