LCOV - code coverage report
Current view: top level - unoxml/qa/unit - domtest.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 80 101 79.2 %
Date: 2015-06-13 12:38:46 Functions: 42 51 82.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <rtl/ref.hxx>
      21             : #include <rtl/byteseq.hxx>
      22             : #include <osl/file.hxx>
      23             : #include <osl/process.h>
      24             : #include <comphelper/seqstream.hxx>
      25             : #include <comphelper/sequence.hxx>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <cppuhelper/compbase1.hxx>
      28             : #include <cppuhelper/bootstrap.hxx>
      29             : #include <cppuhelper/basemutex.hxx>
      30             : #include <cppunit/TestFixture.h>
      31             : #include <cppunit/extensions/HelperMacros.h>
      32             : #include <cppunit/plugin/TestPlugIn.h>
      33             : #include <unotest/macros_test.hxx>
      34             : #include <test/bootstrapfixture.hxx>
      35             : 
      36             : #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
      37             : #include <com/sun/star/xml/sax/FastToken.hpp>
      38             : #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
      39             : #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
      40             : 
      41             : using namespace ::comphelper;
      42             : using namespace ::com::sun::star;
      43             : using namespace ::com::sun::star::uno;
      44             : using css::xml::dom::XDocumentBuilder;
      45             : using css::xml::dom::DocumentBuilder;
      46             : 
      47             : namespace
      48             : {
      49             : // valid xml
      50             : static const char validTestFile[] =
      51             : "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
      52             :  <office:document-content \
      53             :    xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
      54             :    xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
      55             :    office:version=\"1.0\"> \
      56             :    <office:scripts/> \
      57             :    <xlink:test/> \
      58             :    <office:automatic-styles teststyle=\"test\"/> \
      59             :    <moretest/> \
      60             :     some text \303\266\303\244\303\274 \
      61             :  </office:document-content> \
      62             : ";
      63             : 
      64             : // generates a warning: unsupported xml version, unknown xml:space
      65             : // value
      66             : static const char warningTestFile[] =
      67             : "<?xml version=\"47-11.0\" encoding=\"UTF-8\"?> \
      68             :  <office:document-content \
      69             :    xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
      70             :    xml:space=\"blafasl\" \
      71             :    office:version=\"1.0\"> \
      72             :    <office:scripts/> \
      73             :    <office:automatic-styles/> \
      74             :  </office:document-content> \
      75             : ";
      76             : 
      77             : // <?xml not at start of file
      78             : static const char errorTestFile[] =
      79             : " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
      80             :  <office:document-content \
      81             :    xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
      82             :    office:version=\"1.0\"> \
      83             :    <office:scripts/> \
      84             :    <office:automatic-styles/> \
      85             :  </office:document-content> \
      86             : ";
      87             : 
      88             : // plain empty
      89             : static const char fatalTestFile[] = "";
      90             : 
      91           4 : struct ErrorHandler
      92             :     : public ::cppu::WeakImplHelper1< xml::sax::XErrorHandler >
      93             : {
      94             :     sal_uInt32 mnErrCount;
      95             :     sal_uInt32 mnFatalCount;
      96             :     sal_uInt32 mnWarnCount;
      97             : 
      98           2 :     bool noErrors() const { return !mnErrCount && !mnFatalCount && !mnWarnCount; }
      99             : 
     100           2 :     ErrorHandler() : mnErrCount(0), mnFatalCount(0), mnWarnCount(0)
     101           2 :     {}
     102             : 
     103           0 :     virtual void SAL_CALL error( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     104             :     {
     105           0 :         ++mnErrCount;
     106           0 :     }
     107             : 
     108           0 :     virtual void SAL_CALL fatalError( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     109             :     {
     110           0 :         ++mnFatalCount;
     111           0 :     }
     112             : 
     113           0 :     virtual void SAL_CALL warning( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     114             :     {
     115           0 :         ++mnWarnCount;
     116           0 :     }
     117             : };
     118             : 
     119           3 : struct DocumentHandler
     120             :     : public ::cppu::WeakImplHelper1< xml::sax::XFastDocumentHandler >
     121             : {
     122             :     // XFastContextHandler
     123           5 :     virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     124             :     {
     125             :         SAL_INFO(
     126             :             "unoxml",
     127             :             "Seen element: " << (Element & 0xFFFF) << " with namespace "
     128             :                 << (Element & 0xFFFF0000));
     129           5 :     }
     130             : 
     131           0 :     virtual void SAL_CALL startUnknownElement( const OUString& , const OUString& , const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     132             :     {
     133           0 :     }
     134             : 
     135           5 :     virtual void SAL_CALL endFastElement( ::sal_Int32 ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     136             :     {
     137           5 :     }
     138             : 
     139           0 :     virtual void SAL_CALL endUnknownElement( const OUString&, const OUString& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     140             :     {
     141           0 :     }
     142             : 
     143           5 :     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 , const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     144             :     {
     145           5 :         return this;
     146             :     }
     147             : 
     148           0 :     virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& , const OUString& , const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     149             :     {
     150           0 :         return this;
     151             :     }
     152             : 
     153           5 :     virtual void SAL_CALL characters( const OUString& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     154             :     {
     155           5 :     }
     156             : 
     157             :     // XFastDocumentHandler
     158           1 :     virtual void SAL_CALL startDocument(  ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     159             :     {
     160           1 :     }
     161             : 
     162           1 :     virtual void SAL_CALL endDocument(  ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     163             :     {
     164           1 :     }
     165             : 
     166           0 :     virtual void SAL_CALL setDocumentLocator( const uno::Reference< xml::sax::XLocator >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
     167             :     {
     168           0 :     }
     169             : };
     170             : 
     171           3 : struct TokenHandler
     172             :     : public ::cppu::WeakImplHelper1< xml::sax::XFastTokenHandler >
     173             : {
     174           7 :     virtual ::sal_Int32 SAL_CALL getTokenFromUTF8( const uno::Sequence< ::sal_Int8 >& Identifier ) throw (uno::RuntimeException) SAL_OVERRIDE
     175             :     {
     176             :         OSL_TRACE("getTokenFromUTF8() %s", reinterpret_cast<const char*>(Identifier.getConstArray()));
     177           7 :         return Identifier.getLength() ? Identifier[0] : 0;
     178             :     }
     179             : 
     180           0 :     virtual uno::Sequence< ::sal_Int8 > SAL_CALL getUTF8Identifier( ::sal_Int32 ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     181             :     {
     182           0 :         CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
     183           0 :                                 false );
     184           0 :         return uno::Sequence<sal_Int8>();
     185             :     }
     186             : };
     187             : 
     188           3 : struct BasicTest : public test::BootstrapFixture
     189             : {
     190             :     uno::Reference<XDocumentBuilder>    mxDomBuilder;
     191             :     rtl::Reference<ErrorHandler>        mxErrHandler;
     192             :     rtl::Reference<SequenceInputStream> mxValidInStream;
     193             :     rtl::Reference<SequenceInputStream> mxWarningInStream;
     194             :     rtl::Reference<SequenceInputStream> mxErrorInStream;
     195             :     rtl::Reference<SequenceInputStream> mxFatalInStream;
     196             : 
     197           1 :     virtual void setUp() SAL_OVERRIDE
     198             :     {
     199           1 :         test::BootstrapFixture::setUp();
     200             : 
     201           1 :         mxErrHandler.set( new ErrorHandler() );
     202           1 :         uno::Reference<XDocumentBuilder> xDB( getMultiServiceFactory()->createInstance(OUString("com.sun.star.xml.dom.DocumentBuilder")), uno::UNO_QUERY_THROW );
     203           1 :         mxDomBuilder.set( xDB );
     204           1 :         mxValidInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(validTestFile), SAL_N_ELEMENTS(validTestFile))) );
     205           1 :         mxWarningInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(warningTestFile), SAL_N_ELEMENTS(warningTestFile))) );
     206           1 :         mxErrorInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(errorTestFile), SAL_N_ELEMENTS(errorTestFile))) );
     207           1 :         mxFatalInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(fatalTestFile), SAL_N_ELEMENTS(fatalTestFile))) );
     208           1 :         mxDomBuilder->setErrorHandler(mxErrHandler.get());
     209           1 :     }
     210             : 
     211           1 :     void validInputTest()
     212             :     {
     213           2 :         CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #1",
     214             :                                 mxDomBuilder->parse(
     215             :                                     uno::Reference<io::XInputStream>(
     216           1 :                                         mxValidInStream.get())).is() );
     217           2 :         CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
     218           1 :                                 mxErrHandler->noErrors() );
     219           1 :     }
     220             : /*
     221             :     void warningInputTest()
     222             :     {
     223             :         CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #2",
     224             :                                 mxDomBuilder->parse(
     225             :                                     uno::Reference<io::XInputStream>(
     226             :                                         mxWarningInStream.get())).is() );
     227             :         CPPUNIT_ASSERT_MESSAGE( "No parse warnings in unclean input file",
     228             :                                 mxErrHandler->mnWarnCount && !mxErrHandler->mnErrCount && !mxErrHandler->mnFatalCount );
     229             :     }
     230             : 
     231             :     void errorInputTest()
     232             :     {
     233             :         CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #3",
     234             :                                 mxDomBuilder->parse(
     235             :                                     uno::Reference<io::XInputStream>(
     236             :                                         mxErrorInStream.get())).is() );
     237             :         CPPUNIT_ASSERT_MESSAGE( "No parse errors in unclean input file",
     238             :                                 !mxErrHandler->mnWarnCount && mxErrHandler->mnErrCount && !mxErrHandler->mnFatalCount );
     239             :     }
     240             : 
     241             :     void fatalInputTest()
     242             :     {
     243             :         CPPUNIT_ASSERT_MESSAGE( "Broken input file resulted in XDocument",
     244             :                                 !mxDomBuilder->parse(
     245             :                                     uno::Reference<io::XInputStream>(
     246             :                                         mxFatalInStream.get())).is() );
     247             :         CPPUNIT_ASSERT_MESSAGE( "No fatal parse errors in unclean input file",
     248             :                                 !mxErrHandler->mnWarnCount && !mxErrHandler->mnErrCount && mxErrHandler->mnFatalCount );
     249             :     };
     250             : */
     251             :     // Change the following lines only, if you add, remove or rename
     252             :     // member functions of the current class,
     253             :     // because these macros are need by auto register mechanism.
     254           2 :     CPPUNIT_TEST_SUITE(BasicTest);
     255           1 :     CPPUNIT_TEST(validInputTest);
     256             :     //CPPUNIT_TEST(warningInputTest);
     257             :     //CPPUNIT_TEST(errorInputTest);
     258             :     //CPPUNIT_TEST(fatalInputTest);
     259           5 :     CPPUNIT_TEST_SUITE_END();
     260             : };
     261             : 
     262           3 : struct SerializerTest : public test::BootstrapFixture
     263             : {
     264             :     uno::Reference<XDocumentBuilder>                         mxDomBuilder;
     265             :     rtl::Reference<ErrorHandler>                             mxErrHandler;
     266             :     rtl::Reference<SequenceInputStream>                      mxInStream;
     267             :     rtl::Reference<DocumentHandler>                          mxHandler;
     268             :     rtl::Reference<TokenHandler>                             mxTokHandler;
     269             :     uno::Sequence< beans::Pair< OUString, sal_Int32 > > maRegisteredNamespaces;
     270             : 
     271           1 :     void setUp() SAL_OVERRIDE
     272             :     {
     273           1 :         test::BootstrapFixture::setUp();
     274             : 
     275           1 :         mxErrHandler.set( new ErrorHandler() );
     276           1 :         uno::Reference<XDocumentBuilder> xDB( getMultiServiceFactory()->createInstance(OUString("com.sun.star.xml.dom.DocumentBuilder")), uno::UNO_QUERY_THROW );
     277           1 :         mxDomBuilder.set( xDB );
     278           1 :         mxInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const *>(validTestFile), SAL_N_ELEMENTS(validTestFile))) );
     279           1 :         mxDomBuilder->setErrorHandler(mxErrHandler.get());
     280           1 :         mxHandler.set( new DocumentHandler() );
     281           1 :         mxTokHandler.set( new TokenHandler() );
     282             : 
     283           1 :         maRegisteredNamespaces.realloc(2);
     284           2 :         maRegisteredNamespaces[0] = beans::make_Pair(
     285             :             OUString( "urn:oasis:names:tc:opendocument:xmlns:office:1.0" ),
     286           1 :             xml::sax::FastToken::NAMESPACE);
     287           2 :         maRegisteredNamespaces[1] = beans::make_Pair(
     288             :             OUString( "http://www.w3.org/1999/xlink" ),
     289           2 :             2*xml::sax::FastToken::NAMESPACE);
     290           1 :     }
     291             : 
     292           1 :     void serializerTest ()
     293             :     {
     294             :         uno::Reference< xml::dom::XDocument > xDoc=
     295           1 :             mxDomBuilder->parse(
     296             :                 uno::Reference<io::XInputStream>(
     297           1 :                     mxInStream.get()));
     298           2 :         CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument",
     299           1 :                                 xDoc.is() );
     300           2 :         CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
     301           1 :                                 mxErrHandler->noErrors() );
     302             : 
     303             :         uno::Reference< xml::sax::XSAXSerializable > xSaxSerializer(
     304           2 :             xDoc, uno::UNO_QUERY);
     305           2 :         CPPUNIT_ASSERT_MESSAGE( "XSAXSerializable not supported",
     306           1 :                                 xSaxSerializer.is() );
     307             : 
     308             :         uno::Reference< xml::sax::XFastSAXSerializable > xFastSaxSerializer(
     309           2 :             xDoc, uno::UNO_QUERY);
     310           2 :         CPPUNIT_ASSERT_MESSAGE( "XFastSAXSerializable not supported",
     311           1 :                                 xSaxSerializer.is() );
     312             : 
     313           2 :         xFastSaxSerializer->fastSerialize( mxHandler.get(),
     314           1 :                                            mxTokHandler.get(),
     315             :                                            uno::Sequence< beans::StringPair >(),
     316           4 :                                            maRegisteredNamespaces );
     317           1 :     }
     318             : 
     319             :     // Change the following lines only, if you add, remove or rename
     320             :     // member functions of the current class,
     321             :     // because these macros are need by auto register mechanism.
     322             : 
     323           2 :     CPPUNIT_TEST_SUITE(SerializerTest);
     324           1 :     CPPUNIT_TEST(serializerTest);
     325           5 :     CPPUNIT_TEST_SUITE_END();
     326             : };
     327             : 
     328           1 : CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest);
     329           1 : CPPUNIT_TEST_SUITE_REGISTRATION(SerializerTest);
     330             : }
     331             : 
     332             : // this macro creates an empty function, which will called by the RegisterAllFunctions()
     333             : // to let the user the possibility to also register some functions by hand.
     334           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     335             : 
     336             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11