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 // autogenerated file with codegen.pl
22 #include <testshl/simpleheader.hxx>
24 #include <rtl/ref.hxx>
25 #include <osl/file.hxx>
26 #include <osl/process.h>
27 #include <comphelper/seqstream.hxx>
28 #include <comphelper/sequence.hxx>
29 #include <cppuhelper/compbase1.hxx>
30 #include <cppuhelper/bootstrap.hxx>
31 #include <cppuhelper/basemutex.hxx>
33 #include <com/sun/star/xml/sax/FastToken.hpp>
34 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
35 #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
37 #include "../source/dom/documentbuilder.hxx"
40 using namespace ::DOM
;
41 using namespace ::comphelper
;
42 using namespace ::com::sun::star
;
48 static const char validTestFile
[] =
49 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
50 <office:document-content \
51 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
52 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
53 office:version=\"1.0\"> \
56 <office:automatic-styles teststyle=\"test\"/> \
59 </office:document-content> \
62 // generates a warning: unsupported xml version, unknown xml:space
64 static const char warningTestFile
[] =
65 "<?xml version=\"47-11.0\" encoding=\"UTF-8\"?> \
66 <office:document-content \
67 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
68 xml:space=\"blafasl\" \
69 office:version=\"1.0\"> \
71 <office:automatic-styles/> \
72 </office:document-content> \
75 // <?xml not at start of file
76 static const char errorTestFile
[] =
77 " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
78 <office:document-content \
79 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
80 office:version=\"1.0\"> \
82 <office:automatic-styles/> \
83 </office:document-content> \
87 static const char fatalTestFile
[] = "";
90 : public ::cppu::WeakImplHelper1
< xml::sax::XErrorHandler
>
92 sal_uInt32 mnErrCount
;
93 sal_uInt32 mnFatalCount
;
94 sal_uInt32 mnWarnCount
;
96 bool noErrors() const { return !mnErrCount
&& !mnFatalCount
&& !mnWarnCount
; }
98 ErrorHandler() : mnErrCount(0), mnFatalCount(0), mnWarnCount(0)
101 virtual void SAL_CALL
error( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
106 virtual void SAL_CALL
fatalError( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
111 virtual void SAL_CALL
warning( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
117 struct DocumentHandler
118 : public ::cppu::WeakImplHelper1
< xml::sax::XFastDocumentHandler
>
120 // XFastContextHandler
121 virtual void SAL_CALL
startFastElement( ::sal_Int32 Element
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
123 OSL_TRACE("Seen element: %c with namespace 0x%x",
124 Element
& 0xFFFF, Element
& 0xFFFF0000);
127 virtual void SAL_CALL
startUnknownElement( const OUString
& Namespace
, const OUString
& Name
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
131 virtual void SAL_CALL
endFastElement( ::sal_Int32 Element
) throw (xml::sax::SAXException
, uno::RuntimeException
)
135 virtual void SAL_CALL
endUnknownElement( const OUString
& Namespace
, const OUString
& Name
) throw (xml::sax::SAXException
, uno::RuntimeException
)
139 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createFastChildContext( ::sal_Int32 Element
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
144 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext( const OUString
& Namespace
, const OUString
& Name
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
149 virtual void SAL_CALL
characters( const OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
)
153 // XFastDocumentHandler
154 virtual void SAL_CALL
startDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
158 virtual void SAL_CALL
endDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
162 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& xLocator
) throw (xml::sax::SAXException
, uno::RuntimeException
)
168 : public ::cppu::WeakImplHelper1
< xml::sax::XFastTokenHandler
>
170 virtual ::sal_Int32 SAL_CALL
getToken( const OUString
& Identifier
) throw (uno::RuntimeException
)
172 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getToken() unexpected call",
177 virtual OUString SAL_CALL
getIdentifier( ::sal_Int32 Token
) throw (uno::RuntimeException
)
179 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getIdentifier() unexpected call",
184 virtual ::sal_Int32 SAL_CALL
getTokenFromUTF8( const uno::Sequence
< ::sal_Int8
>& Identifier
) throw (uno::RuntimeException
)
186 OSL_TRACE("getTokenFromUTF8() %s", (const char*)Identifier
.getConstArray());
187 return Identifier
.getLength() ? Identifier
[0] : 0;
190 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getUTF8Identifier( ::sal_Int32 Token
) throw (uno::RuntimeException
)
192 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
194 return uno::Sequence
<sal_Int8
>();
198 struct BasicTest
: public CppUnit::TestFixture
200 rtl::Reference
<CDocumentBuilder
> mxDomBuilder
;
201 rtl::Reference
<ErrorHandler
> mxErrHandler
;
202 rtl::Reference
<SequenceInputStream
> mxValidInStream
;
203 rtl::Reference
<SequenceInputStream
> mxWarningInStream
;
204 rtl::Reference
<SequenceInputStream
> mxErrorInStream
;
205 rtl::Reference
<SequenceInputStream
> mxFatalInStream
;
209 // luckily, DOM builder doesn't use service fac, so we need
210 // not bootstrap uno here
211 mxErrHandler
.set( new ErrorHandler() );
212 mxDomBuilder
.set( new CDocumentBuilder(Reference
< XMultiServiceFactory
>() ));
213 mxValidInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)validTestFile
,
214 SAL_N_ELEMENTS(validTestFile
))) );
215 mxWarningInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)warningTestFile
,
216 SAL_N_ELEMENTS(warningTestFile
))) );
217 mxErrorInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)errorTestFile
,
218 SAL_N_ELEMENTS(errorTestFile
))) );
219 mxFatalInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)fatalTestFile
,
220 SAL_N_ELEMENTS(fatalTestFile
))) );
221 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
224 void validInputTest()
226 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #1",
228 uno::Reference
<io::XInputStream
>(
229 mxValidInStream
.get())).is() );
230 CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
231 mxErrHandler
->noErrors() );
234 void warningInputTest()
236 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #2",
238 uno::Reference
<io::XInputStream
>(
239 mxWarningInStream
.get())).is() );
240 CPPUNIT_ASSERT_MESSAGE( "No parse warnings in unclean input file",
241 mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
&& !mxErrHandler
->mnFatalCount
);
244 void errorInputTest()
246 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #3",
248 uno::Reference
<io::XInputStream
>(
249 mxErrorInStream
.get())).is() );
250 CPPUNIT_ASSERT_MESSAGE( "No parse errors in unclean input file",
251 !mxErrHandler
->mnWarnCount
&& mxErrHandler
->mnErrCount
&& !mxErrHandler
->mnFatalCount
);
254 void fatalInputTest()
256 CPPUNIT_ASSERT_MESSAGE( "Broken input file resulted in XDocument",
257 !mxDomBuilder
->parse(
258 uno::Reference
<io::XInputStream
>(
259 mxFatalInStream
.get())).is() );
260 CPPUNIT_ASSERT_MESSAGE( "No fatal parse errors in unclean input file",
261 !mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
&& mxErrHandler
->mnFatalCount
);
264 // Change the following lines only, if you add, remove or rename
265 // member functions of the current class,
266 // because these macros are need by auto register mechanism.
267 CPPUNIT_TEST_SUITE(BasicTest
);
268 CPPUNIT_TEST(validInputTest
);
269 CPPUNIT_TEST(warningInputTest
);
270 CPPUNIT_TEST(errorInputTest
);
271 CPPUNIT_TEST(fatalInputTest
);
272 CPPUNIT_TEST_SUITE_END();
275 struct SerializerTest
: public CppUnit::TestFixture
277 SerializerTest() : mbUnoInitialized(false) {}
279 uno::Reference
<uno::XComponentContext
> mxCtx
;
280 rtl::Reference
<CDocumentBuilder
> mxDomBuilder
;
281 rtl::Reference
<ErrorHandler
> mxErrHandler
;
282 rtl::Reference
<SequenceInputStream
> mxInStream
;
283 rtl::Reference
<DocumentHandler
> mxHandler
;
284 rtl::Reference
<TokenHandler
> mxTokHandler
;
285 uno::Sequence
< beans::Pair
< OUString
, sal_Int32
> > maRegisteredNamespaces
;
286 bool mbUnoInitialized
;
290 // need working typelib, bootstrap UNO now
291 if( !mbUnoInitialized
)
293 const char* pArgs( getForwardString() );
294 CPPUNIT_ASSERT_MESSAGE("Test file parameter", pArgs
);
296 const OUString sBaseDir
=OUString::createFromAscii(pArgs
);
302 CPPUNIT_ASSERT_MESSAGE(
303 "Converting ini file to URL",
304 osl_getFileURLFromSystemPath(
305 (sBaseDir
+rtl::"unoxml_unittest_test.ini").pData
,
306 &aIniUrl
.pData
) == osl_File_E_None
);
308 mxCtx
= ::cppu::defaultBootstrap_InitialComponentContext(aIniUrl
);
309 CPPUNIT_ASSERT_MESSAGE("Getting component context", mxCtx
.is());
311 catch( uno::Exception
& )
313 CPPUNIT_ASSERT_MESSAGE("Bootstrapping UNO", false);
316 mbUnoInitialized
= true;
319 mxErrHandler
.set( new ErrorHandler() );
320 mxDomBuilder
.set( new CDocumentBuilder(
321 uno::Reference
< lang::XMultiServiceFactory
>(
322 mxCtx
->getServiceManager(),
324 mxInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)validTestFile
,
325 SAL_N_ELEMENTS(validTestFile
))) );
326 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
328 mxHandler
.set( new DocumentHandler() );
329 mxTokHandler
.set( new TokenHandler() );
331 maRegisteredNamespaces
.realloc(2);
332 maRegisteredNamespaces
[0] = beans::make_Pair(
333 OUString( "urn:oasis:names:tc:opendocument:xmlns:office:1.0" ),
334 xml::sax::FastToken::NAMESPACE
);
335 maRegisteredNamespaces
[1] = beans::make_Pair(
336 OUString( "http://www.w3.org/1999/xlink" ),
337 2*xml::sax::FastToken::NAMESPACE
);
340 void serializerTest ()
342 uno::Reference
< xml::dom::XDocument
> xDoc
=
344 uno::Reference
<io::XInputStream
>(
346 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument",
348 CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
349 mxErrHandler
->noErrors() );
351 uno::Reference
< xml::sax::XSAXSerializable
> xSaxSerializer(
352 xDoc
, uno::UNO_QUERY
);
353 CPPUNIT_ASSERT_MESSAGE( "XSAXSerializable not supported",
354 xSaxSerializer
.is() );
356 uno::Reference
< xml::sax::XFastSAXSerializable
> xFastSaxSerializer(
357 xDoc
, uno::UNO_QUERY
);
358 CPPUNIT_ASSERT_MESSAGE( "XFastSAXSerializable not supported",
359 xSaxSerializer
.is() );
361 xFastSaxSerializer
->fastSerialize( mxHandler
.get(),
363 uno::Sequence
< beans::StringPair
>(),
364 maRegisteredNamespaces
);
367 // Change the following lines only, if you add, remove or rename
368 // member functions of the current class,
369 // because these macros are need by auto register mechanism.
371 CPPUNIT_TEST_SUITE(SerializerTest
);
372 CPPUNIT_TEST(serializerTest
);
373 CPPUNIT_TEST_SUITE_END();
376 // -----------------------------------------------------------------------------
377 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BasicTest
, "BasicTest");
378 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SerializerTest
, "SerializerTest");
382 // -----------------------------------------------------------------------------
384 // this macro creates an empty function, which will called by the RegisterAllFunctions()
385 // to let the user the possibility to also register some functions by hand.
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */