summaryrefslogtreecommitdiff
path: root/test/rexml/data/test/tests.xsl
blob: 3ecd648b3bf4e88071f9e75f0fcc0c5257e299c9 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<stylesheet	xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:var="http://jaxen.org/test-harness/var">
<!-- this is what I used to generate XPathTestBase. After generating I fixed the illegal strings (its quicker
than fixing the xsl for that few errors) and reformatted the code. Its unlikely this code will be needed
again, its just in cvs for completeness -->
    <output method="text"/>
    <template match="/">
        <text>
            /*
 * $Header: /home/projects/jaxen/scm/jaxen/src/java/test/org/jaxen/XPathTestBase.java,v 1.32 2005/06/15 23:52:40 bewins Exp $
 * $Revision: 1.32 $
 * $Date: 2005/06/15 23:52:40 $
 *
 * ====================================================================
 *
 * Copyright (C) 2000-2002 bob mcwhirter &amp; James Strachan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions, and the disclaimer that follows
 *    these conditions in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. The name "Jaxen" must not be used to endorse or promote products
 *    derived from this software without prior written permission.  For
 *    written permission, please contact license@jaxen.org.
 *
 * 4. Products derived from this software may not be called "Jaxen", nor
 *    may "Jaxen" appear in their name, without prior written permission
 *    from the Jaxen Project Management (pm@jaxen.org).
 *
 * In addition, we request (but do not require) that you include in the
 * end-user documentation provided with the redistribution and/or in the
 * software itself an acknowledgement equivalent to the following:
 *     "This product includes software developed by the
 *      Jaxen Project (http://www.jaxen.org/)."
 * Alternatively, the acknowledgment may be graphical using the logos
 * available at http://www.jaxen.org/
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * ====================================================================
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Jaxen Project and was originally
 * created by bob mcwhirter &lt;bob@werken.com> and
 * James Strachan &lt;jstrachan@apache.org>.  For more information on the
 * Jaxen Project, please see &lt;http://www.jaxen.org/>.
 *
 * $Id: XPathTestBase.java,v 1.32 2005/06/15 23:52:40 bewins Exp $
 */


package org.jaxen;

import junit.framework.TestCase;
import org.jaxen.function.StringFunction;
import org.jaxen.saxpath.helpers.XPathReaderFactory;

import javax.xml.parsers.ParserConfigurationException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public abstract class XPathTestBase extends TestCase
{
    protected static String VAR_URI   = "http://jaxen.org/test-harness/var";
    protected static String TESTS_XML = "xml/test/tests.xml";

    protected static boolean verbose = true;
    protected static boolean debug = true;
    private ContextSupport  contextSupport;

    public XPathTestBase(String name)
    {
        super( name );
    }

    public void setUp() throws ParserConfigurationException
    {
        this.contextSupport = null;
        System.setProperty( XPathReaderFactory.DRIVER_PROPERTY,
                            "" );
        log( "-----------------------------" );
    }

    public void log(String text)
    {
        log( verbose,
             text );
    }

    public void log(boolean actualVerbose,
                    String text)
    {
        if ( ! actualVerbose )
        {
            return;
        }

        System.out.println( text );
    }

    protected void assertCountXPath(int expectedSize, Object context, String xpathStr) throws JaxenException {
        try
        {
            assertCountXPath2(expectedSize, context, xpathStr);
        }
        catch (UnsupportedAxisException e)
        {
            log ( debug,
                  "      ## SKIPPED -- Unsupported Axis" );
        }
    }

    protected Object assertCountXPath2(int expectedSize, Object context, String xpathStr) throws JaxenException {
        log ( debug,
              "  Select :: " + xpathStr );
        BaseXPath xpath = new BaseXPath( xpathStr );
        List results = xpath.selectNodes( getContext( context ) );
        log ( debug,
              "    Expected Size :: " + expectedSize );
        log ( debug,
              "    Result Size   :: " + results.size() );

        if ( expectedSize != results.size() )
        {
            log ( debug,
                  "      ## FAILED" );
            log ( debug,
                  "      ## xpath: " + xpath + " = " + xpath.debug() );

            Iterator resultIter = results.iterator();

            while ( resultIter.hasNext() )
            {
                log ( debug,
                      "      --> " + resultIter.next() );
            }
        }
        assertEquals( xpathStr,
                      expectedSize,
                      results.size() );
        if (expectedSize > 0) {
            return results.get(0);
        }
        return null;
    }

    protected void assertInvalidXPath(Object context, String xpathStr) throws JaxenException {
        try 
        {
            log ( debug,
                  "  Select :: " + xpathStr );
            BaseXPath xpath = new BaseXPath( xpathStr );
            List results = xpath.selectNodes( getContext( context ) );
            log ( debug,
                  "    Result Size   :: " + results.size() );
            fail("An exception was expected.");
        }
        catch (UnsupportedAxisException e)
        {
            log ( debug,
                  "      ## SKIPPED -- Unsupported Axis" );
        }
        catch (JaxenException e) {
            log (debug, "    Caught expected exception "+e.getMessage());
        }
    }

    protected void assertValueOfXPath(String expected, Object context, String xpathStr) throws JaxenException {
        try
        {
            BaseXPath xpath = new BaseXPath( xpathStr );
            Object node = xpath.evaluate( getContext( context ) );
            
            String result = StringFunction.evaluate( node,
                                                     getNavigator() );
            
            log ( debug,
                  "  Select :: " + xpathStr );
            log ( debug,
                  "    Expected :: " + expected );
            log ( debug,
                  "    Result   :: " + result );
            
            if ( ! expected.equals( result ) )
            {
                log ( debug,
                      "      ## FAILED" );
                log ( debug,
                      "      ## xpath: " + xpath + " = " + xpath.debug() );
            }
            
            assertEquals( xpathStr,
                          expected,
                          result );
        }
        catch (UnsupportedAxisException e)
        {
            log ( debug,
                  "      ## SKIPPED -- Unsupported Axis " );

        }        
    }
    protected Context getContext(Object contextNode)
    {
        Context context = new Context( getContextSupport() );

        List list = new ArrayList( 1 );
        list.add( contextNode );
        context.setNodeSet( list );

        return context;
    }

    public ContextSupport getContextSupport()
    {
        if ( this.contextSupport == null )
        {
            this.contextSupport = new ContextSupport( new SimpleNamespaceContext(),
                                                      XPathFunctionContext.getInstance(),
                                                      new SimpleVariableContext(),
                                                      getNavigator() );
        }

        return this.contextSupport;
    }

    public abstract Navigator getNavigator();

    public abstract Object getDocument(String url) throws Exception;

    public void testGetNodeType() throws FunctionCallException, UnsupportedAxisException
    {
        Navigator nav = getNavigator();
        Object document = nav.getDocument("xml/testNamespaces.xml");
        int count = 0;
        Iterator descendantOrSelfAxisIterator = nav.getDescendantOrSelfAxisIterator(document);
        while (descendantOrSelfAxisIterator.hasNext()) {
            Object node = descendantOrSelfAxisIterator.next();
            Iterator namespaceAxisIterator = nav.getNamespaceAxisIterator(node);
            while (namespaceAxisIterator.hasNext()) {
                count++;
                assertEquals("Node type mismatch", Pattern.NAMESPACE_NODE, nav.getNodeType(namespaceAxisIterator.next()));
            }
        }
        assertEquals(25, count);
    }

        </text>
        <apply-templates select="node()|@*"/>
        <text>
}            
        </text>
    </template>
    <template match="context">
        <text>
    public void test</text><value-of select="generate-id()"/><text>() throws JaxenException {
        Navigator nav = getNavigator();
        String url = "</text><value-of select="../@url"/><text>";
        log( "Document [" + url + "]" );
        Object document = nav.getDocument(url);

        XPath contextpath = new BaseXPath("</text><value-of select="@select"/><text>", nav);
        log( "Initial Context :: " + contextpath );
        List list = contextpath.selectNodes(document);
        </text>
        <if test="count(namespace::*) > count(../namespace::*)">
        <text>
        SimpleNamespaceContext nsContext = new SimpleNamespaceContext();</text>
            <for-each select="namespace::*[local-name() != 'var' and local-name() != 'xml']">
                <text>
        nsContext.addNamespace( "</text><value-of select="local-name()"/><text>", "</text><value-of select="."/><text>" );</text>
           </for-each>
            <text>
        getContextSupport().setNamespaceContext( nsContext );</text>
        </if>
        <if test="@*[namespace-uri() = 'http://jaxen.org/test-harness/var']">
        <text>
        SimpleVariableContext varContext = new SimpleVariableContext();</text>
            <for-each select="@*[namespace-uri() = 'http://jaxen.org/test-harness/var']">
                <text>
        varContext.setVariableValue(null, "</text><value-of select="local-name()"/><text>", "</text><value-of select="."/><text>" );</text>
           </for-each>
            <text>
        getContextSupport().setVariableContext( varContext );</text>
        </if>
        <text>
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            Object context = iter.next();</text>
            <apply-templates select="node()|@*"/>
        <text>
        }
    }</text>
    </template>
    <template match="test[@exception]">
        <text>
            assertInvalidXPath(context, "</text><value-of select='@select'/><text>");</text>
    </template>
    <template match="test[valueOf]">
        <choose>
            <when test="@count">
                <text>
            try
            {
                Object result = assertCountXPath2(</text><value-of select="@count"/><text>, context, "</text><value-of select="@select"/><text>");</text>
        <for-each select="valueOf">
            <text>
                assertValueOfXPath("</text><value-of select="."/><text>", result, "</text><value-of select="@select"/><text>");</text>
        </for-each>
        <text>
            }
            catch (UnsupportedAxisException e)
            {
                log ( debug, "      ## SKIPPED -- Unsupported Axis" );
            }</text>
            </when>
            <otherwise>
                <text>
            try
            {
                BaseXPath xpath = new BaseXPath( "</text><value-of select="@select"/><text>" );
                List results = xpath.selectNodes( getContext( context ) );
                Object result = results.get(0);</text>
        <for-each select="valueOf">
            <text>
                assertValueOfXPath("</text><value-of select="."/><text>", result, "</text><value-of select="@select"/><text>");</text>
        </for-each>
        <text>
            }
            catch (UnsupportedAxisException e)
            {
                log ( debug, "      ## SKIPPED -- Unsupported Axis" );
            }</text>
            </otherwise>
        </choose>
    </template>
    <template match="test">
        <text>
            assertCountXPath(</text><value-of select="@count"/><text>, context, "</text><value-of select="@select"/><text>");</text>
    </template>
    <template match="valueOf">
        <text>
            assertValueOfXPath("</text><value-of select="."/>", context, "<value-of select="@select"/><text>");</text>
    </template>
    <template match="comment()"><text>
    /*</text><value-of select="."/><text>
    */</text>
    </template>
    <template match="node()|@*"><apply-templates select="node()|@*"/></template>    
</stylesheet>