2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
23 import lib
.MultiMethodTest
;
25 import lib
.StatusException
;
28 import com
.sun
.star
.xml
.sax
.SAXException
;
29 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
30 import com
.sun
.star
.xml
.sax
.XLocator
;
33 * Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
36 * <li><code> startDocument()</code></li>
37 * <li><code> endDocument()</code></li>
38 * <li><code> startElement()</code></li>
39 * <li><code> endElement()</code></li>
40 * <li><code> characters()</code></li>
41 * <li><code> ignorableWhitespace()</code></li>
42 * <li><code> processingInstruction()</code></li>
43 * <li><code> setDocumentLocator()</code></li>
45 * This test needs the following object relations :
47 * <li> <code>'XDocumentHandler.XMLData'</code> (of type <code>String[][]
48 * </code>):the XML data which will be passed to the handler. Each
49 * array of strings corresponds to some handler event. The fisrt
50 * string of event array is the type of the event they can have
51 * the following values :
53 * <li>'start' : startElement() event. The string with index 1
54 * is the name of element, the next array elements are attributes
55 * of XML element in order Name, Type, Value, Name, Type, Value, etc.
57 * <li>'end' : endElement() event. The string with index 1
58 * is the name of element. </li>
59 * <li>'chars' : characters() event. The string with index 1
60 * is characters. </li>
61 * <li>'spaces' : ignorableWhitespace() event. The string with index 1
63 * <li>'instruct' : processingInstruction() event. The string with
64 * index 1 is the target of instruction. The string with index
65 * 2 is the data of instruction. </li>
67 * <li> <code>'XDocumentHandler.ImportChecker'</code>
68 * (of type <code>ifc.xml.sax._XDocumentHandler.ImportChecker</code>) :
69 * this relation must be implementation of the interface above
70 * ant it must check if the XML data was successfully imported to
73 * Test is <b> NOT </b> multithread compliant. <p>
74 * @see com.sun.star.xml.sax.XDocumentHandler
76 public class _XDocumentHandler
extends MultiMethodTest
{
78 private static class DocumentLocator
implements XLocator
{
79 private final PrintWriter log
;
80 public DocumentLocator(PrintWriter log
) {
83 public int getColumnNumber() {
84 log
.println("getColumnNumber() method called.") ;
87 public int getLineNumber() {
88 log
.println("getLineNumber() method called.") ;
91 public String
getPublicId() {
92 log
.println("getPublicId() method called.") ;
93 return "file://d:/file.txt";
95 public String
getSystemId() {
96 log
.println("getSystemId() method called.") ;
102 * This interface implementation must be passed by component test
103 * for checking the whole import process.
105 public interface ImportChecker
{
107 * Returns <code>true</code> if the XML data was successfully
108 * imported, <code>false</code> in other case.
110 boolean checkImport() ;
114 * This interface implementation must be passed by component test
115 * for setting a target document to the import process
117 public interface TargetDocumentSetter
{
119 void setTargetDocument();
122 public XDocumentHandler oObj
= null;
123 private String
[][] xmlData
= null ;
124 private DocumentLocator locator
= null ;
125 private ImportChecker checker
= null ;
126 private boolean locatorResult
= true ;
127 private SAXException locatorException
= null ;
128 private boolean ToBeSkipped
= false;
131 * Retrieves object relations.
132 * @throws StatusException If one of relations not found.
135 public void before() {
136 locator
= new DocumentLocator(log
) ;
137 if (tEnv
.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
138 log
.println("Settings can't be imported in the current Implementation");
141 xmlData
= (String
[][])tEnv
.getObjRelation("XDocumentHandler.XMLData") ;
142 checker
= (ImportChecker
)
143 tEnv
.getObjRelation("XDocumentHandler.ImportChecker") ;
145 TargetDocumentSetter targetDocSet
= (TargetDocumentSetter
)
146 tEnv
.getObjRelation("XDocumentHandler.TargetDocumentSetter");
148 if (xmlData
== null || checker
== null) throw new StatusException
149 (Status
.failed("Relation wasn't found")) ;
151 if (targetDocSet
!= null){
154 log
.println("object relation 'XDocumentHandler.TargetDocumentSetter' not used.");
155 log
.println("be sure that the test have a target to write throu");
160 * Sets document locator to dummy locator implementation and
161 * calls the <code>startDocument</code> method. <p>
163 * Has <b> OK </b> status if no runtime exceptions occurred.
165 public void _startDocument() {
167 tRes
.tested("startDocument()", Status
.skipped(true));
172 oObj
.setDocumentLocator(locator
) ;
173 } catch (SAXException e
) {
174 locatorException
= e
;
175 locatorResult
= false ;
178 boolean result
= true ;
180 oObj
.startDocument() ;
181 } catch (SAXException e
) {
182 e
.printStackTrace(log
) ;
183 log
.println("Wrapped exception :" + e
.WrappedException
) ;
187 tRes
.tested("startDocument()", result
) ;
191 * This test is finally executed. It finishes XML data
192 * transferring with <code>endDocument</code> method call. <p>
194 * Has <b>OK</b> status if no exceptions occurred during
195 * the whole transferring and if the appropriate changes
196 * occurred in the document where XML data was trnsfered to.
197 * This check is performed by checker relation.
199 public void _endDocument() {
201 tRes
.tested("endDocument()", Status
.skipped(true));
204 requiredMethod("startElement()") ;
205 executeMethod("endElement()") ;
206 executeMethod("characters()") ;
207 executeMethod("ignorableWhitespace()") ;
208 executeMethod("processingInstruction()") ;
210 boolean result
= true ;
213 } catch (SAXException e
) {
214 e
.printStackTrace(log
) ;
215 log
.println("Wrapped exception :" + e
.WrappedException
) ;
219 log
.println("Check if import was successful ...") ;
220 result
&= checker
.checkImport() ;
222 tRes
.tested("endDocument()", result
) ;
226 * Transfers XML data obtained from relation
227 * <code>'XDocumentHandler.XMLData'</code>. <p>
229 * Has <b>OK</b> status if no exceptions occurred during XML data
230 * transferring in <code>startDocument</code> and
231 * <code>startElement</code> method tests. <p>
233 * Exact checking of XML transfer is made in <code>endDocument</code>
235 public void _startElement() {
237 tRes
.tested("startElement()", Status
.skipped(true));
240 boolean result
= true ;
243 log
.println("StartElement Processing XML data ...") ;
244 for(int i
= 0; i
< xmlData
.length
; i
++) {
245 String
[] elem
= xmlData
[i
] ;
247 if ("start".equals(elem
[0])) {
249 String tagName
= elem
[1] ;
251 XMLTools
.AttributeList attr
= new XMLTools
.AttributeList() ;
252 for (int j
= 2; j
< elem
.length
; j
+=3) {
253 attr
.add(elem
[j
], elem
[j
+1], elem
[j
+2]);
254 xmlTag
+= " " + elem
[j
] + "(" + elem
[j
+1] +
255 ")=\"" + elem
[j
+2] + "\"" ;
259 log
.println(xmlTag
) ;
260 oObj
.startElement(tagName
, attr
) ;
262 if ("end".equals(elem
[0])) {
263 log
.println("</" + elem
[1] + ">") ;
264 oObj
.endElement(elem
[1]) ;
266 if ("chars".equals(elem
[0])) {
267 log
.println("'" + elem
[1] + "'") ;
268 oObj
.characters(elem
[1]) ;
270 if ("spaces".equals(elem
[0])) {
271 log
.println("(spaces)'" + elem
[1] + "'") ;
272 oObj
.ignorableWhitespace(elem
[1]) ;
274 if ("instruct".equals(elem
[0])) {
275 log
.println("<?" + elem
[1] + " " + elem
[2] + "?>") ;
276 oObj
.processingInstruction(elem
[1], elem
[2]) ;
278 log
.println("!!! Bad object relation !!!") ;
279 throw new StatusException(Status
.failed("Bad relation")) ;
282 } catch (SAXException e
) {
283 e
.printStackTrace(log
) ;
284 log
.println("Wrapped exception :" + e
.WrappedException
) ;
288 tRes
.tested("startElement()", result
) ;
294 * Has <b>OK</b> status if no exceptions occurred during XML data
295 * transferring in <code>startDocument</code> and
296 * <code>startElement</code> method tests.
298 public void _endElement() {
300 tRes
.tested("endElement()", Status
.skipped(true));
303 requiredMethod("startElement()") ;
305 boolean result
= true ;
307 tRes
.tested("endElement()", result
) ;
313 * Has <b>OK</b> status if no exceptions occurred during XML data
314 * transferring in <code>startDocument</code> and
315 * <code>startElement</code> method tests.
317 public void _characters() {
319 tRes
.tested("characters()", Status
.skipped(true));
322 requiredMethod("startElement()") ;
324 boolean result
= true ;
326 tRes
.tested("characters()", result
) ;
332 * Has <b>OK</b> status if no exceptions occurred during XML data
333 * transferring in <code>startDocument</code> and
334 * <code>startElement</code> method tests.
336 public void _ignorableWhitespace() {
338 tRes
.tested("ignorableWhitespace()", Status
.skipped(true));
341 requiredMethod("startElement()") ;
343 boolean result
= true ;
345 tRes
.tested("ignorableWhitespace()", result
) ;
351 * Has <b>OK</b> status if no exceptions occurred during XML data
352 * transferring in <code>startDocument</code> and
353 * <code>startElement</code> method tests.
355 public void _processingInstruction() {
357 tRes
.tested("processingInstruction()", Status
.skipped(true));
360 requiredMethod("startElement()") ;
362 boolean result
= true ;
364 tRes
.tested("processingInstruction()", result
) ;
370 * Has <b>OK</b> status if no exceptions occurred during XML data
371 * transferring in <code>startDocument</code> and
372 * <code>startElement</code> method tests.
374 public void _setDocumentLocator() {
376 tRes
.tested("setDocumentLocator()", Status
.skipped(true));
379 executeMethod("endDocument()") ;
381 boolean result
= locatorResult
;
382 if (locatorException
!= null) {
383 log
.println("Exception occurred during setDocumentLocator() call:") ;
384 locatorException
.printStackTrace(log
) ;
385 log
.println("Wrapped exception :"
386 + locatorException
.WrappedException
) ;
390 tRes
.tested("setDocumentLocator()", result
) ;