Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / filter / qa / cppunit / xslt-test.cxx
blob061f25c43133ff4538d984afc9c4a2b6c0b0ee59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <sal/config.h>
12 #include <condition_variable>
13 #include <limits>
14 #include <mutex>
16 #include <cppunit/TestAssert.h>
17 #include <cppunit/TestFixture.h>
18 #include <cppunit/extensions/HelperMacros.h>
19 #include <cppunit/plugin/TestPlugIn.h>
21 #include <sal/types.h>
22 #include <sal/log.hxx>
24 #include <rtl/ref.hxx>
26 #include <osl/file.hxx>
28 #include <com/sun/star/beans/NamedValue.hpp>
29 #include <com/sun/star/io/XStreamListener.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
32 #include <com/sun/star/xml/xslt/XSLTTransformer.hpp>
34 #include <cppuhelper/implbase.hxx>
36 #include <test/bootstrapfixture.hxx>
39 using namespace std;
40 using namespace ::com::sun::star;
43 namespace {
45 class XsltFilterTest
46 : public test::BootstrapFixture
48 public:
49 void testXsltCopyOld();
50 void testXsltCopyNew();
52 CPPUNIT_TEST_SUITE(XsltFilterTest);
53 CPPUNIT_TEST(testXsltCopyOld);
54 CPPUNIT_TEST(testXsltCopyNew);
55 CPPUNIT_TEST_SUITE_END();
58 class Listener : public ::cppu::WeakImplHelper<io::XStreamListener>
60 public:
61 Listener() : m_bDone(false) {}
63 void wait() {
64 std::unique_lock<std::mutex> g(m_mutex);
65 m_cond.wait(g, [this]() { return m_bDone; });
68 private:
69 std::mutex m_mutex;
70 std::condition_variable m_cond;
71 bool m_bDone;
73 virtual void SAL_CALL disposing(const lang::EventObject&) throw() override {}
74 virtual void SAL_CALL started() throw() override {}
75 virtual void SAL_CALL closed() throw() override { notifyDone(); }
76 virtual void SAL_CALL terminated() throw() override { notifyDone(); }
77 virtual void SAL_CALL error(const uno::Any& e) override
79 notifyDone(); // set on error too, otherwise main thread waits forever
80 SAL_WARN("filter.xslt", e);
81 CPPUNIT_FAIL("exception while in XSLT");
84 void notifyDone() {
85 std::scoped_lock<std::mutex> g(m_mutex);
86 m_bDone = true;
87 m_cond.notify_all();
91 void XsltFilterTest::testXsltCopyNew()
93 OUString tempDirURL;
94 osl_getTempDirURL(&tempDirURL.pData);
95 oslFileHandle tempFile;
96 OUString tempURL;
97 osl::File::RC rc = osl::File::createTempFile(nullptr, &tempFile, &tempURL);
98 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
99 osl_closeFile(tempFile); // close it so xSFA can open it on WNT
101 OUString source(
102 m_directories.getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
103 uno::Sequence<uno::Any> args(7);
104 args[0] <<= beans::NamedValue("StylesheetURL",
105 uno::makeAny(m_directories.getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
106 args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
107 args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
108 args[3] <<= beans::NamedValue("SourceBaseURL",
109 uno::makeAny(m_directories.getURLFromSrc("/filter/source/xsltfilter/")));
110 args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
111 args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
112 args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
114 uno::Reference<ucb::XSimpleFileAccess3> xSFA =
115 ucb::SimpleFileAccess::create(getComponentContext());
117 uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
118 uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
120 rtl::Reference<Listener> xListener = new Listener();
122 uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
123 xml::xslt::XSLTTransformer::create(getComponentContext(), args));
125 xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
126 xXslt->setInputStream(xIn);
127 xXslt->setOutputStream(xOut);
129 xXslt->start();
131 xListener->wait();
133 xIn->closeInput();
134 xOut->closeOutput();
136 osl::File foo(tempURL); // apparently it's necessary to open it again?
137 foo.open(osl_File_OpenFlag_Read);
138 sal_uInt64 size(0);
139 foo.getSize(size);
140 CPPUNIT_ASSERT(size > 1000); // check that something happened
141 foo.close();
142 osl_removeFile(tempURL.pData);
145 void XsltFilterTest::testXsltCopyOld()
147 OUString tempDirURL;
148 osl_getTempDirURL(&tempDirURL.pData);
149 oslFileHandle tempFile;
150 OUString tempURL;
151 osl::File::RC rc = osl::File::createTempFile(nullptr, &tempFile, &tempURL);
152 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
153 osl_closeFile(tempFile); // close it so xSFA can open it on WNT
155 OUString source(
156 m_directories.getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
157 uno::Sequence<uno::Any> args(7);
158 args[0] <<= beans::NamedValue("StylesheetURL",
159 uno::makeAny(m_directories.getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
160 args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
161 args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
162 args[3] <<= beans::NamedValue("SourceBaseURL",
163 uno::makeAny(m_directories.getURLFromSrc("/filter/source/xsltfilter/")));
164 args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
165 args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
166 args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
169 uno::Reference<ucb::XSimpleFileAccess3> xSFA =
170 ucb::SimpleFileAccess::create(getComponentContext());
172 uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
173 uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
175 rtl::Reference<Listener> xListener = new Listener();
177 uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
178 getMultiServiceFactory()->createInstance(
179 "com.sun.star.comp.documentconversion.LibXSLTTransformer"),
180 uno::UNO_QUERY_THROW);
182 uno::Reference<lang::XInitialization> xInit(xXslt, uno::UNO_QUERY_THROW);
183 xInit->initialize(args);
184 xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
185 xXslt->setInputStream(xIn);
186 xXslt->setOutputStream(xOut);
188 xXslt->start();
190 xListener->wait();
192 xIn->closeInput();
193 xOut->closeOutput();
195 osl::File foo(tempURL); // apparently it's necessary to open it again?
196 foo.open(osl_File_OpenFlag_Read);
197 sal_uInt64 size(0);
198 foo.getSize(size);
199 CPPUNIT_ASSERT(size > 1000); // check that something happened
200 foo.close();
201 osl_removeFile(tempURL.pData);
204 CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest);
208 CPPUNIT_PLUGIN_IMPLEMENT();
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */