1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/config.h>
12 #include <condition_variable>
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
;
42 : public test::BootstrapFixture
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
>
57 Listener() : m_bDone(false) {}
60 std::unique_lock
<std::mutex
> g(m_mutex
);
61 m_cond
.wait(g
, [this]() { return m_bDone
; });
66 std::condition_variable m_cond
;
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");
81 std::scoped_lock
<std::mutex
> g(m_mutex
);
87 void XsltFilterTest::testXsltCopyNew()
90 osl_getTempDirURL(&tempDirURL
.pData
);
91 oslFileHandle tempFile
;
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
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
);
133 osl::File
foo(tempURL
); // apparently it's necessary to open it again?
134 foo
.open(osl_File_OpenFlag_Read
);
137 CPPUNIT_ASSERT(size
> 1000); // check that something happened
139 osl_removeFile(tempURL
.pData
);
142 void XsltFilterTest::testXsltCopyOld()
145 osl_getTempDirURL(&tempDirURL
.pData
);
146 oslFileHandle tempFile
;
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
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
);
192 osl::File
foo(tempURL
); // apparently it's necessary to open it again?
193 foo
.open(osl_File_OpenFlag_Read
);
196 CPPUNIT_ASSERT(size
> 1000); // check that something happened
198 osl_removeFile(tempURL
.pData
);
201 CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest
);
205 CPPUNIT_PLUGIN_IMPLEMENT();
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */