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 compilant. <p>
74 * @see com.sun.star.xml.sax.XDocumentHandler
76 public class _XDocumentHandler
extends MultiMethodTest
{
78 private static class DocumentLocator
implements XLocator
{
79 private PrintWriter log
= null ;
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 static 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 static 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.
134 public void before() {
135 locator
= new DocumentLocator(log
) ;
136 if (tEnv
.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
137 log
.println("Settings can't be imported in the current Implementation");
140 xmlData
= (String
[][])tEnv
.getObjRelation("XDocumentHandler.XMLData") ;
141 checker
= (ImportChecker
)
142 tEnv
.getObjRelation("XDocumentHandler.ImportChecker") ;
144 TargetDocumentSetter targetDocSet
= (TargetDocumentSetter
)
145 tEnv
.getObjRelation("XDocumentHandler.TargetDocumentSetter");
147 if (xmlData
== null || checker
== null) throw new StatusException
148 (Status
.failed("Relation wasn't found")) ;
150 if (targetDocSet
!= null){
153 log
.println("object realtion 'XDocumentHandler.TargetDocumentSetter' not used.");
154 log
.println("be shure that the test have a target to write throu");
159 * Sets document locator to dummy locator implementation and
160 * calls the <code>startDocument</code> method. <p>
162 * Has <b> OK </b> status if no runtime exceptions occurred.
164 public void _startDocument() {
166 tRes
.tested("startDocument()", Status
.skipped(true));
171 oObj
.setDocumentLocator(locator
) ;
172 } catch (SAXException e
) {
173 locatorException
= e
;
174 locatorResult
= false ;
177 boolean result
= true ;
179 oObj
.startDocument() ;
180 } catch (SAXException e
) {
181 e
.printStackTrace(log
) ;
182 log
.println("Wrapped exception :" + e
.WrappedException
) ;
186 tRes
.tested("startDocument()", result
) ;
190 * This test is finally executed. It finishes XML data
191 * transfering with <code>endDocument</code> method call. <p>
193 * Has <b>OK</b> status if no exceptions occurred during
194 * the whole transfering and if the appropriate changes
195 * occurred in the document where XML data was trnsfered to.
196 * This check is performed by checker relation.
198 public void _endDocument() {
200 tRes
.tested("endDocument()", Status
.skipped(true));
203 requiredMethod("startElement()") ;
204 executeMethod("endElement()") ;
205 executeMethod("characters()") ;
206 executeMethod("ignorableWhitespace()") ;
207 executeMethod("processingInstruction()") ;
209 boolean result
= true ;
212 } catch (SAXException e
) {
213 e
.printStackTrace(log
) ;
214 log
.println("Wrapped exception :" + e
.WrappedException
) ;
218 log
.println("Check if import was successful ...") ;
219 result
&= checker
.checkImport() ;
221 tRes
.tested("endDocument()", result
) ;
225 * Transfers XML data obtained from relation
226 * <code>'XDocumentHandler.XMLData'</code>. <p>
228 * Has <b>OK</b> status if no exceptions occurred during XML data
229 * transfering in <code>startDocument</code> and
230 * <code>startElement</code> method tests. <p>
232 * Exact checking of XML transfer is made in <code>endDocument</code>
234 public void _startElement() {
236 tRes
.tested("startElement()", Status
.skipped(true));
239 boolean result
= true ;
242 log
.println("StartElement Processing XML data ...") ;
243 for(int i
= 0; i
< xmlData
.length
; i
++) {
244 String
[] elem
= xmlData
[i
] ;
246 if ("start".equals(elem
[0])) {
248 String tagName
= elem
[1] ;
250 XMLTools
.AttributeList attr
= new XMLTools
.AttributeList() ;
251 for (int j
= 2; j
< elem
.length
; j
+=3) {
252 attr
.add(elem
[j
], elem
[j
+1], elem
[j
+2]);
253 xmlTag
+= " " + elem
[j
] + "(" + elem
[j
+1] +
254 ")=\"" + elem
[j
+2] + "\"" ;
258 log
.println(xmlTag
) ;
259 oObj
.startElement(tagName
, attr
) ;
261 if ("end".equals(elem
[0])) {
262 log
.println("</" + elem
[1] + ">") ;
263 oObj
.endElement(elem
[1]) ;
265 if ("chars".equals(elem
[0])) {
266 log
.println("'" + elem
[1] + "'") ;
267 oObj
.characters(elem
[1]) ;
269 if ("spaces".equals(elem
[0])) {
270 log
.println("(spaces)'" + elem
[1] + "'") ;
271 oObj
.ignorableWhitespace(elem
[1]) ;
273 if ("instruct".equals(elem
[0])) {
274 log
.println("<?" + elem
[1] + " " + elem
[2] + "?>") ;
275 oObj
.processingInstruction(elem
[1], elem
[2]) ;
277 log
.println("!!! Bad object relation !!!") ;
278 throw new StatusException(Status
.failed("Bad relation")) ;
281 } catch (SAXException e
) {
282 e
.printStackTrace(log
) ;
283 log
.println("Wrapped exception :" + e
.WrappedException
) ;
287 tRes
.tested("startElement()", result
) ;
293 * Has <b>OK</b> status if no exceptions occurred during XML data
294 * transfering in <code>startDocument</code> and
295 * <code>startElement</code> method tests.
297 public void _endElement() {
299 tRes
.tested("endElement()", Status
.skipped(true));
302 requiredMethod("startElement()") ;
304 boolean result
= true ;
306 tRes
.tested("endElement()", result
) ;
312 * Has <b>OK</b> status if no exceptions occurred during XML data
313 * transfering in <code>startDocument</code> and
314 * <code>startElement</code> method tests.
316 public void _characters() {
318 tRes
.tested("characters()", Status
.skipped(true));
321 requiredMethod("startElement()") ;
323 boolean result
= true ;
325 tRes
.tested("characters()", result
) ;
331 * Has <b>OK</b> status if no exceptions occurred during XML data
332 * transfering in <code>startDocument</code> and
333 * <code>startElement</code> method tests.
335 public void _ignorableWhitespace() {
337 tRes
.tested("ignorableWhitespace()", Status
.skipped(true));
340 requiredMethod("startElement()") ;
342 boolean result
= true ;
344 tRes
.tested("ignorableWhitespace()", result
) ;
350 * Has <b>OK</b> status if no exceptions occurred during XML data
351 * transfering in <code>startDocument</code> and
352 * <code>startElement</code> method tests.
354 public void _processingInstruction() {
356 tRes
.tested("processingInstruction()", Status
.skipped(true));
359 requiredMethod("startElement()") ;
361 boolean result
= true ;
363 tRes
.tested("processingInstruction()", result
) ;
369 * Has <b>OK</b> status if no exceptions occurred during XML data
370 * transfering in <code>startDocument</code> and
371 * <code>startElement</code> method tests.
373 public void _setDocumentLocator() {
375 tRes
.tested("setDocumentLocator()", Status
.skipped(true));
378 executeMethod("endDocument()") ;
380 boolean result
= locatorResult
;
381 if (locatorException
!= null) {
382 log
.println("Exception occurred during setDocumentLocator() call:") ;
383 locatorException
.printStackTrace(log
) ;
384 log
.println("Wrapped exception :"
385 + locatorException
.WrappedException
) ;
389 tRes
.tested("setDocumentLocator()", result
) ;