From 8f75eeebbf354ef58d3d0ced6ea1c2d8df89e64f Mon Sep 17 00:00:00 2001 From: Mohammed Abdul Azeem Date: Mon, 6 Jun 2016 03:07:35 +0530 Subject: GSoC: sax2/ unit tests: Parsed the same test files using XFastParser and built string, we asssert that both the strings built from XParser and XFastParser are identical. Change-Id: Ie1e0dc6b676ca32dd10d75c3cb272b5c8b795ab7 --- sax/CppunitTest_sax_xmlimport.mk | 11 +- sax/qa/cppunit/xmlimport.cxx | 218 ++++++++++++++++++++++++++++++++------- sax/qa/data/nestedns.xml | 2 +- 3 files changed, 190 insertions(+), 41 deletions(-) (limited to 'sax') diff --git a/sax/CppunitTest_sax_xmlimport.mk b/sax/CppunitTest_sax_xmlimport.mk index 71e2792c9c65..14b6488ec192 100644 --- a/sax/CppunitTest_sax_xmlimport.mk +++ b/sax/CppunitTest_sax_xmlimport.mk @@ -22,6 +22,7 @@ $(eval $(call gb_CppunitTest_use_libraries,sax_xmlimport, \ sax \ unotest \ utl \ + salhelper \ $(gb_UWINAPI) \ )) @@ -30,11 +31,18 @@ $(eval $(call gb_CppunitTest_use_api,sax_xmlimport,\ udkapi \ )) -$(eval $(call gb_CppunitTest_use_external,sax_xmlimport,boost_headers)) +$(eval $(call gb_CppunitTest_use_externals,sax_xmlimport, \ + boost_headers \ +)) $(eval $(call gb_CppunitTest_use_ure,sax_xmlimport)) $(eval $(call gb_CppunitTest_use_vcl,sax_xmlimport)) +$(eval $(call gb_CppunitTest_set_include,sax_xmlimport,\ + -I$(SRCDIR)/sax/inc \ + $$(INCLUDE) \ +)) + $(eval $(call gb_CppunitTest_use_components,sax_xmlimport,\ configmgr/source/configmgr \ sax/source/expatwrap/expwrap \ @@ -42,7 +50,6 @@ $(eval $(call gb_CppunitTest_use_components,sax_xmlimport,\ ucb/source/ucp/file/ucpfile1 \ )) - $(eval $(call gb_CppunitTest_use_configuration,sax_xmlimport)) # vim: set noet sw=4 ts=4: diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index 28c2764489e8..394a285658b2 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -25,39 +25,52 @@ #include #include #include +#include +#include #include #include #include +#include +#include +#include #include +#include +#include #include +#include #include #include #include #include #include +#include + namespace { using namespace css; -using namespace css::uno; -using namespace css::io; +using namespace uno; +using namespace io; +using namespace xml::sax; using namespace std; +using namespace ::osl; +using namespace sax_fastparser; Reference< XInputStream > createStreamFromFile ( const OUString & filePath) { Reference< XInputStream > xInputStream; OUString aInStr; - osl::FileBase::getFileURLFromSystemPath(filePath, aInStr); + FileBase::getFileURLFromSystemPath(filePath, aInStr); SvStream* pStream = utl::UcbStreamHelper::CreateStream(aInStr, StreamMode::READ); if(pStream == nullptr) CPPUNIT_ASSERT(false); - uno::Reference xStream(new utl::OStreamWrapper(*pStream)); - xInputStream.set(xStream, uno::UNO_QUERY); + Reference< XStream > xStream(new utl::OStreamWrapper(*pStream)); + xInputStream.set(xStream, UNO_QUERY); return xInputStream; } -class TestDocumentHandler : public cppu::WeakImplHelper< xml::sax::XDocumentHandler > +class TestDocumentHandler : public cppu::WeakImplHelper< XDocumentHandler > { private: OUString m_aStr; @@ -69,16 +82,17 @@ private: public: TestDocumentHandler() {} + const OUString& getString() { return m_aStr; } // XDocumentHandler - virtual void SAL_CALL startDocument() throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL endDocument() throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL startElement( const OUString& aName, const Reference< xml::sax::XAttributeList >& xAttribs ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL endElement( const OUString& aName ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL characters( const OUString& aChars ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; - virtual void SAL_CALL setDocumentLocator( const Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; + virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL startElement( const OUString& aName, const Reference< XAttributeList >& xAttribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endElement( const OUString& aName ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL characters( const OUString& aChars ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL ignorableWhitespace( const OUString& aWhitespaces ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL processingInstruction( const OUString& aTarget, const OUString& aData ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) throw (SAXException, RuntimeException, exception) override; }; OUString TestDocumentHandler::canonicalform(const OUString &sName, const OUString &sValue, bool isElement) @@ -123,7 +137,7 @@ OUString TestDocumentHandler::getNamespace(const OUString &sName) } void SAL_CALL TestDocumentHandler::startDocument() - throw(xml::sax::SAXException, RuntimeException, std::exception) + throw(SAXException, RuntimeException, exception) { m_aStr.clear(); m_aNamespaceStack.emplace_back( make_pair( OUString( "default" ), OUString() ) ); @@ -132,12 +146,12 @@ void SAL_CALL TestDocumentHandler::startDocument() void SAL_CALL TestDocumentHandler::endDocument() - throw(xml::sax::SAXException, RuntimeException, std::exception) + throw(SAXException, RuntimeException, exception) { } -void SAL_CALL TestDocumentHandler::startElement( const OUString& aName, const Reference< xml::sax::XAttributeList >& xAttribs ) - throw( xml::sax::SAXException, RuntimeException, std::exception ) +void SAL_CALL TestDocumentHandler::startElement( const OUString& aName, const Reference< XAttributeList >& xAttribs ) + throw( SAXException, RuntimeException, exception ) { OUString sAttributes; m_aCountStack.push(0); @@ -147,14 +161,14 @@ void SAL_CALL TestDocumentHandler::startElement( const OUString& aName, const Re OUString sAttrValue = xAttribs->getValueByIndex(i); OUString sAttrName = canonicalform(xAttribs->getNameByIndex(i), sAttrValue, false); if (!sAttrName.isEmpty()) - sAttributes = sAttrName + sAttrValue; + sAttributes = sAttributes + sAttrName + sAttrValue; } m_aStr = m_aStr + canonicalform(aName, "", true) + sAttributes; } void SAL_CALL TestDocumentHandler::endElement( const OUString& aName ) - throw( xml::sax::SAXException, RuntimeException, std::exception ) + throw( SAXException, RuntimeException, exception ) { m_aStr = m_aStr + canonicalform(aName, "", true); sal_uInt16 nPopQty = m_aCountStack.top(); @@ -165,38 +179,156 @@ void SAL_CALL TestDocumentHandler::endElement( const OUString& aName ) void SAL_CALL TestDocumentHandler::characters( const OUString& aChars ) - throw(xml::sax::SAXException, RuntimeException, std::exception) + throw(SAXException, RuntimeException, exception) { m_aStr = m_aStr + aChars; } void SAL_CALL TestDocumentHandler::ignorableWhitespace( const OUString& aWhitespaces ) - throw(xml::sax::SAXException, RuntimeException, std::exception) + throw(SAXException, RuntimeException, exception) { m_aStr = m_aStr + aWhitespaces; } void SAL_CALL TestDocumentHandler::processingInstruction( const OUString& /*aTarget*/, const OUString& /*aData*/ ) - throw(xml::sax::SAXException, RuntimeException, std::exception) + throw(SAXException, RuntimeException, exception) +{ +} + + +void SAL_CALL TestDocumentHandler::setDocumentLocator( const Reference< XLocator >& /*xLocator*/ ) + throw(SAXException, RuntimeException, exception) +{ +} + +class TestFastDocumentHandler : public cppu::WeakImplHelper< XFastDocumentHandler > +{ +private: + OUString m_aStr; +public: + const OUString& getString() { return m_aStr; } + + // XFastDocumentHandler + virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) throw (SAXException, RuntimeException, exception) override; + + // XFastContextHandler + virtual void SAL_CALL startFastElement( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endFastElement( sal_Int32 Element ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) throw (SAXException, RuntimeException, exception) override; + virtual Reference< XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual Reference< XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) throw (SAXException, RuntimeException, exception) override; + virtual void SAL_CALL characters( const OUString& aChars ) throw (SAXException, RuntimeException, exception) override; + +}; + +void SAL_CALL TestFastDocumentHandler::startDocument() + throw (SAXException, RuntimeException, exception) +{ + m_aStr.clear(); +} + +void SAL_CALL TestFastDocumentHandler::endDocument() + throw (SAXException, RuntimeException, exception) +{ +} + +void SAL_CALL TestFastDocumentHandler::setDocumentLocator( const Reference< XLocator >&/* xLocator */ ) + throw (SAXException, RuntimeException, exception) +{ +} + +void SAL_CALL TestFastDocumentHandler::startFastElement( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ ) + throw (SAXException, RuntimeException, exception) +{ +} + +void SAL_CALL TestFastDocumentHandler::startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) + throw (SAXException, RuntimeException, exception) +{ + if ( !Namespace.isEmpty() ) + m_aStr = m_aStr + Namespace + ":" + Name; + else + m_aStr = m_aStr + Name; + Sequence< xml::Attribute > unknownAttribs = Attribs->getUnknownAttributes(); + sal_uInt16 len = unknownAttribs.getLength(); + for (sal_uInt16 i = 0; i < len; i++) + { + OUString& rAttrValue = unknownAttribs[i].Value; + OUString& rAttrName = unknownAttribs[i].Name; + OUString& rAttrNamespaceURL = unknownAttribs[i].NamespaceURL; + if ( !rAttrNamespaceURL.isEmpty() ) + m_aStr = m_aStr + rAttrNamespaceURL + ":" + rAttrName + rAttrValue; + else + m_aStr = m_aStr + rAttrName + rAttrValue; + } + +} + +void SAL_CALL TestFastDocumentHandler::endFastElement( sal_Int32/* nElement */) + throw (SAXException, RuntimeException, exception) { } -void SAL_CALL TestDocumentHandler::setDocumentLocator( const Reference< xml::sax::XLocator >& /*xLocator*/ ) - throw(xml::sax::SAXException, RuntimeException, std::exception) +void SAL_CALL TestFastDocumentHandler::endUnknownElement( const OUString& Namespace, const OUString& Name ) + throw (SAXException, RuntimeException, exception) +{ + if ( !Namespace.isEmpty() ) + m_aStr = m_aStr + Namespace + ":" + Name; + else + m_aStr = m_aStr + Name; +} + +Reference< XFastContextHandler > SAL_CALL TestFastDocumentHandler::createFastChildContext( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ ) + throw (SAXException, RuntimeException, exception) +{ + return this; +} + + +Reference< XFastContextHandler > SAL_CALL TestFastDocumentHandler::createUnknownChildContext( const OUString&/* Namespace */, const OUString&/* Name */, const Reference< XFastAttributeList >&/* Attribs */ ) + throw (SAXException, RuntimeException, exception) +{ + return this; +} + +void SAL_CALL TestFastDocumentHandler::characters( const OUString& aChars ) + throw (SAXException, RuntimeException, exception) { + m_aStr = m_aStr + aChars; } +class TestTokenHandler : public cppu::WeakImplHelper< XFastTokenHandler > +{ +public: + virtual sal_Int32 SAL_CALL getTokenFromUTF8( const Sequence& ) + throw (RuntimeException, exception) override + { + return FastToken::DONTKNOW; + } + virtual Sequence< sal_Int8 > SAL_CALL getUTF8Identifier( sal_Int32 ) + throw (RuntimeException, exception) override + { + CPPUNIT_ASSERT_MESSAGE( "getUTF8Identifier: unexpected call", false ); + return Sequence(); + } +}; + class XMLImportTest : public test::BootstrapFixture { private: OUString m_sDirPath; - rtl::Reference< TestDocumentHandler > m_xDocumentHandler; - Reference< xml::sax::XParser > m_xParser; - Reference< lang::XMultiServiceFactory > m_xSMgr; + Reference< TestDocumentHandler > m_xDocumentHandler; + Reference< TestFastDocumentHandler > m_xFastDocumentHandler; + Reference< XParser > m_xParser; + Reference< XFastParser > m_xFastParser; + Reference< XFastTokenHandler > m_xFastTokenHandler; public: virtual void setUp() override; @@ -214,10 +346,15 @@ void XMLImportTest::setUp() { test::BootstrapFixture::setUp(); m_xDocumentHandler.set( new TestDocumentHandler() ); - m_xSMgr = getMultiServiceFactory(); - m_xParser = xml::sax::Parser::create( + m_xFastDocumentHandler.set( new TestFastDocumentHandler() ); + m_xFastTokenHandler.set( new TestTokenHandler() ); + m_xParser = Parser::create( + ::comphelper::getProcessComponentContext() ); + m_xFastParser = FastParser::create( ::comphelper::getProcessComponentContext() ); - m_xParser->setDocumentHandler( m_xDocumentHandler.get() ); + m_xParser->setDocumentHandler( m_xDocumentHandler ); + m_xFastParser->setFastDocumentHandler( m_xFastDocumentHandler ); + m_xFastParser->setTokenHandler( m_xFastTokenHandler ); m_sDirPath = m_directories.getPathFromSrc( "/sax/qa/data/" ); } @@ -234,15 +371,20 @@ void XMLImportTest::parse() for (sal_uInt16 i = 0; i < sizeof( fileNames ) / sizeof( string ); i++) { - Reference< XInputStream > rIS = createStreamFromFile( m_sDirPath + fileNames[i] ); - xml::sax::InputSource source; - source.aInputStream = rIS; + InputSource source; source.sSystemId = "internal"; + + source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] ); m_xParser->parseStream(source); - // OUString aStr = m_xDocumentHandler->getString(); - // OString o = OUStringToOString( aStr, RTL_TEXTENCODING_ASCII_US ); - // CPPUNIT_ASSERT_MESSAGE( std::string(o.pData->buffer), false ); - CPPUNIT_ASSERT(true); + const OUString& rParserStr = m_xDocumentHandler->getString(); + + source.aInputStream = createStreamFromFile( m_sDirPath + fileNames[i] ); + m_xFastParser->parseStream(source); + const OUString& rFastParserStr = m_xFastDocumentHandler->getString(); + + CPPUNIT_ASSERT_EQUAL( rParserStr, rFastParserStr ); + // OString o = OUStringToOString( Str, RTL_TEXTENCODING_ASCII_US ); + // CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false ); } } diff --git a/sax/qa/data/nestedns.xml b/sax/qa/data/nestedns.xml index 33170592938c..18bc4ed34e60 100644 --- a/sax/qa/data/nestedns.xml +++ b/sax/qa/data/nestedns.xml @@ -18,7 +18,7 @@ Philips Aqua Touch Shaver - + AT890 $74 -- cgit