Bump version to 5.0-14
[LibreOffice.git] / filter / qa / cppunit / xslt-test.cxx
blobf9945eac89529ffe36b2f16566f7eaeb131c736a
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 <limits>
12 #include <cppunit/TestAssert.h>
13 #include <cppunit/TestFixture.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <cppunit/plugin/TestPlugIn.h>
17 #include <sal/types.h>
19 #include <rtl/ref.hxx>
21 #include <osl/file.hxx>
22 #include <osl/thread.h>
24 #include <com/sun/star/beans/NamedValue.hpp>
25 #include <com/sun/star/io/XStreamListener.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
28 #include <com/sun/star/xml/xslt/XSLTTransformer.hpp>
30 #include <cppuhelper/implbase1.hxx>
32 #include <test/bootstrapfixture.hxx>
35 using namespace std;
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 struct Listener : public ::cppu::WeakImplHelper1<io::XStreamListener>
56 bool m_bDone;
58 Listener() : m_bDone(false) {}
60 virtual void SAL_CALL disposing(const lang::EventObject&) throw() SAL_OVERRIDE {}
61 virtual void SAL_CALL started() throw() SAL_OVERRIDE { m_bDone = false; }
62 virtual void SAL_CALL closed() throw() SAL_OVERRIDE { m_bDone = true; }
63 virtual void SAL_CALL terminated() throw() SAL_OVERRIDE { m_bDone = true; }
64 virtual void SAL_CALL error(const uno::Any& e) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
66 m_bDone = true; // set on error too, otherwise main thread waits forever
67 SAL_WARN("filter.xslt", "exception " << e);
68 CPPUNIT_FAIL("exception while in XSLT");
72 void XsltFilterTest::testXsltCopyNew()
74 OUString tempDirURL;
75 osl_getTempDirURL(&tempDirURL.pData);
76 oslFileHandle tempFile;
77 OUString tempURL;
78 osl::File::RC rc = osl::File::createTempFile(0, &tempFile, &tempURL);
79 CPPUNIT_ASSERT(osl::FileBase::E_None == rc);
80 osl_closeFile(tempFile); // close it so xSFA can open it on WNT
82 OUString source(
83 getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
84 uno::Sequence<uno::Any> args(7);
85 args[0] <<= beans::NamedValue("StylesheetURL",
86 uno::makeAny(getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
87 args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
88 args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
89 args[3] <<= beans::NamedValue("SourceBaseURL",
90 uno::makeAny(getURLFromSrc("/filter/source/xsltfilter/")));
91 args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
92 args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
93 args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
95 uno::Reference<ucb::XSimpleFileAccess3> xSFA =
96 ucb::SimpleFileAccess::create(getComponentContext());
98 uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
99 uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
101 rtl::Reference<Listener> xListener = new Listener();
103 uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
104 xml::xslt::XSLTTransformer::create(getComponentContext(), args));
106 xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
107 xXslt->setInputStream(xIn);
108 xXslt->setOutputStream(xOut);
110 xXslt->start();
112 TimeValue delay;
113 delay.Seconds = 0;
114 delay.Nanosec = 1000000;
115 while (!xListener->m_bDone) { osl_waitThread(&delay); }
117 xIn->closeInput();
118 xOut->closeOutput();
120 osl::File foo(tempURL); // apparently it's necessary to open it again?
121 foo.open(osl_File_OpenFlag_Read);
122 sal_uInt64 size(0);
123 foo.getSize(size);
124 CPPUNIT_ASSERT(size > 1000); // check that something happened
125 foo.close();
126 osl_removeFile(tempURL.pData);
129 void XsltFilterTest::testXsltCopyOld()
131 OUString tempDirURL;
132 osl_getTempDirURL(&tempDirURL.pData);
133 oslFileHandle tempFile;
134 OUString tempURL;
135 osl::File::RC rc = osl::File::createTempFile(0, &tempFile, &tempURL);
136 CPPUNIT_ASSERT(osl::FileBase::E_None == rc);
137 osl_closeFile(tempFile); // close it so xSFA can open it on WNT
139 OUString source(
140 getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
141 uno::Sequence<uno::Any> args(7);
142 args[0] <<= beans::NamedValue("StylesheetURL",
143 uno::makeAny(getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
144 args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
145 args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
146 args[3] <<= beans::NamedValue("SourceBaseURL",
147 uno::makeAny(getURLFromSrc("/filter/source/xsltfilter/")));
148 args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
149 args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
150 args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
153 uno::Reference<ucb::XSimpleFileAccess3> xSFA =
154 ucb::SimpleFileAccess::create(getComponentContext());
156 uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
157 uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
159 rtl::Reference<Listener> xListener = new Listener();
161 uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
162 getMultiServiceFactory()->createInstance(
163 "com.sun.star.comp.documentconversion.LibXSLTTransformer"),
164 uno::UNO_QUERY_THROW);
166 uno::Reference<lang::XInitialization> xInit(xXslt, uno::UNO_QUERY_THROW);
167 xInit->initialize(args);
168 xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
169 xXslt->setInputStream(xIn);
170 xXslt->setOutputStream(xOut);
172 xXslt->start();
174 TimeValue delay;
175 delay.Seconds = 0;
176 delay.Nanosec = 1000000;
177 while (!xListener->m_bDone) { osl_waitThread(&delay); }
179 xIn->closeInput();
180 xOut->closeOutput();
182 osl::File foo(tempURL); // apparently it's necessary to open it again?
183 foo.open(osl_File_OpenFlag_Read);
184 sal_uInt64 size(0);
185 foo.getSize(size);
186 CPPUNIT_ASSERT(size > 1000); // check that something happened
187 foo.close();
188 osl_removeFile(tempURL.pData);
191 CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest);
195 CPPUNIT_PLUGIN_IMPLEMENT();
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */