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 first
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){
152 log
.println("object relation 'XDocumentHandler.TargetDocumentSetter' not used.");
153 log
.println("be sure that the test have a target to write through");
158 * Sets document locator to dummy locator implementation and
159 * calls the <code>startDocument</code> method. <p>
161 * Has <b> OK </b> status if no runtime exceptions occurred.
163 public void _startDocument() {
165 tRes
.tested("startDocument()", Status
.skipped(true));
170 oObj
.setDocumentLocator(locator
) ;
171 } catch (SAXException e
) {
172 locatorException
= e
;
173 locatorResult
= false ;
176 boolean result
= true ;
178 oObj
.startDocument() ;
179 } catch (SAXException e
) {
180 e
.printStackTrace(log
) ;
181 log
.println("Wrapped exception :" + e
.WrappedException
) ;
185 tRes
.tested("startDocument()", result
) ;
189 * This test is finally executed. It finishes XML data
190 * transferring with <code>endDocument</code> method call. <p>
192 * Has <b>OK</b> status if no exceptions occurred during
193 * the whole transferring and if the appropriate changes
194 * occurred in the document where XML data was transferred to.
195 * This check is performed by checker relation.
197 public void _endDocument() {
199 tRes
.tested("endDocument()", Status
.skipped(true));
202 requiredMethod("startElement()") ;
203 executeMethod("endElement()") ;
204 executeMethod("characters()") ;
205 executeMethod("ignorableWhitespace()") ;
206 executeMethod("processingInstruction()") ;
208 boolean result
= true ;
211 } catch (SAXException e
) {
212 e
.printStackTrace(log
) ;
213 log
.println("Wrapped exception :" + e
.WrappedException
) ;
217 log
.println("Check if import was successful ...") ;
218 result
&= checker
.checkImport() ;
220 tRes
.tested("endDocument()", result
) ;
224 * Transfers XML data obtained from relation
225 * <code>'XDocumentHandler.XMLData'</code>. <p>
227 * Has <b>OK</b> status if no exceptions occurred during XML data
228 * transferring in <code>startDocument</code> and
229 * <code>startElement</code> method tests. <p>
231 * Exact checking of XML transfer is made in <code>endDocument</code>
233 public void _startElement() {
235 tRes
.tested("startElement()", Status
.skipped(true));
238 boolean result
= true ;
241 log
.println("StartElement Processing XML data ...") ;
242 for(int i
= 0; i
< xmlData
.length
; i
++) {
243 String
[] elem
= xmlData
[i
] ;
244 if ("start".equals(elem
[0])) {
245 StringBuilder xmlTag
= new StringBuilder();
247 String tagName
= elem
[1] ;
248 xmlTag
.append(tagName
);
249 XMLTools
.AttributeList attr
= new XMLTools
.AttributeList() ;
250 for (int j
= 2; j
< elem
.length
; j
+=3) {
251 attr
.add(elem
[j
], elem
[j
+1], elem
[j
+2]);
252 xmlTag
.append(" ").append(elem
[j
]).append("(").append(elem
[j
+1]).append(
253 ")=\"").append(elem
[j
+2]).append("\"");
257 log
.println(xmlTag
.toString()) ;
258 oObj
.startElement(tagName
, attr
) ;
260 if ("end".equals(elem
[0])) {
261 log
.println("</" + elem
[1] + ">") ;
262 oObj
.endElement(elem
[1]) ;
264 if ("chars".equals(elem
[0])) {
265 log
.println("'" + elem
[1] + "'") ;
266 oObj
.characters(elem
[1]) ;
268 if ("spaces".equals(elem
[0])) {
269 log
.println("(spaces)'" + elem
[1] + "'") ;
270 oObj
.ignorableWhitespace(elem
[1]) ;
272 if ("instruct".equals(elem
[0])) {
273 log
.println("<?" + elem
[1] + " " + elem
[2] + "?>") ;
274 oObj
.processingInstruction(elem
[1], elem
[2]) ;
276 log
.println("!!! Bad object relation !!!") ;
277 throw new StatusException(Status
.failed("Bad relation")) ;
280 } catch (SAXException e
) {
281 e
.printStackTrace(log
) ;
282 log
.println("Wrapped exception :" + e
.WrappedException
) ;
286 tRes
.tested("startElement()", result
) ;
292 * Has <b>OK</b> status if no exceptions occurred during XML data
293 * transferring in <code>startDocument</code> and
294 * <code>startElement</code> method tests.
296 public void _endElement() {
298 tRes
.tested("endElement()", Status
.skipped(true));
301 requiredMethod("startElement()") ;
303 boolean result
= true ;
305 tRes
.tested("endElement()", result
) ;
311 * Has <b>OK</b> status if no exceptions occurred during XML data
312 * transferring in <code>startDocument</code> and
313 * <code>startElement</code> method tests.
315 public void _characters() {
317 tRes
.tested("characters()", Status
.skipped(true));
320 requiredMethod("startElement()") ;
322 boolean result
= true ;
324 tRes
.tested("characters()", result
) ;
330 * Has <b>OK</b> status if no exceptions occurred during XML data
331 * transferring in <code>startDocument</code> and
332 * <code>startElement</code> method tests.
334 public void _ignorableWhitespace() {
336 tRes
.tested("ignorableWhitespace()", Status
.skipped(true));
339 requiredMethod("startElement()") ;
341 boolean result
= true ;
343 tRes
.tested("ignorableWhitespace()", result
) ;
349 * Has <b>OK</b> status if no exceptions occurred during XML data
350 * transferring in <code>startDocument</code> and
351 * <code>startElement</code> method tests.
353 public void _processingInstruction() {
355 tRes
.tested("processingInstruction()", Status
.skipped(true));
358 requiredMethod("startElement()") ;
360 boolean result
= true ;
362 tRes
.tested("processingInstruction()", result
) ;
368 * Has <b>OK</b> status if no exceptions occurred during XML data
369 * transferring in <code>startDocument</code> and
370 * <code>startElement</code> method tests.
372 public void _setDocumentLocator() {
374 tRes
.tested("setDocumentLocator()", Status
.skipped(true));
377 executeMethod("endDocument()") ;
379 boolean result
= locatorResult
;
380 if (locatorException
!= null) {
381 log
.println("Exception occurred during setDocumentLocator() call:") ;
382 locatorException
.printStackTrace(log
) ;
383 log
.println("Wrapped exception :"
384 + locatorException
.WrappedException
) ;
388 tRes
.tested("setDocumentLocator()", result
) ;