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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/ref.hxx>
21 #include <rtl/byteseq.hxx>
22 #include <sal/log.hxx>
23 #include <osl/file.hxx>
24 #include <osl/process.h>
25 #include <comphelper/seqstream.hxx>
26 #include <cppuhelper/implbase.hxx>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <cppuhelper/basemutex.hxx>
29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/plugin/TestPlugIn.h>
32 #include <unotest/macros_test.hxx>
33 #include <test/bootstrapfixture.hxx>
35 #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
36 #include <com/sun/star/xml/sax/FastToken.hpp>
37 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
38 #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
39 #include <com/sun/star/xml/sax/SAXParseException.hpp>
41 using namespace ::comphelper
;
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::uno
;
44 using css::xml::dom::XDocumentBuilder
;
45 using css::xml::dom::DocumentBuilder
;
50 static const char validTestFile
[] =
51 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
52 <office:document-content \
53 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
54 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
55 office:version=\"1.0\"> \
58 <office:automatic-styles teststyle=\"test\"/> \
60 some text \303\266\303\244\303\274 \
61 </office:document-content> \
64 // generates a warning: unknown xml:space
66 static const char warningTestFile
[] =
67 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
68 <office:document-content \
69 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
71 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
72 office:version=\"1.0\"> \
75 <office:automatic-styles teststyle=\"test\"/> \
77 some text \303\266\303\244\303\274 \
78 </office:document-content> \
81 // <?xml not at start of file
82 static const char errorTestFile
[] =
83 " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
84 <office:document-content \
85 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
86 office:version=\"1.0\"> \
88 <office:automatic-styles/> \
89 </office:document-content> \
93 : public ::cppu::WeakImplHelper
< xml::sax::XErrorHandler
>
95 sal_uInt32 mnErrCount
;
96 // sal_uInt32 mnFatalCount; // No fatal error counter, as lib2xml doesn't distinguish between error and fatal error
97 // (see See xmlFatalErrMsg from lib2xml/parse.c and __xmlRaiseError from lib2xml/error.c)
98 sal_uInt32 mnWarnCount
;
100 bool noErrors() const { return !mnErrCount
/*&& !mnFatalCount*/ && !mnWarnCount
; }
102 ErrorHandler() : mnErrCount(0), /*mnFatalCount(0),*/ mnWarnCount(0)
105 virtual void SAL_CALL
error( const uno::Any
& ) override
110 // Just implement FatalError function as it is in XErrorHandler
111 // This function is never used, as lib2xml doesn't distinguish between error and fatalerror and calls error functions in both cases
112 virtual void SAL_CALL
fatalError( const uno::Any
& ) override
117 virtual void SAL_CALL
warning( const uno::Any
& ) override
123 struct DocumentHandler
124 : public ::cppu::WeakImplHelper
< xml::sax::XFastDocumentHandler
>
126 // XFastContextHandler
127 virtual void SAL_CALL
startFastElement( ::sal_Int32 Element
, const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
131 "Seen element: " << (Element
& 0xFFFF) << " with namespace "
132 << (Element
& 0xFFFF0000));
135 virtual void SAL_CALL
startUnknownElement( const OUString
& , const OUString
& , const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
139 virtual void SAL_CALL
endFastElement( ::sal_Int32
) override
143 virtual void SAL_CALL
endUnknownElement( const OUString
&, const OUString
& ) override
147 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext( ::sal_Int32
, const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
152 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext( const OUString
& , const OUString
& , const uno::Reference
< xml::sax::XFastAttributeList
>& ) override
157 virtual void SAL_CALL
characters( const OUString
& ) override
161 // XFastDocumentHandler
162 virtual void SAL_CALL
startDocument( ) override
166 virtual void SAL_CALL
endDocument( ) override
170 virtual void SAL_CALL
processingInstruction( const OUString
& /*rTarget*/, const OUString
& /*rData*/ ) override
174 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& ) override
180 : public ::cppu::WeakImplHelper
< xml::sax::XFastTokenHandler
>
182 virtual ::sal_Int32 SAL_CALL
getTokenFromUTF8( const uno::Sequence
< ::sal_Int8
>& Identifier
) override
184 return Identifier
.hasElements() ? Identifier
[0] : 0;
187 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getUTF8Identifier( ::sal_Int32
) override
189 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
191 return uno::Sequence
<sal_Int8
>();
195 struct BasicTest
: public test::BootstrapFixture
197 uno::Reference
<XDocumentBuilder
> mxDomBuilder
;
198 rtl::Reference
<ErrorHandler
> mxErrHandler
;
199 rtl::Reference
<SequenceInputStream
> mxValidInStream
;
200 rtl::Reference
<SequenceInputStream
> mxWarningInStream
;
201 rtl::Reference
<SequenceInputStream
> mxErrorInStream
;
203 virtual void setUp() override
205 test::BootstrapFixture::setUp();
207 mxErrHandler
.set( new ErrorHandler() );
208 uno::Reference
<XDocumentBuilder
> xDB( getMultiServiceFactory()->createInstance("com.sun.star.xml.dom.DocumentBuilder"), uno::UNO_QUERY_THROW
);
209 mxDomBuilder
.set( xDB
);
210 mxValidInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(validTestFile
), SAL_N_ELEMENTS(validTestFile
))) );
211 mxWarningInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(warningTestFile
), SAL_N_ELEMENTS(warningTestFile
))) );
212 mxErrorInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(errorTestFile
), SAL_N_ELEMENTS(errorTestFile
))) );
213 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
216 void validInputTest()
220 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #1",
222 uno::Reference
<io::XInputStream
>(
223 mxValidInStream
.get())).is());
224 CPPUNIT_ASSERT_MESSAGE("Valid input file resulted in parse errors",
225 mxErrHandler
->noErrors());
227 catch (const css::xml::sax::SAXParseException
&)
229 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument (exception thrown)", false);
233 void warningInputTest()
237 // We DON'T expect exception here, as mxWarningInStream is valid XML Doc
238 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #2",
240 uno::Reference
<io::XInputStream
>(
241 mxWarningInStream
.get())).is());
243 catch (const css::xml::sax::SAXParseException
& )
245 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument #2 (exception thrown)", false);
247 CPPUNIT_ASSERT_MESSAGE("No parse warnings in unclean input file",
248 mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
/*&& !mxErrHandler->mnFatalCount*/);
251 void errorInputTest()
255 // We expect exception here, as mxErrorInStream is invalid XML Doc
256 CPPUNIT_ASSERT_MESSAGE("Invalid input file result in XDocument #2!",
257 !mxDomBuilder
->parse(
258 uno::Reference
<io::XInputStream
>(
259 mxErrorInStream
.get())).is());
260 CPPUNIT_ASSERT_MESSAGE("No exception is thrown in unclean input file", false);
262 catch (const css::xml::sax::SAXParseException
&)
264 // It's OK to catch an exception here as we parse incorrect XML file
266 CPPUNIT_ASSERT_MESSAGE("No parse errors in unclean input file",
267 !mxErrHandler
->mnWarnCount
&& mxErrHandler
->mnErrCount
/*&& !mxErrHandler->mnFatalCount*/);
270 // Change the following lines only, if you add, remove or rename
271 // member functions of the current class,
272 // because these macros are need by auto register mechanism.
273 CPPUNIT_TEST_SUITE(BasicTest
);
274 CPPUNIT_TEST(validInputTest
);
275 CPPUNIT_TEST(warningInputTest
);
276 CPPUNIT_TEST(errorInputTest
);
277 CPPUNIT_TEST_SUITE_END();
280 struct SerializerTest
: public test::BootstrapFixture
282 uno::Reference
<XDocumentBuilder
> mxDomBuilder
;
283 rtl::Reference
<ErrorHandler
> mxErrHandler
;
284 rtl::Reference
<SequenceInputStream
> mxInStream
;
285 rtl::Reference
<DocumentHandler
> mxHandler
;
286 rtl::Reference
<TokenHandler
> mxTokHandler
;
287 uno::Sequence
< beans::Pair
< OUString
, sal_Int32
> > maRegisteredNamespaces
;
289 void setUp() override
291 test::BootstrapFixture::setUp();
293 mxErrHandler
.set( new ErrorHandler() );
294 uno::Reference
<XDocumentBuilder
> xDB( getMultiServiceFactory()->createInstance("com.sun.star.xml.dom.DocumentBuilder"), uno::UNO_QUERY_THROW
);
295 mxDomBuilder
.set( xDB
);
296 mxInStream
.set( new SequenceInputStream(css::uno::Sequence
<sal_Int8
>(reinterpret_cast<sal_Int8
const *>(validTestFile
), SAL_N_ELEMENTS(validTestFile
))) );
297 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
298 mxHandler
.set( new DocumentHandler
);
299 mxTokHandler
.set( new TokenHandler
);
301 maRegisteredNamespaces
.realloc(2);
302 maRegisteredNamespaces
[0] = beans::make_Pair(
303 OUString( "urn:oasis:names:tc:opendocument:xmlns:office:1.0" ),
304 xml::sax::FastToken::NAMESPACE
);
305 maRegisteredNamespaces
[1] = beans::make_Pair(
306 OUString( "http://www.w3.org/1999/xlink" ),
307 2*xml::sax::FastToken::NAMESPACE
);
310 void serializerTest ()
314 uno::Reference
< xml::dom::XDocument
> xDoc
=
316 uno::Reference
<io::XInputStream
>(
318 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument",
320 CPPUNIT_ASSERT_MESSAGE("Valid input file resulted in parse errors",
321 mxErrHandler
->noErrors());
323 uno::Reference
< xml::sax::XSAXSerializable
> xSaxSerializer(
324 xDoc
, uno::UNO_QUERY
);
325 CPPUNIT_ASSERT_MESSAGE("XSAXSerializable not supported",
326 xSaxSerializer
.is());
328 uno::Reference
< xml::sax::XFastSAXSerializable
> xFastSaxSerializer(
329 xDoc
, uno::UNO_QUERY
);
330 CPPUNIT_ASSERT_MESSAGE("XFastSAXSerializable not supported",
331 xSaxSerializer
.is());
333 xFastSaxSerializer
->fastSerialize(mxHandler
.get(),
335 uno::Sequence
< beans::StringPair
>(),
336 maRegisteredNamespaces
);
338 catch (const css::xml::sax::SAXParseException
&)
340 CPPUNIT_ASSERT_MESSAGE("Valid input file did not result in XDocument (exception thrown)", false);
344 // Change the following lines only, if you add, remove or rename
345 // member functions of the current class,
346 // because these macros are need by auto register mechanism.
348 CPPUNIT_TEST_SUITE(SerializerTest
);
349 CPPUNIT_TEST(serializerTest
);
350 CPPUNIT_TEST_SUITE_END();
353 CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest
);
354 CPPUNIT_TEST_SUITE_REGISTRATION(SerializerTest
);
357 // this macro creates an empty function, which will called by the RegisterAllFunctions()
358 // to let the user the possibility to also register some functions by hand.
359 CPPUNIT_PLUGIN_IMPLEMENT();
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */