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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <rtl/ref.hxx>
21 : #include <rtl/byteseq.hxx>
22 : #include <osl/file.hxx>
23 : #include <osl/process.h>
24 : #include <comphelper/seqstream.hxx>
25 : #include <comphelper/sequence.hxx>
26 : #include <comphelper/processfactory.hxx>
27 : #include <cppuhelper/compbase1.hxx>
28 : #include <cppuhelper/bootstrap.hxx>
29 : #include <cppuhelper/basemutex.hxx>
30 : #include <cppunit/TestFixture.h>
31 : #include <cppunit/extensions/HelperMacros.h>
32 : #include <cppunit/plugin/TestPlugIn.h>
33 : #include <unotest/macros_test.hxx>
34 : #include <test/bootstrapfixture.hxx>
35 :
36 : #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
37 : #include <com/sun/star/xml/sax/FastToken.hpp>
38 : #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
39 : #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
40 :
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;
46 :
47 : namespace
48 : {
49 : // valid xml
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\"> \
56 : <office:scripts/> \
57 : <xlink:test/> \
58 : <office:automatic-styles teststyle=\"test\"/> \
59 : <moretest/> \
60 : some text \303\266\303\244\303\274 \
61 : </office:document-content> \
62 : ";
63 :
64 : // generates a warning: unsupported xml version, unknown xml:space
65 : // value
66 : static const char warningTestFile[] =
67 : "<?xml version=\"47-11.0\" encoding=\"UTF-8\"?> \
68 : <office:document-content \
69 : xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
70 : xml:space=\"blafasl\" \
71 : office:version=\"1.0\"> \
72 : <office:scripts/> \
73 : <office:automatic-styles/> \
74 : </office:document-content> \
75 : ";
76 :
77 : // <?xml not at start of file
78 : static const char errorTestFile[] =
79 : " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \
80 : <office:document-content \
81 : xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" \
82 : office:version=\"1.0\"> \
83 : <office:scripts/> \
84 : <office:automatic-styles/> \
85 : </office:document-content> \
86 : ";
87 :
88 : // plain empty
89 : static const char fatalTestFile[] = "";
90 :
91 8 : struct ErrorHandler
92 : : public ::cppu::WeakImplHelper1< xml::sax::XErrorHandler >
93 : {
94 : sal_uInt32 mnErrCount;
95 : sal_uInt32 mnFatalCount;
96 : sal_uInt32 mnWarnCount;
97 :
98 4 : bool noErrors() const { return !mnErrCount && !mnFatalCount && !mnWarnCount; }
99 :
100 4 : ErrorHandler() : mnErrCount(0), mnFatalCount(0), mnWarnCount(0)
101 4 : {}
102 :
103 0 : virtual void SAL_CALL error( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
104 : {
105 0 : ++mnErrCount;
106 0 : }
107 :
108 0 : virtual void SAL_CALL fatalError( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
109 : {
110 0 : ++mnFatalCount;
111 0 : }
112 :
113 0 : virtual void SAL_CALL warning( const uno::Any& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
114 : {
115 0 : ++mnWarnCount;
116 0 : }
117 : };
118 :
119 6 : struct DocumentHandler
120 : : public ::cppu::WeakImplHelper1< xml::sax::XFastDocumentHandler >
121 : {
122 : // XFastContextHandler
123 10 : virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
124 : {
125 : OSL_TRACE("Seen element: %c with namespace 0x%x",
126 : Element & 0xFFFF, Element & 0xFFFF0000);
127 10 : }
128 :
129 0 : virtual void SAL_CALL startUnknownElement( const OUString& , const OUString& , const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
130 : {
131 0 : }
132 :
133 10 : virtual void SAL_CALL endFastElement( ::sal_Int32 ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
134 : {
135 10 : }
136 :
137 0 : virtual void SAL_CALL endUnknownElement( const OUString&, const OUString& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
138 : {
139 0 : }
140 :
141 10 : virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 , const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
142 : {
143 10 : return this;
144 : }
145 :
146 0 : virtual uno::Reference< xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& , const OUString& , const uno::Reference< xml::sax::XFastAttributeList >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
147 : {
148 0 : return this;
149 : }
150 :
151 10 : virtual void SAL_CALL characters( const OUString& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
152 : {
153 10 : }
154 :
155 : // XFastDocumentHandler
156 2 : virtual void SAL_CALL startDocument( ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
157 : {
158 2 : }
159 :
160 2 : virtual void SAL_CALL endDocument( ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
161 : {
162 2 : }
163 :
164 0 : virtual void SAL_CALL setDocumentLocator( const uno::Reference< xml::sax::XLocator >& ) throw (xml::sax::SAXException, uno::RuntimeException) SAL_OVERRIDE
165 : {
166 0 : }
167 : };
168 :
169 6 : struct TokenHandler
170 : : public ::cppu::WeakImplHelper1< xml::sax::XFastTokenHandler >
171 : {
172 14 : virtual ::sal_Int32 SAL_CALL getTokenFromUTF8( const uno::Sequence< ::sal_Int8 >& Identifier ) throw (uno::RuntimeException) SAL_OVERRIDE
173 : {
174 : OSL_TRACE("getTokenFromUTF8() %s", (const char*)Identifier.getConstArray());
175 14 : return Identifier.getLength() ? Identifier[0] : 0;
176 : }
177 :
178 0 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getUTF8Identifier( ::sal_Int32 ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
179 : {
180 0 : CPPUNIT_ASSERT_MESSAGE( "TokenHandler::getUTF8Identifier() unexpected call",
181 0 : false );
182 0 : return uno::Sequence<sal_Int8>();
183 : }
184 : };
185 :
186 6 : struct BasicTest : public test::BootstrapFixture
187 : {
188 : uno::Reference<XDocumentBuilder> mxDomBuilder;
189 : rtl::Reference<ErrorHandler> mxErrHandler;
190 : rtl::Reference<SequenceInputStream> mxValidInStream;
191 : rtl::Reference<SequenceInputStream> mxWarningInStream;
192 : rtl::Reference<SequenceInputStream> mxErrorInStream;
193 : rtl::Reference<SequenceInputStream> mxFatalInStream;
194 :
195 2 : virtual void setUp() SAL_OVERRIDE
196 : {
197 2 : test::BootstrapFixture::setUp();
198 :
199 2 : mxErrHandler.set( new ErrorHandler() );
200 2 : uno::Reference<XDocumentBuilder> xDB( getMultiServiceFactory()->createInstance(OUString("com.sun.star.xml.dom.DocumentBuilder")), uno::UNO_QUERY_THROW );
201 2 : mxDomBuilder.set( xDB );
202 2 : mxValidInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>((sal_Int8*)validTestFile, SAL_N_ELEMENTS(validTestFile))) );
203 2 : mxWarningInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>((sal_Int8*)warningTestFile, SAL_N_ELEMENTS(warningTestFile))) );
204 2 : mxErrorInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>((sal_Int8*)errorTestFile, SAL_N_ELEMENTS(errorTestFile))) );
205 2 : mxFatalInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>((sal_Int8*)fatalTestFile, SAL_N_ELEMENTS(fatalTestFile))) );
206 2 : mxDomBuilder->setErrorHandler(mxErrHandler.get());
207 2 : }
208 :
209 2 : void validInputTest()
210 : {
211 4 : CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #1",
212 : mxDomBuilder->parse(
213 : uno::Reference<io::XInputStream>(
214 2 : mxValidInStream.get())).is() );
215 4 : CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
216 2 : mxErrHandler->noErrors() );
217 2 : }
218 : /*
219 : void warningInputTest()
220 : {
221 : CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #2",
222 : mxDomBuilder->parse(
223 : uno::Reference<io::XInputStream>(
224 : mxWarningInStream.get())).is() );
225 : CPPUNIT_ASSERT_MESSAGE( "No parse warnings in unclean input file",
226 : mxErrHandler->mnWarnCount && !mxErrHandler->mnErrCount && !mxErrHandler->mnFatalCount );
227 : }
228 :
229 : void errorInputTest()
230 : {
231 : CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument #3",
232 : mxDomBuilder->parse(
233 : uno::Reference<io::XInputStream>(
234 : mxErrorInStream.get())).is() );
235 : CPPUNIT_ASSERT_MESSAGE( "No parse errors in unclean input file",
236 : !mxErrHandler->mnWarnCount && mxErrHandler->mnErrCount && !mxErrHandler->mnFatalCount );
237 : }
238 :
239 : void fatalInputTest()
240 : {
241 : CPPUNIT_ASSERT_MESSAGE( "Broken input file resulted in XDocument",
242 : !mxDomBuilder->parse(
243 : uno::Reference<io::XInputStream>(
244 : mxFatalInStream.get())).is() );
245 : CPPUNIT_ASSERT_MESSAGE( "No fatal parse errors in unclean input file",
246 : !mxErrHandler->mnWarnCount && !mxErrHandler->mnErrCount && mxErrHandler->mnFatalCount );
247 : };
248 : */
249 : // Change the following lines only, if you add, remove or rename
250 : // member functions of the current class,
251 : // because these macros are need by auto register mechanism.
252 4 : CPPUNIT_TEST_SUITE(BasicTest);
253 2 : CPPUNIT_TEST(validInputTest);
254 : //CPPUNIT_TEST(warningInputTest);
255 : //CPPUNIT_TEST(errorInputTest);
256 : //CPPUNIT_TEST(fatalInputTest);
257 4 : CPPUNIT_TEST_SUITE_END();
258 : };
259 :
260 6 : struct SerializerTest : public test::BootstrapFixture
261 : {
262 : uno::Reference<XDocumentBuilder> mxDomBuilder;
263 : rtl::Reference<ErrorHandler> mxErrHandler;
264 : rtl::Reference<SequenceInputStream> mxInStream;
265 : rtl::Reference<DocumentHandler> mxHandler;
266 : rtl::Reference<TokenHandler> mxTokHandler;
267 : uno::Sequence< beans::Pair< OUString, sal_Int32 > > maRegisteredNamespaces;
268 :
269 2 : void setUp() SAL_OVERRIDE
270 : {
271 2 : test::BootstrapFixture::setUp();
272 :
273 2 : mxErrHandler.set( new ErrorHandler() );
274 2 : uno::Reference<XDocumentBuilder> xDB( getMultiServiceFactory()->createInstance(OUString("com.sun.star.xml.dom.DocumentBuilder")), uno::UNO_QUERY_THROW );
275 2 : mxDomBuilder.set( xDB );
276 2 : mxInStream.set( new SequenceInputStream(css::uno::Sequence<sal_Int8>((sal_Int8*)validTestFile, SAL_N_ELEMENTS(validTestFile))) );
277 2 : mxDomBuilder->setErrorHandler(mxErrHandler.get());
278 2 : mxHandler.set( new DocumentHandler() );
279 2 : mxTokHandler.set( new TokenHandler() );
280 :
281 2 : maRegisteredNamespaces.realloc(2);
282 4 : maRegisteredNamespaces[0] = beans::make_Pair(
283 : OUString( "urn:oasis:names:tc:opendocument:xmlns:office:1.0" ),
284 2 : xml::sax::FastToken::NAMESPACE);
285 4 : maRegisteredNamespaces[1] = beans::make_Pair(
286 : OUString( "http://www.w3.org/1999/xlink" ),
287 4 : 2*xml::sax::FastToken::NAMESPACE);
288 2 : }
289 :
290 2 : void serializerTest ()
291 : {
292 : uno::Reference< xml::dom::XDocument > xDoc=
293 2 : mxDomBuilder->parse(
294 : uno::Reference<io::XInputStream>(
295 2 : mxInStream.get()));
296 4 : CPPUNIT_ASSERT_MESSAGE( "Valid input file did not result in XDocument",
297 2 : xDoc.is() );
298 4 : CPPUNIT_ASSERT_MESSAGE( "Valid input file resulted in parse errors",
299 2 : mxErrHandler->noErrors() );
300 :
301 : uno::Reference< xml::sax::XSAXSerializable > xSaxSerializer(
302 4 : xDoc, uno::UNO_QUERY);
303 4 : CPPUNIT_ASSERT_MESSAGE( "XSAXSerializable not supported",
304 2 : xSaxSerializer.is() );
305 :
306 : uno::Reference< xml::sax::XFastSAXSerializable > xFastSaxSerializer(
307 4 : xDoc, uno::UNO_QUERY);
308 4 : CPPUNIT_ASSERT_MESSAGE( "XFastSAXSerializable not supported",
309 2 : xSaxSerializer.is() );
310 :
311 4 : xFastSaxSerializer->fastSerialize( mxHandler.get(),
312 2 : mxTokHandler.get(),
313 : uno::Sequence< beans::StringPair >(),
314 8 : maRegisteredNamespaces );
315 2 : }
316 :
317 : // Change the following lines only, if you add, remove or rename
318 : // member functions of the current class,
319 : // because these macros are need by auto register mechanism.
320 :
321 4 : CPPUNIT_TEST_SUITE(SerializerTest);
322 2 : CPPUNIT_TEST(serializerTest);
323 4 : CPPUNIT_TEST_SUITE_END();
324 : };
325 :
326 2 : CPPUNIT_TEST_SUITE_REGISTRATION(BasicTest);
327 2 : CPPUNIT_TEST_SUITE_REGISTRATION(SerializerTest);
328 : }
329 :
330 : // this macro creates an empty function, which will called by the RegisterAllFunctions()
331 : // to let the user the possibility to also register some functions by hand.
332 8 : CPPUNIT_PLUGIN_IMPLEMENT();
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|