1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: basictest.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // autogenerated file with codegen.pl
33 #include <cppunit/simpleheader.hxx>
35 #include <rtl/ref.hxx>
36 #include <osl/file.hxx>
37 #include <osl/process.h>
38 #include <comphelper/seqstream.hxx>
39 #include <comphelper/sequence.hxx>
40 #include <cppuhelper/compbase1.hxx>
41 #include <cppuhelper/bootstrap.hxx>
42 #include <cppuhelper/basemutex.hxx>
44 #include <com/sun/star/xml/sax/FastToken.hpp>
45 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
46 #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
48 #include "../source/dom/documentbuilder.hxx"
51 using namespace ::DOM
;
52 using namespace ::comphelper
;
53 using namespace ::com::sun::star
;
59 static const char validTestFile
[] =
60 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
61 <office:document-content \
62 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
63 xmlns:xlink=\"http://www.w3.org/1999/xlink\" \
64 office:version=\"1.0\"> \
67 <office:automatic-styles teststyle=\"test\"/> \
70 </office:document-content> \
73 // generates a warning: unsupported xml version, unknown xml:space
75 static const char warningTestFile
[] =
76 "<?xml version=\"47-11.0\" encoding=\"UTF-8\"?> \
77 <office:document-content \
78 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
79 xml:space=\"blafasl\" \
80 office:version=\"1.0\"> \
82 <office:automatic-styles/> \
83 </office:document-content> \
86 // <?xml not at start of file
87 static const char errorTestFile
[] =
88 " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
89 <office:document-content \
90 xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
91 office:version=\"1.0\"> \
93 <office:automatic-styles/> \
94 </office:document-content> \
98 static const char fatalTestFile
[] = "";
101 : public ::cppu::WeakImplHelper1
< xml::sax::XErrorHandler
>
103 sal_uInt32 mnErrCount
;
104 sal_uInt32 mnFatalCount
;
105 sal_uInt32 mnWarnCount
;
107 bool noErrors() const { return !mnErrCount
&& !mnFatalCount
&& !mnWarnCount
; }
109 ErrorHandler() : mnErrCount(0), mnFatalCount(0), mnWarnCount(0)
112 virtual void SAL_CALL
error( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
117 virtual void SAL_CALL
fatalError( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
122 virtual void SAL_CALL
warning( const uno::Any
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
128 struct DocumentHandler
129 : public ::cppu::WeakImplHelper1
< xml::sax::XFastDocumentHandler
>
131 // XFastContextHandler
132 virtual void SAL_CALL
startFastElement( ::sal_Int32 Element
, const uno::Reference
< xml::sax::XFastAttributeList
>& Attribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
134 OSL_TRACE("Seen element: %c with namespace 0x%x",
135 Element
& 0xFFFF, Element
& 0xFFFF0000);
138 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
)
142 virtual void SAL_CALL
endFastElement( ::sal_Int32 Element
) throw (xml::sax::SAXException
, uno::RuntimeException
)
146 virtual void SAL_CALL
endUnknownElement( const ::rtl::OUString
& Namespace
, const ::rtl::OUString
& Name
) throw (xml::sax::SAXException
, uno::RuntimeException
)
150 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
)
155 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
)
160 virtual void SAL_CALL
characters( const ::rtl::OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
)
164 // XFastDocumentHandler
165 virtual void SAL_CALL
startDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
169 virtual void SAL_CALL
endDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
173 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& xLocator
) throw (xml::sax::SAXException
, uno::RuntimeException
)
179 : public ::cppu::WeakImplHelper1
< xml::sax::XFastTokenHandler
>
181 virtual ::sal_Int32 SAL_CALL
getToken( const ::rtl::OUString
& Identifier
) throw (uno::RuntimeException
)
183 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getToken() unexpected call",
188 virtual ::rtl::OUString SAL_CALL
getIdentifier( ::sal_Int32 Token
) throw (uno::RuntimeException
)
190 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getIdentifier() unexpected call",
192 return rtl::OUString();
195 virtual ::sal_Int32 SAL_CALL
getTokenFromUTF8( const uno::Sequence
< ::sal_Int8
>& Identifier
) throw (uno::RuntimeException
)
197 OSL_TRACE("getTokenFromUTF8() %s", (const char*)Identifier
.getConstArray());
198 return Identifier
.getLength() ? Identifier
[0] : 0;
201 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getUTF8Identifier( ::sal_Int32 Token
) throw (uno::RuntimeException
)
203 CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
205 return uno::Sequence
<sal_Int8
>();
209 struct BasicTest
: public CppUnit::TestFixture
211 rtl::Reference
<CDocumentBuilder
> mxDomBuilder
;
212 rtl::Reference
<ErrorHandler
> mxErrHandler
;
213 rtl::Reference
<SequenceInputStream
> mxValidInStream
;
214 rtl::Reference
<SequenceInputStream
> mxWarningInStream
;
215 rtl::Reference
<SequenceInputStream
> mxErrorInStream
;
216 rtl::Reference
<SequenceInputStream
> mxFatalInStream
;
220 // luckily, DOM builder doesn't use service fac, so we need
221 // not bootstrap uno here
222 mxErrHandler
.set( new ErrorHandler() );
223 mxDomBuilder
.set( new CDocumentBuilder(Reference
< XMultiServiceFactory
>() ));
224 mxValidInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)validTestFile
,
225 sizeof(validTestFile
)/sizeof(*validTestFile
))) );
226 mxWarningInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)warningTestFile
,
227 sizeof(warningTestFile
)/sizeof(*warningTestFile
))) );
228 mxErrorInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)errorTestFile
,
229 sizeof(errorTestFile
)/sizeof(*errorTestFile
))) );
230 mxFatalInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)fatalTestFile
,
231 sizeof(fatalTestFile
)/sizeof(*fatalTestFile
))) );
232 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
235 void validInputTest()
237 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #1",
239 uno::Reference
<io::XInputStream
>(
240 mxValidInStream
.get())).is() );
241 CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
242 mxErrHandler
->noErrors() );
245 void warningInputTest()
247 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #2",
249 uno::Reference
<io::XInputStream
>(
250 mxWarningInStream
.get())).is() );
251 CPPUNIT_ASSERT_MESSAGE( "No parse warnings in unclean input file",
252 mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
&& !mxErrHandler
->mnFatalCount
);
255 void errorInputTest()
257 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #3",
259 uno::Reference
<io::XInputStream
>(
260 mxErrorInStream
.get())).is() );
261 CPPUNIT_ASSERT_MESSAGE( "No parse errors in unclean input file",
262 !mxErrHandler
->mnWarnCount
&& mxErrHandler
->mnErrCount
&& !mxErrHandler
->mnFatalCount
);
265 void fatalInputTest()
267 CPPUNIT_ASSERT_MESSAGE( "Broken input file resulted in XDocument",
268 !mxDomBuilder
->parse(
269 uno::Reference
<io::XInputStream
>(
270 mxFatalInStream
.get())).is() );
271 CPPUNIT_ASSERT_MESSAGE( "No fatal parse errors in unclean input file",
272 !mxErrHandler
->mnWarnCount
&& !mxErrHandler
->mnErrCount
&& mxErrHandler
->mnFatalCount
);
275 // Change the following lines only, if you add, remove or rename
276 // member functions of the current class,
277 // because these macros are need by auto register mechanism.
278 CPPUNIT_TEST_SUITE(BasicTest
);
279 CPPUNIT_TEST(validInputTest
);
280 CPPUNIT_TEST(warningInputTest
);
281 CPPUNIT_TEST(errorInputTest
);
282 CPPUNIT_TEST(fatalInputTest
);
283 CPPUNIT_TEST_SUITE_END();
286 struct SerializerTest
: public CppUnit::TestFixture
288 SerializerTest() : mbUnoInitialized(false) {}
290 uno::Reference
<uno::XComponentContext
> mxCtx
;
291 rtl::Reference
<CDocumentBuilder
> mxDomBuilder
;
292 rtl::Reference
<ErrorHandler
> mxErrHandler
;
293 rtl::Reference
<SequenceInputStream
> mxInStream
;
294 rtl::Reference
<DocumentHandler
> mxHandler
;
295 rtl::Reference
<TokenHandler
> mxTokHandler
;
296 uno::Sequence
< beans::Pair
< rtl::OUString
, sal_Int32
> > maRegisteredNamespaces
;
297 bool mbUnoInitialized
;
301 // need working typelib, bootstrap UNO now
302 if( !mbUnoInitialized
)
304 const char* pArgs( getForwardString() );
305 CPPUNIT_ASSERT_MESSAGE("Test file parameter", pArgs
);
307 const rtl::OUString sBaseDir
=rtl::OUString::createFromAscii(pArgs
);
312 ::rtl::OUString aIniUrl
;
313 CPPUNIT_ASSERT_MESSAGE(
314 "Converting ini file to URL",
315 osl_getFileURLFromSystemPath(
316 (sBaseDir
+rtl::OUString::createFromAscii("unoxml_unittest_test.ini")).pData
,
317 &aIniUrl
.pData
) == osl_File_E_None
);
319 mxCtx
= ::cppu::defaultBootstrap_InitialComponentContext(aIniUrl
);
320 CPPUNIT_ASSERT_MESSAGE("Getting component context", mxCtx
.is());
322 catch( uno::Exception
& )
324 CPPUNIT_ASSERT_MESSAGE("Bootstrapping UNO", false);
327 mbUnoInitialized
= true;
330 mxErrHandler
.set( new ErrorHandler() );
331 mxDomBuilder
.set( new CDocumentBuilder(
332 uno::Reference
< lang::XMultiServiceFactory
>(
333 mxCtx
->getServiceManager(),
335 mxInStream
.set( new SequenceInputStream(ByteSequence((sal_Int8
*)validTestFile
,
336 sizeof(validTestFile
)/sizeof(*validTestFile
))) );
337 mxDomBuilder
->setErrorHandler(mxErrHandler
.get());
339 mxHandler
.set( new DocumentHandler() );
340 mxTokHandler
.set( new TokenHandler() );
342 maRegisteredNamespaces
.realloc(2);
343 maRegisteredNamespaces
[0] = beans::make_Pair(
345 RTL_CONSTASCII_USTRINGPARAM(
346 "urn:oasis:names:tc:opendocument:xmlns:office:1.0") ),
347 xml::sax::FastToken::NAMESPACE
);
348 maRegisteredNamespaces
[1] = beans::make_Pair(
350 RTL_CONSTASCII_USTRINGPARAM(
351 "http://www.w3.org/1999/xlink") ),
352 2*xml::sax::FastToken::NAMESPACE
);
355 void serializerTest ()
357 uno::Reference
< xml::dom::XDocument
> xDoc
=
359 uno::Reference
<io::XInputStream
>(
361 CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument",
363 CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
364 mxErrHandler
->noErrors() );
366 uno::Reference
< xml::sax::XSAXSerializable
> xSaxSerializer(
367 xDoc
, uno::UNO_QUERY
);
368 CPPUNIT_ASSERT_MESSAGE( "XSAXSerializable not supported",
369 xSaxSerializer
.is() );
371 uno::Reference
< xml::sax::XFastSAXSerializable
> xFastSaxSerializer(
372 xDoc
, uno::UNO_QUERY
);
373 CPPUNIT_ASSERT_MESSAGE( "XFastSAXSerializable not supported",
374 xSaxSerializer
.is() );
376 xFastSaxSerializer
->fastSerialize( mxHandler
.get(),
378 uno::Sequence
< beans::StringPair
>(),
379 maRegisteredNamespaces
);
382 // Change the following lines only, if you add, remove or rename
383 // member functions of the current class,
384 // because these macros are need by auto register mechanism.
386 CPPUNIT_TEST_SUITE(SerializerTest
);
387 CPPUNIT_TEST(serializerTest
);
388 CPPUNIT_TEST_SUITE_END();
391 // -----------------------------------------------------------------------------
392 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(BasicTest
, "BasicTest");
393 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SerializerTest
, "SerializerTest");
397 // -----------------------------------------------------------------------------
399 // this macro creates an empty function, which will called by the RegisterAllFunctions()
400 // to let the user the possibility to also register some functions by hand.