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>
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>
40 using namespace ::com::sun::star
;
46 : public test::BootstrapFixture
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
>
61 Listener() : m_bDone(false) {}
64 std::unique_lock
<std::mutex
> g(m_mutex
);
65 m_cond
.wait(g
, [this]() { return m_bDone
; });
70 std::condition_variable m_cond
;
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");
85 std::scoped_lock
<std::mutex
> g(m_mutex
);
91 void XsltFilterTest::testXsltCopyNew()
94 osl_getTempDirURL(&tempDirURL
.pData
);
95 oslFileHandle tempFile
;
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
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
);
136 osl::File
foo(tempURL
); // apparently it's necessary to open it again?
137 foo
.open(osl_File_OpenFlag_Read
);
140 CPPUNIT_ASSERT(size
> 1000); // check that something happened
142 osl_removeFile(tempURL
.pData
);
145 void XsltFilterTest::testXsltCopyOld()
148 osl_getTempDirURL(&tempDirURL
.pData
);
149 oslFileHandle tempFile
;
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
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
);
195 osl::File
foo(tempURL
); // apparently it's necessary to open it again?
196 foo
.open(osl_File_OpenFlag_Read
);
199 CPPUNIT_ASSERT(size
> 1000); // check that something happened
201 osl_removeFile(tempURL
.pData
);
204 CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest
);
208 CPPUNIT_PLUGIN_IMPLEMENT();
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */