Package org.custommonkey.xmlunit

Examples of org.custommonkey.xmlunit.XpathEngine


    private XPathRegexAssert() {}

    public static void assertXPathMatches(String message, String regex,
                                          String xpath, Document doc)
        throws XpathException {
        XpathEngine engine = XMLUnit.newXpathEngine();
        String value = engine.evaluate(xpath, doc);
        Assert.assertTrue(message, value.matches(regex));
    }
View Full Code Here


     * @return
     */
    public static String evaluate(final String xpath, final String xml) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("svrl", Constants.HTTP_PURL_OCLC_ORG_DSDL_SVRL);
        XpathEngine xpathEngine = XMLUnit.newXpathEngine();
        xpathEngine.setNamespaceContext(new SimpleNamespaceContext(m));
        try {
            return xpathEngine.evaluate(xpath, XMLUnit.buildControlDocument(xml));

        } catch (Exception e) {
            LOG.error("Failed to apply xpath {} on xml {}", xpath, xml);
            throw new RuntimeCamelException(e);
        }
View Full Code Here

            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>another paragra" + MATCH_START + "ph with " + MATCH_END + "<note><hi>"
                + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note>"
                + MATCH_START + " elements." + MATCH_END + "</para>", result);

            XpathEngine xpe = XMLUnit.newXpathEngine();
            NodeList matches = xpe.getMatchingNodes("//exist:match", XMLUnit.buildControlDocument(result));
            StringBuilder m = new StringBuilder();
            for (int i = 0; i < matches.getLength(); i++)
                m.append(matches.item(i).getTextContent());
            String match = m.toString();
            System.out.println("MATCH: " + match);

            assertMatches(wildcardQuery, match);

            wildcardQuery = "\\*.*\\?";
            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>Where did all the " + MATCH_START + "*s go?" + MATCH_END + "</para>", result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);
            assertMatches(wildcardQuery, match);

            wildcardQuery = ".est[][?]tes.";

            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>" + MATCH_START + "test]test" + MATCH_END + " " + MATCH_START + "test[test"
                + MATCH_END + " " + MATCH_START + "test?test" + MATCH_END + "</para>", result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);

            seq = xquery.execute("//para[ngram:wildcard-contains(., '^" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>" + MATCH_START + "test]test" + MATCH_END + " test[test test?test</para>",
                result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);

            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "$')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>test]test test[test " + MATCH_START + "test?test" + MATCH_END + "</para>",
                result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);

            wildcardQuery = "^aaa.aaa$";
            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>" + MATCH_START + "aaacaaa" + MATCH_END + "</para>", result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);
            assertMatches(wildcardQuery, match);

            wildcardQuery = ".+simple";
            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>" + MATCH_START + "a simple" + MATCH_END + " paragraph</para>", result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);
            assertMatches(wildcardQuery, match);

            wildcardQuery = "a s.?i.?m.?p.?l.?e.?";
            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>" + MATCH_START + "a simple " + MATCH_END + "paragraph</para>", result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);
            assertMatches(wildcardQuery, match);

            wildcardQuery = "a s.?i.?m.?p.?l.?e.?";
            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(1, seq.getItemCount());
            result = queryResult2String(broker, seq, 0);
            System.out.println("RESULT: " + result);
            XMLAssert.assertEquals("<para>" + MATCH_START + "a simple " + MATCH_END + "paragraph</para>", result);

            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            System.out.println("MATCH: " + match);
            assertMatches(wildcardQuery, match);

            wildcardQuery = "b.{3,6}c";
            seq = xquery.execute("//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null,
                AccessContext.TEST);
            assertNotNull(seq);
            assertEquals(2, seq.getItemCount());

            for (int i = 0; i < 2; i++) {
                result = queryResult2String(broker, seq, i);
                System.out.println("RESULT: " + result);

                match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
                System.out.println("MATCH: " + match);
                assertMatches(wildcardQuery, match);
            }

        } catch (Exception e) {
View Full Code Here

        final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
        final Sequence result = idFunctions.eval(new Sequence[]{Sequence.EMPTY_SEQUENCE}, null);

        assertEquals(1, result.getItemCount());

        final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
        final Map<String, String> namespaces = new HashMap<String, String>();
        namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
        xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));

        final DocumentImpl resultDoc = (DocumentImpl)result.itemAt(0);

        final NodeList nlRealUsername = xpathEngine.getMatchingNodes("/sm:id/sm:real/sm:username", resultDoc);
        assertEquals(1, nlRealUsername.getLength());
        assertEquals(realUsername, nlRealUsername.item(0).getNodeValue());

        final NodeList nlEffectiveUsername = xpathEngine.getMatchingNodes("/sm:id/sm:effective/sm:username", resultDoc);
        assertEquals(1, nlEffectiveUsername.getLength());
        assertEquals(effectiveUsername, nlEffectiveUsername.item(0).getNodeValue());

        verify(mckEffectiveUser, mckRealUser, mckContext);
    }
