Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <limits>
11 :
12 : #include <cppunit/TestAssert.h>
13 : #include <cppunit/TestFixture.h>
14 : #include <cppunit/extensions/HelperMacros.h>
15 : #include <cppunit/plugin/TestPlugIn.h>
16 :
17 : #include <sal/types.h>
18 :
19 : #include <rtl/ref.hxx>
20 :
21 : #include <osl/file.hxx>
22 : #include <osl/thread.h>
23 :
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>
29 :
30 : #include <cppuhelper/implbase1.hxx>
31 :
32 : #include <test/bootstrapfixture.hxx>
33 :
34 :
35 : using namespace std;
36 : using namespace ::com::sun::star;
37 :
38 :
39 : namespace {
40 :
41 6 : class XsltFilterTest
42 : : public test::BootstrapFixture
43 : {
44 : public:
45 : void testXsltCopyOld();
46 : void testXsltCopyNew();
47 :
48 2 : CPPUNIT_TEST_SUITE(XsltFilterTest);
49 1 : CPPUNIT_TEST(testXsltCopyOld);
50 1 : CPPUNIT_TEST(testXsltCopyNew);
51 2 : CPPUNIT_TEST_SUITE_END();
52 : };
53 :
54 4 : struct Listener : public ::cppu::WeakImplHelper1<io::XStreamListener>
55 : {
56 : bool m_bDone;
57 :
58 2 : Listener() : m_bDone(false) {}
59 :
60 0 : virtual void SAL_CALL disposing(const lang::EventObject&) throw() SAL_OVERRIDE {}
61 2 : virtual void SAL_CALL started() throw() SAL_OVERRIDE { m_bDone = false; }
62 2 : virtual void SAL_CALL closed() throw() SAL_OVERRIDE { m_bDone = true; }
63 0 : virtual void SAL_CALL terminated() throw() SAL_OVERRIDE { m_bDone = true; }
64 0 : virtual void SAL_CALL error(const uno::Any& e) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
65 : {
66 0 : m_bDone = true; // set on error too, otherwise main thread waits forever
67 : SAL_WARN("filter.xslt", "exception " << e);
68 0 : CPPUNIT_FAIL("exception while in XSLT");
69 0 : }
70 : };
71 :
72 1 : void XsltFilterTest::testXsltCopyNew()
73 : {
74 1 : OUString tempDirURL;
75 1 : osl_getTempDirURL(&tempDirURL.pData);
76 : oslFileHandle tempFile;
77 2 : OUString tempURL;
78 1 : osl::File::RC rc = osl::File::createTempFile(0, &tempFile, &tempURL);
79 1 : CPPUNIT_ASSERT(osl::FileBase::E_None == rc);
80 1 : osl_closeFile(tempFile); // close it so xSFA can open it on WNT
81 :
82 : OUString source(
83 2 : getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
84 2 : uno::Sequence<uno::Any> args(7);
85 2 : args[0] <<= beans::NamedValue("StylesheetURL",
86 1 : uno::makeAny(getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
87 1 : args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
88 1 : args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
89 2 : args[3] <<= beans::NamedValue("SourceBaseURL",
90 1 : uno::makeAny(getURLFromSrc("/filter/source/xsltfilter/")));
91 1 : args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
92 1 : args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
93 1 : args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
94 :
95 : uno::Reference<ucb::XSimpleFileAccess3> xSFA =
96 2 : ucb::SimpleFileAccess::create(getComponentContext());
97 :
98 2 : uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
99 2 : uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
100 :
101 2 : rtl::Reference<Listener> xListener = new Listener();
102 :
103 : uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
104 2 : xml::xslt::XSLTTransformer::create(getComponentContext(), args));
105 :
106 1 : xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
107 1 : xXslt->setInputStream(xIn);
108 1 : xXslt->setOutputStream(xOut);
109 :
110 1 : xXslt->start();
111 :
112 : TimeValue delay;
113 1 : delay.Seconds = 0;
114 1 : delay.Nanosec = 1000000;
115 1 : while (!xListener->m_bDone) { osl_waitThread(&delay); }
116 :
117 1 : xIn->closeInput();
118 1 : xOut->closeOutput();
119 :
120 2 : osl::File foo(tempURL); // apparently it's necessary to open it again?
121 1 : foo.open(osl_File_OpenFlag_Read);
122 1 : sal_uInt64 size(0);
123 1 : foo.getSize(size);
124 2 : CPPUNIT_ASSERT(size > 1000); // check that something happened
125 1 : }
126 :
127 1 : void XsltFilterTest::testXsltCopyOld()
128 : {
129 1 : OUString tempDirURL;
130 1 : osl_getTempDirURL(&tempDirURL.pData);
131 : oslFileHandle tempFile;
132 2 : OUString tempURL;
133 1 : osl::File::RC rc = osl::File::createTempFile(0, &tempFile, &tempURL);
134 1 : CPPUNIT_ASSERT(osl::FileBase::E_None == rc);
135 1 : osl_closeFile(tempFile); // close it so xSFA can open it on WNT
136 :
137 : OUString source(
138 2 : getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
139 2 : uno::Sequence<uno::Any> args(7);
140 2 : args[0] <<= beans::NamedValue("StylesheetURL",
141 1 : uno::makeAny(getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
142 1 : args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
143 1 : args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
144 2 : args[3] <<= beans::NamedValue("SourceBaseURL",
145 1 : uno::makeAny(getURLFromSrc("/filter/source/xsltfilter/")));
146 1 : args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
147 1 : args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
148 1 : args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
149 :
150 :
151 : uno::Reference<ucb::XSimpleFileAccess3> xSFA =
152 2 : ucb::SimpleFileAccess::create(getComponentContext());
153 :
154 2 : uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
155 2 : uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
156 :
157 2 : rtl::Reference<Listener> xListener = new Listener();
158 :
159 : uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
160 2 : getMultiServiceFactory()->createInstance(
161 1 : "com.sun.star.comp.documentconversion.LibXSLTTransformer"),
162 2 : uno::UNO_QUERY_THROW);
163 :
164 2 : uno::Reference<lang::XInitialization> xInit(xXslt, uno::UNO_QUERY_THROW);
165 1 : xInit->initialize(args);
166 1 : xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
167 1 : xXslt->setInputStream(xIn);
168 1 : xXslt->setOutputStream(xOut);
169 :
170 1 : xXslt->start();
171 :
172 : TimeValue delay;
173 1 : delay.Seconds = 0;
174 1 : delay.Nanosec = 1000000;
175 1 : while (!xListener->m_bDone) { osl_waitThread(&delay); }
176 :
177 1 : xIn->closeInput();
178 1 : xOut->closeOutput();
179 :
180 2 : osl::File foo(tempURL); // apparently it's necessary to open it again?
181 1 : foo.open(osl_File_OpenFlag_Read);
182 1 : sal_uInt64 size(0);
183 1 : foo.getSize(size);
184 2 : CPPUNIT_ASSERT(size > 1000); // check that something happened
185 1 : }
186 :
187 1 : CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest);
188 :
189 : }
190 :
191 4 : CPPUNIT_PLUGIN_IMPLEMENT();
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|