diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /filter/qa/cppunit/xslt-test.cxx | |
parent | Initial commit. (diff) | |
download | libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | filter/qa/cppunit/xslt-test.cxx | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/filter/qa/cppunit/xslt-test.cxx b/filter/qa/cppunit/xslt-test.cxx new file mode 100644 index 000000000..4e0c621d6 --- /dev/null +++ b/filter/qa/cppunit/xslt-test.cxx @@ -0,0 +1,207 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <condition_variable> +#include <mutex> + +#include <cppunit/TestAssert.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <sal/types.h> +#include <sal/log.hxx> + +#include <rtl/ref.hxx> + +#include <osl/file.hxx> + +#include <com/sun/star/beans/NamedValue.hpp> +#include <com/sun/star/io/XStreamListener.hpp> +#include <com/sun/star/ucb/SimpleFileAccess.hpp> +#include <com/sun/star/xml/xslt/XSLTTransformer.hpp> + +#include <cppuhelper/implbase.hxx> + +#include <test/bootstrapfixture.hxx> + + +using namespace ::com::sun::star; + + +namespace { + +class XsltFilterTest + : public test::BootstrapFixture +{ +public: + void testXsltCopyOld(); + void testXsltCopyNew(); + + CPPUNIT_TEST_SUITE(XsltFilterTest); + CPPUNIT_TEST(testXsltCopyOld); + CPPUNIT_TEST(testXsltCopyNew); + CPPUNIT_TEST_SUITE_END(); +}; + +class Listener : public ::cppu::WeakImplHelper<io::XStreamListener> +{ +public: + Listener() : m_bDone(false) {} + + void wait() { + std::unique_lock<std::mutex> g(m_mutex); + m_cond.wait(g, [this]() { return m_bDone; }); + } + +private: + std::mutex m_mutex; + std::condition_variable m_cond; + bool m_bDone; + + virtual void SAL_CALL disposing(const lang::EventObject&) noexcept override {} + virtual void SAL_CALL started() noexcept override {} + virtual void SAL_CALL closed() noexcept override { notifyDone(); } + virtual void SAL_CALL terminated() noexcept override { notifyDone(); } + virtual void SAL_CALL error(const uno::Any& e) override + { + notifyDone(); // set on error too, otherwise main thread waits forever + SAL_WARN("filter.xslt", e); + CPPUNIT_FAIL("exception while in XSLT"); + } + + void notifyDone() { + std::scoped_lock<std::mutex> g(m_mutex); + m_bDone = true; + m_cond.notify_all(); + } +}; + +void XsltFilterTest::testXsltCopyNew() +{ + OUString tempDirURL; + osl_getTempDirURL(&tempDirURL.pData); + oslFileHandle tempFile; + OUString tempURL; + osl::File::RC rc = osl::File::createTempFile(nullptr, &tempFile, &tempURL); + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc); + osl_closeFile(tempFile); // close it so xSFA can open it on WNT + + OUString source( + m_directories.getURLFromSrc(u"/filter/source/xsltfilter/xsltfilter.component")); + uno::Sequence<uno::Any> args{ + uno::Any(beans::NamedValue("StylesheetURL", + uno::Any(m_directories.getURLFromSrc(u"/filter/qa/cppunit/data/xslt/copy.xslt")))), + uno::Any(beans::NamedValue("SourceURL", uno::Any(source))), + uno::Any(beans::NamedValue("TargetURL", uno::Any(tempURL))), + uno::Any(beans::NamedValue("SourceBaseURL", + uno::Any(m_directories.getURLFromSrc(u"/filter/source/xsltfilter/")))), + uno::Any(beans::NamedValue("TargetBaseURL", uno::Any(tempDirURL))), + uno::Any(beans::NamedValue("SystemType", uno::Any(OUString()))), + uno::Any(beans::NamedValue("PublicType", uno::Any(OUString()))) + }; + + uno::Reference<ucb::XSimpleFileAccess3> xSFA = + ucb::SimpleFileAccess::create(getComponentContext()); + + uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source); + uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL); + + rtl::Reference<Listener> xListener = new Listener(); + + uno::Reference<xml::xslt::XXSLTTransformer> xXslt( + xml::xslt::XSLTTransformer::create(getComponentContext(), args)); + + xXslt->addListener(xListener); + xXslt->setInputStream(xIn); + xXslt->setOutputStream(xOut); + + xXslt->start(); + + xListener->wait(); + + xIn->closeInput(); + xOut->closeOutput(); + + osl::File foo(tempURL); // apparently it's necessary to open it again? + foo.open(osl_File_OpenFlag_Read); + sal_uInt64 size(0); + foo.getSize(size); + CPPUNIT_ASSERT(size > 1000); // check that something happened + foo.close(); + osl_removeFile(tempURL.pData); +} + +void XsltFilterTest::testXsltCopyOld() +{ + OUString tempDirURL; + osl_getTempDirURL(&tempDirURL.pData); + oslFileHandle tempFile; + OUString tempURL; + osl::File::RC rc = osl::File::createTempFile(nullptr, &tempFile, &tempURL); + CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc); + osl_closeFile(tempFile); // close it so xSFA can open it on WNT + + OUString source( + m_directories.getURLFromSrc(u"/filter/source/xsltfilter/xsltfilter.component")); + uno::Sequence<uno::Any> args{ + uno::Any(beans::NamedValue("StylesheetURL", + uno::Any(m_directories.getURLFromSrc(u"/filter/qa/cppunit/data/xslt/copy.xslt")))), + uno::Any(beans::NamedValue("SourceURL", uno::Any(source))), + uno::Any(beans::NamedValue("TargetURL", uno::Any(tempURL))), + uno::Any(beans::NamedValue("SourceBaseURL", + uno::Any(m_directories.getURLFromSrc(u"/filter/source/xsltfilter/")))), + uno::Any(beans::NamedValue("TargetBaseURL", uno::Any(tempDirURL))), + uno::Any(beans::NamedValue("SystemType", uno::Any(OUString()))), + uno::Any(beans::NamedValue("PublicType", uno::Any(OUString()))) + }; + + uno::Reference<ucb::XSimpleFileAccess3> xSFA = + ucb::SimpleFileAccess::create(getComponentContext()); + + uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source); + uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL); + + rtl::Reference<Listener> xListener = new Listener(); + + uno::Reference<xml::xslt::XXSLTTransformer> xXslt( + getMultiServiceFactory()->createInstance( + "com.sun.star.comp.documentconversion.LibXSLTTransformer"), + uno::UNO_QUERY_THROW); + + uno::Reference<lang::XInitialization> xInit(xXslt, uno::UNO_QUERY_THROW); + xInit->initialize(args); + xXslt->addListener(xListener); + xXslt->setInputStream(xIn); + xXslt->setOutputStream(xOut); + + xXslt->start(); + + xListener->wait(); + + xIn->closeInput(); + xOut->closeOutput(); + + osl::File foo(tempURL); // apparently it's necessary to open it again? + foo.open(osl_File_OpenFlag_Read); + sal_uInt64 size(0); + foo.getSize(size); + CPPUNIT_ASSERT(size > 1000); // check that something happened + foo.close(); + osl_removeFile(tempURL.pData); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest); + +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |