JUnit将XML与文本进行比较

时间:2016-11-30 14:55:55

标签: java xml junit xquery

我试图将我的输入XML与预期的TXT进行比较

但不知怎的,它失败了,我不知道为什么。我认为这是因为我正在编写一个否定测试(预期TXT包含错误消息,由于输入无效而由xquery产生)

比较两个XML正在运行(没有否定测试)。

public void testHeader(String inputHeaderFileName, String expectedResultFileName) throws Exception {
        HashMap<String, Object> xqueryParametersMap = new HashMap<String, Object>();
        xqueryParametersMap.put("udgHeader", db.parse(getFileFromTestDataBasePath(inputHeaderFileName)));

        this.test(xqueryParametersMap, expectedResultFileName);
    }

public void test(Map<String, Object> xqueryParametersMap, String expectedResultFileName) throws Exception {

        String expectedOutput = readFile(testDataBasePath + File.separator + expectedResultFileName, encoding);

        String result = transform(xqueryParametersMap).xmlText(new XmlOptions().setSavePrettyPrint().setSavePrettyPrintIndent(2));

        if (printTransformedXmlToConsoleBoolean) System.out.println(result);

        Diff diff = new Diff(expectedOutput, result);
        diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
        assertTrue("Grundstruktur des Resultats ist anders als in '" + expectedResultFileName + "' erwartet!\n\n" + diff + "\n\n", diff.similar());

        DetailedDiff detailDiff = new DetailedDiff(compareXML(expectedOutput, result));
        @SuppressWarnings("unchecked")
        List<Difference> allDifferences = detailDiff.getAllDifferences();

        assertEquals("Tatsaechliches Ergebnis weicht von '" + expectedResultFileName + "' ab!\n\n" + detailDiff + "\n\n", 0, allDifferences.size());
    }

private XmlObject transform(Map<String, Object> xqueryParametersMap) throws Exception {
        XmlObject xmlObject = XmlObject.Factory.newInstance();
        XmlOptions options = new XmlOptions();

        Map<String, Object> paramMap = new HashMap<String, Object>();

        Iterator<Entry<String, Object>> it = xqueryParametersMap.entrySet().iterator();

        while (it.hasNext()) {

            Map.Entry<String, Object> pairs = it.next();
            Object value = pairs.getValue();
            String key = pairs.getKey();

            if (value instanceof Document) {

                XmlObject inputXml = XmlObject.Factory.parse(getStringFromDocument((Document) value));
                paramMap.put(key, getXmlObject(inputXml));

            } else if (value instanceof String) {

                XmlString string = XmlString.Factory.newInstance();
                string.setStringValue(value.toString());
                paramMap.put(key, string);

            }
        }

        String xqueryFileContent = readFile(xQueryUnderTestPath, encoding);

        options.setXqueryVariables(paramMap);

            xqueryFileContent = xqueryFileContent.replaceAll("(?s)\\s*?\\(:.*?:\\)", "");

        XmlObject[] resultsObjects = xmlObject.execQuery(xqueryFileContent, options);

        if (resultsObjects.length  < 1)
        {
            //throw new NoResultException();
            throw new Exception("Xquery transformation did not return a result");
        }
        else if (resultsObjects.length  > 1)
        {
            //throw new NonUniqueResultException("result size is '" + resultsObjects.length + "'");
            throw new Exception("Xquery transformation did return more than one result: " + resultsObjects.length);
        }
        else return resultsObjects[0];
    }

&#39;

测试:

public void testRecipientListNegative() throws Exception {
        recipientTester.testHeader("input.xml", "expected.txt");
    }

堆栈跟踪:

org.apache.xmlbeans.XmlRuntimeException: weblogic.xml.query.exceptions.XQueryUserException: line 29, column 5: fase: Unknown msg-name/recipient combination ['ARS_XYZ'/'ESM']! Please check fase recipient list.
        at weblogic.xml.query.runtime.core.Error.fetchNext(Error.java:61)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.runtime.core.IfThenElse.fetchNext(IfThenElse.java:91)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.runtime.core.IfThenElse.fetchNext(IfThenElse.java:91)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.runtime.constructor.AtomicElementConstructor.fetchNext(AtomicElementConstructor.java:129)
        at weblogic.xml.query.iterators.GenericIterator.peekNext(GenericIterator.java:163)
        at weblogic.xml.query.runtime.constructor.SuperElementConstructor.getPhase2(SuperElementConstructor.java:388)
        at weblogic.xml.query.runtime.constructor.PartMatElemConstructor.matEverything(PartMatElemConstructor.java:123)
        at weblogic.xml.query.runtime.constructor.PartMatElemConstructor.fetchNext(PartMatElemConstructor.java:197)
        at weblogic.xml.query.iterators.GenericIterator.peekNext(GenericIterator.java:163)
        at weblogic.xml.query.runtime.constructor.SuperElementConstructor.getPhase2(SuperElementConstructor.java:388)
        at weblogic.xml.query.runtime.constructor.PartMatElemConstructor.fetchNext(PartMatElemConstructor.java:229)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.runtime.core.LetIterator.fetchNext(LetIterator.java:133)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.runtime.core.LetIterator.fetchNext(LetIterator.java:133)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.xdbc.iterators.ItemIterator.fetchNext(ItemIterator.java:86)
        at weblogic.xml.query.iterators.LegacyGenericIterator.next(LegacyGenericIterator.java:109)
        at weblogic.xml.query.runtime.qname.InsertNamespaces.fetchNext(InsertNamespaces.java:216)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at weblogic.xml.query.runtime.core.ExecutionWrapper.fetchNext(ExecutionWrapper.java:88)
        at weblogic.xml.query.iterators.GenericIterator.next(GenericIterator.java:104)
        at org.apache.xmlbeans.impl.store.XqrlImpl$SegmentedIterator.next(XqrlImpl.java:1656)
        at org.apache.xmlbeans.impl.store.XqrlImpl.loadTokenStream(XqrlImpl.java:1410)
        at org.apache.xmlbeans.impl.store.XqrlImpl.loadTokenStream(XqrlImpl.java:1383)
        at org.apache.xmlbeans.impl.store.XqrlImpl.executeQueryToXmlObjects(XqrlImpl.java:1575)
        at org.apache.xmlbeans.impl.store.XqrlImpl.access$000(XqrlImpl.java:53)
        at org.apache.xmlbeans.impl.store.XqrlImpl$CompiledQuery.objectExecute(XqrlImpl.java:302)
        at org.apache.xmlbeans.impl.store.Query.objectExecQuery(Query.java:80)
        at org.apache.xmlbeans.impl.store.Xobj.exec_query(Xobj.java:2525)
        at org.apache.xmlbeans.impl.values.XmlObjectBase.execQuery(XmlObjectBase.java:525)
        at de.db.udg.componenttest.XQueryTester.transform(XQueryTester.java:271)
        at de.db.udg.componenttest.XQueryTester.test(XQueryTester.java:210)
        at de.db.udg.componenttest.XQueryTester.testHeader(XQueryTester.java:172)
        at XQueryTest.testRecipientListNegative(XQueryTest.java:38)

根据stacktrace失败的行:

this.test(xqueryParametersMap, expectedResultFileName);

String result = transform(xqueryParametersMap).xmlText(new XmlOptions().setSavePrettyPrint().setSavePrettyPrintIndent(2));

XmlObject[] resultsObjects = xmlObject.execQuery(xqueryFileContent, options);

0 个答案:

没有答案