LCOV - code coverage report
Current view: top level - filter/qa/cppunit - xslt-test.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 95 101 94.1 %
Date: 2014-11-03 Functions: 16 20 80.0 %
Legend: Lines: hit not hit

          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          12 : class XsltFilterTest
      42             :     : public test::BootstrapFixture
      43             : {
      44             : public:
      45             :     void testXsltCopyOld();
      46             :     void testXsltCopyNew();
      47             : 
      48           4 :     CPPUNIT_TEST_SUITE(XsltFilterTest);
      49           2 :     CPPUNIT_TEST(testXsltCopyOld);
      50           2 :     CPPUNIT_TEST(testXsltCopyNew);
      51           4 :     CPPUNIT_TEST_SUITE_END();
      52             : };
      53             : 
      54           8 : struct Listener : public ::cppu::WeakImplHelper1<io::XStreamListener>
      55             : {
      56             :     bool m_bDone;
      57             : 
      58           4 :     Listener() : m_bDone(false) {}
      59             : 
      60           0 :     virtual void SAL_CALL disposing(const lang::EventObject&) throw() SAL_OVERRIDE {}
      61           4 :     virtual void SAL_CALL started() throw() SAL_OVERRIDE { m_bDone = false; }
      62           8 :     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           2 : void XsltFilterTest::testXsltCopyNew()
      73             : {
      74           2 :     OUString tempDirURL;
      75           2 :     osl_getTempDirURL(&tempDirURL.pData);
      76             :     oslFileHandle tempFile;
      77           4 :     OUString tempURL;
      78           2 :     osl::File::RC rc = osl::File::createTempFile(0, &tempFile, &tempURL);
      79           2 :     CPPUNIT_ASSERT(osl::FileBase::E_None == rc);
      80           2 :     osl_closeFile(tempFile); // close it so xSFA can open it on WNT
      81             : 
      82             :     OUString source(
      83           4 :             getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
      84           4 :     uno::Sequence<uno::Any> args(7);
      85           4 :     args[0] <<= beans::NamedValue("StylesheetURL",
      86           2 :         uno::makeAny(getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
      87           2 :     args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
      88           2 :     args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
      89           4 :     args[3] <<= beans::NamedValue("SourceBaseURL",
      90           2 :         uno::makeAny(getURLFromSrc("/filter/source/xsltfilter/")));
      91           2 :     args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
      92           2 :     args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
      93           2 :     args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
      94             : 
      95             :     uno::Reference<ucb::XSimpleFileAccess3> xSFA =
      96           4 :         ucb::SimpleFileAccess::create(getComponentContext());
      97             : 
      98           4 :     uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
      99           4 :     uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
     100             : 
     101           4 :     rtl::Reference<Listener> xListener = new Listener();
     102             : 
     103             :     uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
     104           4 :             xml::xslt::XSLTTransformer::create(getComponentContext(), args));
     105             : 
     106           2 :     xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
     107           2 :     xXslt->setInputStream(xIn);
     108           2 :     xXslt->setOutputStream(xOut);
     109             : 
     110           2 :     xXslt->start();
     111             : 
     112             :     TimeValue delay;
     113           2 :     delay.Seconds = 0;
     114           2 :     delay.Nanosec = 1000000;
     115           2 :     while (!xListener->m_bDone) { osl_waitThread(&delay); }
     116             : 
     117           2 :     xIn->closeInput();
     118           2 :     xOut->closeOutput();
     119             : 
     120           4 :     osl::File foo(tempURL); // apparently it's necessary to open it again?
     121           2 :     foo.open(osl_File_OpenFlag_Read);
     122           2 :     sal_uInt64 size(0);
     123           2 :     foo.getSize(size);
     124           2 :     CPPUNIT_ASSERT(size > 1000); // check that something happened
     125           2 :     foo.close();
     126           4 :     osl_removeFile(tempURL.pData);
     127           2 : }
     128             : 
     129           2 : void XsltFilterTest::testXsltCopyOld()
     130             : {
     131           2 :     OUString tempDirURL;
     132           2 :     osl_getTempDirURL(&tempDirURL.pData);
     133             :     oslFileHandle tempFile;
     134           4 :     OUString tempURL;
     135           2 :     osl::File::RC rc = osl::File::createTempFile(0, &tempFile, &tempURL);
     136           2 :     CPPUNIT_ASSERT(osl::FileBase::E_None == rc);
     137           2 :     osl_closeFile(tempFile); // close it so xSFA can open it on WNT
     138             : 
     139             :     OUString source(
     140           4 :             getURLFromSrc("/filter/source/xsltfilter/xsltfilter.component"));
     141           4 :     uno::Sequence<uno::Any> args(7);
     142           4 :     args[0] <<= beans::NamedValue("StylesheetURL",
     143           2 :         uno::makeAny(getURLFromSrc("/filter/qa/cppunit/data/xslt/copy.xslt")));
     144           2 :     args[1] <<= beans::NamedValue("SourceURL", uno::makeAny(source));
     145           2 :     args[2] <<= beans::NamedValue("TargetURL", uno::makeAny(tempURL));
     146           4 :     args[3] <<= beans::NamedValue("SourceBaseURL",
     147           2 :         uno::makeAny(getURLFromSrc("/filter/source/xsltfilter/")));
     148           2 :     args[4] <<= beans::NamedValue("TargetBaseURL", uno::makeAny(tempDirURL));
     149           2 :     args[5] <<= beans::NamedValue("SystemType", uno::makeAny(OUString()));
     150           2 :     args[6] <<= beans::NamedValue("PublicType", uno::makeAny(OUString()));
     151             : 
     152             : 
     153             :     uno::Reference<ucb::XSimpleFileAccess3> xSFA =
     154           4 :         ucb::SimpleFileAccess::create(getComponentContext());
     155             : 
     156           4 :     uno::Reference<io::XInputStream> xIn = xSFA->openFileRead(source);
     157           4 :     uno::Reference<io::XOutputStream> xOut = xSFA->openFileWrite(tempURL);
     158             : 
     159           4 :     rtl::Reference<Listener> xListener = new Listener();
     160             : 
     161             :     uno::Reference<xml::xslt::XXSLTTransformer> xXslt(
     162           4 :         getMultiServiceFactory()->createInstance(
     163           2 :             "com.sun.star.comp.documentconversion.LibXSLTTransformer"),
     164           4 :         uno::UNO_QUERY_THROW);
     165             : 
     166           4 :     uno::Reference<lang::XInitialization> xInit(xXslt, uno::UNO_QUERY_THROW);
     167           2 :     xInit->initialize(args);
     168           2 :     xXslt->addListener(uno::Reference<io::XStreamListener>(xListener.get()));
     169           2 :     xXslt->setInputStream(xIn);
     170           2 :     xXslt->setOutputStream(xOut);
     171             : 
     172           2 :     xXslt->start();
     173             : 
     174             :     TimeValue delay;
     175           2 :     delay.Seconds = 0;
     176           2 :     delay.Nanosec = 1000000;
     177           2 :     while (!xListener->m_bDone) { osl_waitThread(&delay); }
     178             : 
     179           2 :     xIn->closeInput();
     180           2 :     xOut->closeOutput();
     181             : 
     182           4 :     osl::File foo(tempURL); // apparently it's necessary to open it again?
     183           2 :     foo.open(osl_File_OpenFlag_Read);
     184           2 :     sal_uInt64 size(0);
     185           2 :     foo.getSize(size);
     186           2 :     CPPUNIT_ASSERT(size > 1000); // check that something happened
     187           2 :     foo.close();
     188           4 :     osl_removeFile(tempURL.pData);
     189           2 : }
     190             : 
     191           2 : CPPUNIT_TEST_SUITE_REGISTRATION(XsltFilterTest);
     192             : 
     193             : }
     194             : 
     195           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     196             : 
     197             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10