1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // autogenerated file with codegen.pl
31 #include <testshl/simpleheader.hxx>
33 #include <rtl/ref.hxx>
34 #include <osl/file.hxx>
35 #include <osl/process.h>
36 #include <comphelper/seqstream.hxx>
37 #include <comphelper/sequence.hxx>
38 #include <cppuhelper/compbase1.hxx>
39 #include <cppuhelper/bootstrap.hxx>
40 #include <cppuhelper/basemutex.hxx>
42 #include <com/sun/star/xml/sax/FastToken.hpp>
43 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
44 #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
46 #include "../source/dom/documentbuilder.hxx"
49 using namespace ::DOM
;
50 using namespace ::comphelper
;
51 using namespace ::com::sun::star
;
57 static const char validTestFile
[] =
58 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
59 <office:document-content \
60 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
61 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
62 office:version=\"1.0\"> \
65 <office:automatic-styles teststyle=\"test\"/> \
68 </office:document-content> \
71 // generates a warning: unsupported xml version, unknown xml:space
73 static const char warningTestFile
[] =
74 "<?xml version=\"47-11.0\" encoding=\"UTF-8\"?> \
75 <office:document-content \
76 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
77 xml:space=\"blafasl\" \
78 office:version=\"1.0\"> \
80 <office:automatic-styles/> \
81 </office:document-content> \
84 // <?xml not at start of file
85 static const char errorTestFile
[] =
86 " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
87 <office:document-content \
88 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
89 office:version=\"1.0\"> \
91 <office:automatic-styles/> \
92 </office:document-content> \
96 static const char fatalTestFile
[] = "";
99 : public ::cppu::WeakImplHelper1
< xml::sax::XErrorHandler
>
101 sal_uInt32 mnErrCount
;
102 sal_uInt32 mnFatalCount
;
103 sal_uInt32 mnWarnCount
;
105 bool noErrors() const { return !mnErrCount
&& !mnFatalCount
&& !mnWarnCount
; }
107 ErrorHandler() : mnErrCount(0), mnFatalCount(0), mnWarnCount(0)
110 virtual void SAL_CALL
error( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
115 virtual void SAL_CALL
fatalError( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
120 virtual void SAL_CALL
warning( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
126 struct DocumentHandler
127 : public ::cppu::WeakImplHelper1
< xml::sax::XFastDocumentHandler
>
129 // XFastContextHandler
130 virtual void SAL_CALL
startFastElement( ::sal_Int32 Element
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
132 OSL_TRACE("Seen element: %c with namespace 0x%x",
133 Element
& 0xFFFF, Element
& 0xFFFF0000);
136 virtual void SAL_CALL
startUnknownElement( const ::rtl::OUString
& Namespace
, const ::rtl::OUString
& Name
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
140 virtual void SAL_CALL
endFastElement( ::sal_Int32 Element
) throw (xml::sax::SAXException
, uno::RuntimeException
)
144 virtual void SAL_CALL
endUnknownElement( const ::rtl::OUString
& Namespace
, const ::rtl::OUString
& Name
) throw (xml::sax::SAXException
, uno::RuntimeException
)
148 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
)
153 virtual uno::Reference
< xml::sax::XFastContextHandler
> SAL_CALL
createUnknownChildContext( const ::rtl::OUString
& Namespace
, const ::rtl::OUString
& Name
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
158 virtual void SAL_CALL
characters( const ::rtl::OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
)
162 // XFastDocumentHandler
163 virtual void SAL_CALL
startDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
167 virtual void SAL_CALL
endDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
171 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& xLocator
) throw (xml::sax::SAXException
, uno::RuntimeException
)
177 : public ::cppu::WeakImplHelper1
< xml::sax::XFastTokenHandler
>
179 virtual ::sal_Int32 SAL_CALL
getToken( const ::rtl::OUString
& Identifier
) throw (uno::RuntimeException
)
181 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getToken() unexpected call",
186 virtual ::rtl::OUString SAL_CALL
getIdentifier( ::sal_Int32 Token
) throw (uno::RuntimeException
)
188 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getIdentifier() unexpected call",
190 return rtl::OUString();
193 virtual ::sal_Int32 SAL_CALL
getTokenFromUTF8( const uno::Sequence
< ::sal_Int8
>& Identifier
) throw (uno::RuntimeException
)
195 OSL_TRACE("getTokenFromUTF8() %s", (const char*)Identifier
.getConstArray());
196 return Identifier
.getLength() ? Identifier
[0] : 0;
199 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getUTF8Identifier( ::sal_Int32 Token
) throw (uno::RuntimeException
)
201 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
203 return uno::Sequence
<sal_Int8
>();
207 struct BasicTest
: public CppUnit::TestFixture
209 rtl::Reference
<CDocumentBuilder
> mxDomBuilder
;
210 rtl::Reference
<ErrorHandler
> mxErrHandler
;
211 rtl::Reference
<SequenceInputStream
> mxValidInStream
;
212 rtl::Reference
<SequenceInputStream
> mxWarningInStream
;
213 rtl::Reference
<SequenceInputStream
> mxErrorInStream
;
214 rtl::Reference
<SequenceInputStream
> mxFatalInStream
;
218 // luckily, DOM builder doesn't use service fac, so we need
219 // not bootstrap uno here
220 mxErrHandler
.set( new ErrorHandler() );
221 mxDomBuilder
.set( new CDocumentBuilder(Reference
< XMultiServiceFactory
>() ));
222 mxValidInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)validTestFile
,
223 SAL_N_ELEMENTS(validTestFile
))) );
224 mxWarningInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)warningTestFile
,
225 SAL_N_ELEMENTS(warningTestFile
))) );
226 mxErrorInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)errorTestFile
,
227 SAL_N_ELEMENTS(errorTestFile
))) );
228 mxFatalInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)fatalTestFile
,
229 SAL_N_ELEMENTS(fatalTestFile
))) );
230 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
233 void validInputTest()
235 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #1",
237 uno::Reference
<io::XInputStream
>(
238 mxValidInStream
.get())).is() );
239 CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
240 mxErrHandler
->noErrors() );
243 void warningInputTest()
245 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #2",
247 uno::Reference
<io::XInputStream
>(
248 mxWarningInStream
.get())).is() );
249 CPPUNIT_ASSERT_MESSAGE( "No parse warnings in unclean input file",
250 mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
&& !mxErrHandler
->mnFatalCount
);
253 void errorInputTest()
255 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #3",
257 uno::Reference
<io::XInputStream
>(
258 mxErrorInStream
.get())).is() );
259 CPPUNIT_ASSERT_MESSAGE( "No parse errors in unclean input file",
260 !mxErrHandler
->mnWarnCount
&& mxErrHandler
->mnErrCount
&& !mxErrHandler
->mnFatalCount
);
263 void fatalInputTest()
265 CPPUNIT_ASSERT_MESSAGE( "Broken input file resulted in XDocument",
266 !mxDomBuilder
->parse(
267 uno::Reference
<io::XInputStream
>(
268 mxFatalInStream
.get())).is() );
269 CPPUNIT_ASSERT_MESSAGE( "No fatal parse errors in unclean input file",
270 !mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
&& mxErrHandler
->mnFatalCount
);
273 // Change the following lines only, if you add, remove or rename
274 // member functions of the current class,
275 // because these macros are need by auto register mechanism.
276 CPPUNIT_TEST_SUITE(BasicTest
);
277 CPPUNIT_TEST(validInputTest
);
278 CPPUNIT_TEST(warningInputTest
);
279 CPPUNIT_TEST(errorInputTest
);
280 CPPUNIT_TEST(fatalInputTest
);
281 CPPUNIT_TEST_SUITE_END();
284 struct SerializerTest
: public CppUnit::TestFixture
286 SerializerTest() : mbUnoInitialized(false) {}
288 uno::Reference
<uno::XComponentContext
> mxCtx
;
289 rtl::Reference
<CDocumentBuilder
> mxDomBuilder
;
290 rtl::Reference
<ErrorHandler
> mxErrHandler
;
291 rtl::Reference
<SequenceInputStream
> mxInStream
;
292 rtl::Reference
<DocumentHandler
> mxHandler
;
293 rtl::Reference
<TokenHandler
> mxTokHandler
;
294 uno::Sequence
< beans::Pair
< rtl::OUString
, sal_Int32
> > maRegisteredNamespaces
;
295 bool mbUnoInitialized
;
299 // need working typelib, bootstrap UNO now
300 if( !mbUnoInitialized
)
302 const char* pArgs( getForwardString() );
303 CPPUNIT_ASSERT_MESSAGE("Test file parameter", pArgs
);
305 const rtl::OUString sBaseDir
=rtl::OUString::createFromAscii(pArgs
);
310 ::rtl::OUString aIniUrl
;
311 CPPUNIT_ASSERT_MESSAGE(
312 "Converting ini file to URL",
313 osl_getFileURLFromSystemPath(
314 (sBaseDir
+rtl::"unoxml_unittest_test.ini").pData
,
315 &aIniUrl
.pData
) == osl_File_E_None
);
317 mxCtx
= ::cppu::defaultBootstrap_InitialComponentContext(aIniUrl
);
318 CPPUNIT_ASSERT_MESSAGE("Getting component context", mxCtx
.is());
320 catch( uno::Exception
& )
322 CPPUNIT_ASSERT_MESSAGE("Bootstrapping UNO", false);
325 mbUnoInitialized
= true;
328 mxErrHandler
.set( new ErrorHandler() );
329 mxDomBuilder
.set( new CDocumentBuilder(
330 uno::Reference
< lang::XMultiServiceFactory
>(
331 mxCtx
->getServiceManager(),
333 mxInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)validTestFile
,
334 SAL_N_ELEMENTS(validTestFile
))) );
335 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
337 mxHandler
.set( new DocumentHandler() );
338 mxTokHandler
.set( new TokenHandler() );
340 maRegisteredNamespaces
.realloc(2);
341 maRegisteredNamespaces
[0] = beans::make_Pair(
342 rtl::OUString( "urn:oasis:names:tc:opendocument:xmlns:office:1.0" ),
343 xml::sax::FastToken::NAMESPACE
);
344 maRegisteredNamespaces
[1] = beans::make_Pair(
345 rtl::OUString( "http://www.w3.org/1999/xlink" ),
346 2*xml::sax::FastToken::NAMESPACE
);
349 void serializerTest ()
351 uno::Reference
< xml::dom::XDocument
> xDoc
=
353 uno::Reference
<io::XInputStream
>(
355 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument",
357 CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
358 mxErrHandler
->noErrors() );
360 uno::Reference
< xml::sax::XSAXSerializable
> xSaxSerializer(
361 xDoc
, uno::UNO_QUERY
);
362 CPPUNIT_ASSERT_MESSAGE( "XSAXSerializable not supported",
363 xSaxSerializer
.is() );
365 uno::Reference
< xml::sax::XFastSAXSerializable
> xFastSaxSerializer(
366 xDoc
, uno::UNO_QUERY
);
367 CPPUNIT_ASSERT_MESSAGE( "XFastSAXSerializable not supported",
368 xSaxSerializer
.is() );
370 xFastSaxSerializer
->fastSerialize( mxHandler
.get(),
372 uno::Sequence
< beans::StringPair
>(),
373 maRegisteredNamespaces
);
376 // Change the following lines only, if you add, remove or rename
377 // member functions of the current class,
378 // because these macros are need by auto register mechanism.
380 CPPUNIT_TEST_SUITE(SerializerTest
);
381 CPPUNIT_TEST(serializerTest
);
382 CPPUNIT_TEST_SUITE_END();
385 // -----------------------------------------------------------------------------
386 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BasicTest
, "BasicTest");
387 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SerializerTest
, "SerializerTest");
391 // -----------------------------------------------------------------------------
393 // this macro creates an empty function, which will called by the RegisterAllFunctions()
394 // to let the user the possibility to also register some functions by hand.
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */