tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / filter / qa / cppunit / xslt-test.cxx
blob3ef87ad118eeb2381883b4ef0f991101650296ab
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 <mutex>
15 #include <cppunit/TestAssert.h>
16 #include <cppunit/extensions/HelperMacros.h>
17 #include <cppunit/plugin/TestPlugIn.h>
19 #include <sal/types.h>
20 #include <sal/log.hxx>
22 #include <rtl/ref.hxx>
24 #include <osl/file.hxx>
26 #include <com/sun/star/beans/NamedValue.hpp>
27 #include <com/sun/star/io/XStreamListener.hpp>
28 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
29 #include <com/sun/star/xml/xslt/XSLTTransformer.hpp>
31 #include <cppuhelper/implbase.hxx>
33 #include <test/bootstrapfixture.hxx>
36 using namespace ::com::sun::star;
39 namespace {
41 class XsltFilterTest
42 : public test::BootstrapFixture
44 public:
45 void testXsltCopyOld();
46 void testXsltCopyNew();
48 CPPUNIT_TEST_SUITE(XsltFilterTest);
49 CPPUNIT_TEST(testXsltCopyOld);
50 CPPUNIT_TEST(testXsltCopyNew);
51 CPPUNIT_TEST_SUITE_END();
54 class Listener : public ::cppu::WeakImplHelper<io::XStreamListener>
56 public:
57 Listener() : m_bDone(false) {}
59 void wait() {
60 std::unique_lock<std::mutex> g(m_mutex);
61 m_cond.wait(g, [this]() { return m_bDone; });
64 private:
65 std::mutex m_mutex;
66 std::condition_variable m_cond;
67 bool m_bDone;
69 virtual void SAL_CALL disposing(const lang::EventObject&) noexcept override {}
70 virtual void SAL_CALL started() noexcept override {}
71 virtual void SAL_CALL closed() noexcept override { notifyDone(); }
72 virtual void SAL_CALL terminated() noexcept override { notifyDone(); }
73 virtual void SAL_CALL error(const uno::Any& e) override
75 notifyDone(); // set on error too, otherwise main thread waits forever
76 SAL_WARN("filter.xslt", e);
77 CPPUNIT_FAIL("exception while in XSLT");
80 void notifyDone() {
81 std::scoped_lock<std::mutex> g(m_mutex);
82 m_bDone = true;
83 m_cond.notify_all();
87 void XsltFilterTest::testXsltCopyNew()
89 OUString tempDirURL;
90 osl_getTempDirURL(&tempDirURL.pData);
91 oslFileHandle tempFile;
92 OUString tempURL;
93 osl::File::RC rc = osl::File::createTempFile(nullptr, &tempFile, &tempURL);
94 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
95 osl_closeFile(tempFile); // close it so xSFA can open it on WNT
97 OUString source(
98 m_directories.getURLFromSrc(u"/filter/source/xsltfilter/xsltfilter.component"));
99 uno::Sequence<uno::Any> args{
100 uno::Any(beans::NamedValue(u"StylesheetURL"_ustr,
101 uno::Any(m_directories.getURLFromSrc(u"/filter/qa/cppunit/data/xslt/copy.xslt")))),
102 uno::Any(beans::NamedValue(u"SourceURL"_ustr, uno::Any(source))),
103 uno::Any(beans::NamedValue(u"TargetURL"_ustr, uno::Any(tempURL))),
104 uno::Any(beans::NamedValue(u"SourceBaseURL"_ustr,
105 uno::Any(m_directories.getURLFromSrc(u"/filter/source/xsltfilter/")))),
106 uno::Any(beans::NamedValue(u"TargetBaseURL"_ustr, uno::Any(tempDirURL))),
107 uno::Any(beans::NamedValue(u"SystemType"_ustr, uno::Any(OUString()))),
108 uno::Any(beans::NamedValue(u"PublicType"_ustr, uno::Any(OUString())))
111 uno::Reference<ucb::XSimpleFileAccess3> xSFA =
112 ucb::SimpleFileAccess::create(getComponentContext());
114 uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
115 uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
117 rtl::Reference<Listener> xListener = new Listener();
119 uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
120 xml::xslt::XSLTTransformer::create(getComponentContext(), args));
122 xXslt->addListener(xListener);
123 xXslt->setInputStream(xIn);
124 xXslt->setOutputStream(xOut);
126 xXslt->start();
128 xListener->wait();
130 xIn->closeInput();
131 xOut->closeOutput();
133 osl::File foo(tempURL); // apparently it's necessary to open it again?
134 foo.open(osl_File_OpenFlag_Read);
135 sal_uInt64 size(0);
136 foo.getSize(size);
137 CPPUNIT_ASSERT(size > 1000); // check that something happened
138 foo.close();
139 osl_removeFile(tempURL.pData);
142 void XsltFilterTest::testXsltCopyOld()
144 OUString tempDirURL;
145 osl_getTempDirURL(&tempDirURL.pData);
146 oslFileHandle tempFile;
147 OUString tempURL;
148 osl::File::RC rc = osl::File::createTempFile(nullptr, &tempFile, &tempURL);
149 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
150 osl_closeFile(tempFile); // close it so xSFA can open it on WNT
152 OUString source(
153 m_directories.getURLFromSrc(u"/filter/source/xsltfilter/xsltfilter.component"));
154 uno::Sequence<uno::Any> args{
155 uno::Any(beans::NamedValue(u"StylesheetURL"_ustr,
156 uno::Any(m_directories.getURLFromSrc(u"/filter/qa/cppunit/data/xslt/copy.xslt")))),
157 uno::Any(beans::NamedValue(u"SourceURL"_ustr, uno::Any(source))),
158 uno::Any(beans::NamedValue(u"TargetURL"_ustr, uno::Any(tempURL))),
159 uno::Any(beans::NamedValue(u"SourceBaseURL"_ustr,
160 uno::Any(m_directories.getURLFromSrc(u"/filter/source/xsltfilter/")))),
161 uno::Any(beans::NamedValue(u"TargetBaseURL"_ustr, uno::Any(tempDirURL))),
162 uno::Any(beans::NamedValue(u"SystemType"_ustr, uno::Any(OUString()))),
163 uno::Any(beans::NamedValue(u"PublicType"_ustr, uno::Any(OUString())))
166 uno::Reference<ucb::XSimpleFileAccess3> xSFA =
167 ucb::SimpleFileAccess::create(getComponentContext());
169 uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
170 uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
172 rtl::Reference<Listener> xListener = new Listener();
174 uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
175 getMultiServiceFactory()->createInstance(
176 u"com.sun.star.comp.documentconversion.LibXSLTTransformer"_ustr),
177 uno::UNO_QUERY_THROW);
179 uno::Reference<lang::XInitialization> xInit(xXslt, uno::UNO_QUERY_THROW);
180 xInit->initialize(args);
181 xXslt->addListener(xListener);
182 xXslt->setInputStream(xIn);
183 xXslt->setOutputStream(xOut);
185 xXslt->start();
187 xListener->wait();
189 xIn->closeInput();
190 xOut->closeOutput();
192 osl::File foo(tempURL); // apparently it's necessary to open it again?
193 foo.open(osl_File_OpenFlag_Read);
194 sal_uInt64 size(0);
195 foo.getSize(size);
196 CPPUNIT_ASSERT(size > 1000); // check that something happened
197 foo.close();
198 osl_removeFile(tempURL.pData);
201 CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest);
205 CPPUNIT_PLUGIN_IMPLEMENT();
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */