From 8128d597224b55965c4f1bba3d824f193a55041b Mon Sep 17 00:00:00 2001 From: Mohammed Abdul Azeem Date: Thu, 26 May 2016 02:05:13 +0530 Subject: sax2/ unit tests. Test xml files are added, string is built for each file and namespaces are handled. Change-Id: I0ab799ca5c9de7311ccca2a6033a96e02598064f Reviewed-on: https://gerrit.libreoffice.org/25468 Tested-by: Jenkins Reviewed-by: Michael Meeks --- sax/qa/cppunit/xmlimport.cxx | 114 +++++++++++++++++++++++++++++++++-------- sax/qa/data/defaultns.xml | 11 ++++ sax/qa/data/inlinens.xml | 12 +++++ sax/qa/data/multiplens.xml | 13 +++++ sax/qa/data/multiplepfx.xml | 9 ++++ sax/qa/data/namespace.xml | 6 --- sax/qa/data/nestedns.xml | 30 +++++++++++ sax/qa/data/note.xml | 7 --- sax/qa/data/nstoattributes.xml | 17 ++++++ sax/qa/data/simple.xml | 11 ++++ 10 files changed, 197 insertions(+), 33 deletions(-) create mode 100644 sax/qa/data/defaultns.xml create mode 100644 sax/qa/data/inlinens.xml create mode 100644 sax/qa/data/multiplens.xml create mode 100644 sax/qa/data/multiplepfx.xml delete mode 100644 sax/qa/data/namespace.xml create mode 100644 sax/qa/data/nestedns.xml delete mode 100644 sax/qa/data/note.xml create mode 100644 sax/qa/data/nstoattributes.xml create mode 100644 sax/qa/data/simple.xml (limited to 'sax') diff --git a/sax/qa/cppunit/xmlimport.cxx b/sax/qa/cppunit/xmlimport.cxx index 204c45d18fd7..0ba32c15578a 100644 --- a/sax/qa/cppunit/xmlimport.cxx +++ b/sax/qa/cppunit/xmlimport.cxx @@ -32,12 +32,16 @@ #include #include #include +#include +#include +#include namespace { using namespace css; using namespace css::uno; using namespace css::io; +using namespace std; Reference< XInputStream > createStreamFromFile ( const OUString filePath) @@ -56,11 +60,17 @@ Reference< XInputStream > createStreamFromFile ( class TestDocumentHandler : public cppu::WeakImplHelper< xml::sax::XDocumentHandler > { private: - // OUString m_aStr; + OUString m_aStr; + deque< pair > m_aNamespaceStack; + stack m_aCountStack; + + OUString canonicalform(const OUString &sName, const OUString &sValue, bool isElement); + OUString getNamespace(const OUString &sName); public: TestDocumentHandler() {} - //const OUString& getString() { return m_aStr; } + 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; @@ -72,10 +82,53 @@ public: virtual void SAL_CALL setDocumentLocator( const Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, RuntimeException, std::exception) override; }; +OUString TestDocumentHandler::canonicalform(const OUString &sName, const OUString &sValue, bool isElement) +{ + sal_Int16 nIndex = sName.indexOf(":"); + if ( !isElement && sName.match( "xmlns" ) ) + { + m_aCountStack.top() += 1; + if ( nIndex < 0 ) + m_aNamespaceStack.push_back( make_pair( OUString( "default" ), sValue ) ); + else + m_aNamespaceStack.push_back( make_pair( sName.copy( nIndex + 1 ), sValue ) ); + } + else + { + if ( nIndex >= 0 ) + { + OUString sNamespace = getNamespace( sName.copy( 0, nIndex ) ); + return sNamespace + sName.copy(nIndex); + } + else + { + OUString sDefaultns = getNamespace( "default" ); + if ( !isElement || sDefaultns.isEmpty() ) + return sName; + else + return sDefaultns + ":" + sName; + } + } + return OUString(""); +} + +OUString TestDocumentHandler::getNamespace(const OUString &sName) +{ + for (sal_Int16 i = m_aNamespaceStack.size() - 1; i>=0; i--) + { + pair aPair = m_aNamespaceStack.at(i); + if (aPair.first == sName) + return aPair.second; + } + return OUString(""); +} + void SAL_CALL TestDocumentHandler::startDocument() throw(xml::sax::SAXException, RuntimeException, std::exception) { - // m_aStr.clear(); + m_aStr.clear(); + m_aNamespaceStack.emplace_back( make_pair( OUString( "default" ), OUString() ) ); + m_aCountStack.emplace(0); } @@ -84,30 +137,45 @@ void SAL_CALL TestDocumentHandler::endDocument() { } -void SAL_CALL TestDocumentHandler::startElement( const OUString& /*aName*/, const Reference< xml::sax::XAttributeList >& /*xAttribs*/ ) +void SAL_CALL TestDocumentHandler::startElement( const OUString& aName, const Reference< xml::sax::XAttributeList >& xAttribs ) throw( xml::sax::SAXException, RuntimeException, std::exception ) { - // m_aStr = m_aStr + "<" + aName + "> "; + OUString sAttributes; + m_aCountStack.push(0); + sal_uInt16 len = xAttribs->getLength(); + for (sal_uInt16 i=0; igetValueByIndex(i); + OUString sAttrName = canonicalform(xAttribs->getNameByIndex(i), sAttrValue, false); + if (!sAttrName.isEmpty()) + sAttributes = sAttrName + sAttrValue; + } + m_aStr = m_aStr + canonicalform(aName, "", true) + sAttributes; } -void SAL_CALL TestDocumentHandler::endElement( const OUString& /*aName*/ ) +void SAL_CALL TestDocumentHandler::endElement( const OUString& aName ) throw( xml::sax::SAXException, RuntimeException, std::exception ) { - // m_aStr = m_aStr + "\n"; + m_aStr = m_aStr + canonicalform(aName, "", true); + sal_uInt16 nPopQty = m_aCountStack.top(); + for (sal_uInt16 i=0; i rIS = createStreamFromFile( m_sDirPath + fileName ); - xml::sax::InputSource source; - source.aInputStream = rIS; - source.sSystemId = "internal"; - m_xParser->parseStream(source); - // OUString aStr = m_xDocumentHandler->getString(); - // OString o = OUStringToOString( aStr, RTL_TEXTENCODING_ASCII_US ); - // CPPUNIT_ASSERT_MESSAGE( string(o.pData->buffer), false ); - CPPUNIT_ASSERT(true); + OUString fileNames[] = {"simple.xml", "defaultns.xml", "inlinens.xml", + "multiplens.xml", "multiplepfx.xml", + "nstoattributes.xml", "nestedns.xml"}; + + 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; + source.sSystemId = "internal"; + 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); + } } CPPUNIT_TEST_SUITE_REGISTRATION( XMLImportTest ); diff --git a/sax/qa/data/defaultns.xml b/sax/qa/data/defaultns.xml new file mode 100644 index 000000000000..554f4b48f7ea --- /dev/null +++ b/sax/qa/data/defaultns.xml @@ -0,0 +1,11 @@ + + + + War and Peacd + Leo Tolstoy + + + To Kill a Mockingbird + Harper Lee + + \ No newline at end of file diff --git a/sax/qa/data/inlinens.xml b/sax/qa/data/inlinens.xml new file mode 100644 index 000000000000..02c421408569 --- /dev/null +++ b/sax/qa/data/inlinens.xml @@ -0,0 +1,12 @@ + + + + ABC + Computer Science + 7.9 + + + PQR + Artificial Intelligence + + \ No newline at end of file diff --git a/sax/qa/data/multiplens.xml b/sax/qa/data/multiplens.xml new file mode 100644 index 000000000000..e1dc4ce01e89 --- /dev/null +++ b/sax/qa/data/multiplens.xml @@ -0,0 +1,13 @@ + + + + Lionel Messi + 1.70 m + Forward + + + Sachin Ramesh Tendulkar + 165 cm + Right handed + + \ No newline at end of file diff --git a/sax/qa/data/multiplepfx.xml b/sax/qa/data/multiplepfx.xml new file mode 100644 index 000000000000..b7686cad5f69 --- /dev/null +++ b/sax/qa/data/multiplepfx.xml @@ -0,0 +1,9 @@ + + + + + Test Document + For testing purposes only + + + diff --git a/sax/qa/data/namespace.xml b/sax/qa/data/namespace.xml deleted file mode 100644 index 6abb6468e7f8..000000000000 --- a/sax/qa/data/namespace.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - Frobnostication - Moved to - here. - \ No newline at end of file diff --git a/sax/qa/data/nestedns.xml b/sax/qa/data/nestedns.xml new file mode 100644 index 000000000000..33170592938c --- /dev/null +++ b/sax/qa/data/nestedns.xml @@ -0,0 +1,30 @@ + + + + Sherlock Holmes - I + Arthur Conan Doyle + + Sherlock Holmes - II + Arthur Conan Doyle + + Sherlock Holmes - III + Arthur Conan Doyle + + + + Apple iPhone 6s + $324 + + + Philips Aqua Touch Shaver + + + $74 + + + + Macbook Pro + $500 + + + \ No newline at end of file diff --git a/sax/qa/data/note.xml b/sax/qa/data/note.xml deleted file mode 100644 index 830b1c2d95ab..000000000000 --- a/sax/qa/data/note.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - ABC - XYZ - Reminder - Don't forget me this weekend! - \ No newline at end of file diff --git a/sax/qa/data/nstoattributes.xml b/sax/qa/data/nstoattributes.xml new file mode 100644 index 000000000000..dee2edfdcb2a --- /dev/null +++ b/sax/qa/data/nstoattributes.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sax/qa/data/simple.xml b/sax/qa/data/simple.xml new file mode 100644 index 000000000000..67c4fbde57c3 --- /dev/null +++ b/sax/qa/data/simple.xml @@ -0,0 +1,11 @@ + + + + Bryan Cranston + Breaking Bad + + + Peter Dinklage + Game of Thrones + + \ No newline at end of file -- cgit