View Full Code Here

        final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
        final Sequence result = idFunctions.eval(new Sequence[]{Sequence.EMPTY_SEQUENCE}, null);

        assertEquals(1, result.getItemCount());

        final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
        final Map<String, String> namespaces = new HashMap<String, String>();
        namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
        xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));

        final DocumentImpl resultDoc = (DocumentImpl)result.itemAt(0);

        final NodeList nlRealUsername = xpathEngine.getMatchingNodes("/sm:id/sm:real/sm:username", resultDoc);
        assertEquals(1, nlRealUsername.getLength());
        assertEquals(username, nlRealUsername.item(0).getNodeValue());

        final NodeList nlEffectiveUsername = xpathEngine.getMatchingNodes("/sm:id/sm:effective/sm:username", resultDoc);
        assertEquals(0, nlEffectiveUsername.getLength());

        verify(mckUser, mckContext);
    }
View Full Code Here

    Map<String, String> namespaces = new HashMap<String, String>();
    namespaces.put("tns", "http://samples.springframework.org/flight");
    namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");

    NamespaceContext ctx = new SimpleNamespaceContext(namespaces);
    XpathEngine engine = XMLUnit.newXpathEngine();
    engine.setNamespaceContext(ctx);

    Document doc = XMLUnit.buildControlDocument(xmlDoc);
    NodeList node = engine.getMatchingNodes(xpath, doc);
    assertEquals(msg, expected, node.item(0).getNodeValue());
  }
View Full Code Here

    }

    private boolean isXPathMatch(String value) {
        try {
            Document inDocument = XMLUnit.buildControlDocument(value);
            XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
            NodeList nodeList = simpleXpathEngine.getMatchingNodes(
                    matchesXPath, inDocument);
            return nodeList.getLength() > 0;
        } catch (SAXException e) {
            notifier().info(String.format(
                    "Warning: failed to parse the XML document. Reason: %s\nXML: %s", e.getMessage(), value));
View Full Code Here

        LineString curve = new CurvedGeometryFactory(0.1)
                .createCurvedGeometry(new LiteCoordinateSequence(new double[] { 1, 1, 2, 2, 3, 1,
                        5, 5, 7, 3 }));
        Document dom = encode(curve, GML.curveProperty);
        // print(dom);
        XpathEngine xpath = XMLUnit.newXpathEngine();
        String basePath = "/gml:curveProperty/gml:Curve/gml:segments/gml:ArcString";
        assertEquals(1,
                xpath.getMatchingNodes(basePath + "[@interpolation='circularArc3Points']", dom)
                        .getLength());
        assertEquals("1.0 1.0 2.0 2.0 3.0 1.0 5.0 5.0 7.0 3.0",
                xpath.evaluate(basePath + "/gml:posList", dom));
    }
View Full Code Here

        LineString compound = factory.createCurvedGeometry(curve, straight);

        // encode
        Document dom = encode(compound, GML.curveProperty);
        // print(dom);
        XpathEngine xpath = XMLUnit.newXpathEngine();

        // the curve portion
        String basePath1 = "/gml:curveProperty/gml:Curve/gml:segments/gml:ArcString";
        assertEquals(1,
                xpath.getMatchingNodes(basePath1 + "[@interpolation='circularArc3Points']", dom)
                        .getLength());
        assertEquals("1.0 1.0 2.0 2.0 3.0 1.0 5.0 5.0 7.0 3.0",
                xpath.evaluate(basePath1 + "/gml:posList", dom));

        // the straight portion
        String basePath2 = "/gml:curveProperty/gml:Curve/gml:segments/gml:LineStringSegment";
        assertEquals(1, xpath.getMatchingNodes(basePath2 + "[@interpolation='linear']", dom)
                .getLength());
        assertEquals("7.0 3.0 10.0 15.0", xpath.evaluate(basePath2 + "/gml:posList", dom));

    }
View Full Code Here

        LineString curve = new CurvedGeometryFactory(0.1)
                .createCurvedGeometry(new LiteCoordinateSequence(new double[] { 1, 1, 2, 2, 3, 1,
                        5, 5, 7, 3 }));
        Document dom = encode(curve, GML.curveProperty);
        // print(dom);
        XpathEngine xpath = XMLUnit.newXpathEngine();
        String basePath = "/gml:curveProperty/gml:Curve/gml:segments/gml:ArcString";
        assertEquals(1,
                xpath.getMatchingNodes(basePath + "[@interpolation='circularArc3Points']", dom)
                        .getLength());
        assertEquals("1.0 1.0 2.0 2.0 3.0 1.0 5.0 5.0 7.0 3.0",
                xpath.evaluate(basePath + "/gml:posList", dom));
    }
View Full Code Here

TOP

Related Classes of org.custommonkey.xmlunit.XpathEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.