Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / xml / sax / _XDocumentHandler.java
blobd73f6c3ab1555d5e688b2f1dc6f26b96d627e8a8
1 /*
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 .
19 package ifc.xml.sax;
21 import java.io.PrintWriter;
23 import lib.MultiMethodTest;
24 import lib.Status;
25 import lib.StatusException;
26 import util.XMLTools;
28 import com.sun.star.xml.sax.SAXException;
29 import com.sun.star.xml.sax.XDocumentHandler;
30 import com.sun.star.xml.sax.XLocator;
32 /**
33 * Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
34 * interface methods :
35 * <ul>
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>
44 * </ul> <p>
45 * This test needs the following object relations :
46 * <ul>
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 :
52 * <ul>
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.
56 * </li>
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
62 * is spaces. </li>
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>
66 * </ul> </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
71 * the document. </li>
72 * </li>
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) {
81 this.log = log ;
83 public int getColumnNumber() {
84 log.println("getColumnNumber() method called.") ;
85 return 10 ;
87 public int getLineNumber() {
88 log.println("getLineNumber() method called.") ;
89 return 9 ;
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.") ;
97 return "system";
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.
134 @Override
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");
139 ToBeSkipped = true;
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){
153 }else{
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() {
166 if (ToBeSkipped) {
167 tRes.tested("startDocument()", Status.skipped(true));
168 return;
171 try {
172 oObj.setDocumentLocator(locator) ;
173 } catch (SAXException e) {
174 locatorException = e ;
175 locatorResult = false ;
178 boolean result = true ;
179 try {
180 oObj.startDocument() ;
181 } catch (SAXException e) {
182 e.printStackTrace(log) ;
183 log.println("Wrapped exception :" + e.WrappedException) ;
184 result = false ;
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() {
200 if (ToBeSkipped) {
201 tRes.tested("endDocument()", Status.skipped(true));
202 return;
204 requiredMethod("startElement()") ;
205 executeMethod("endElement()") ;
206 executeMethod("characters()") ;
207 executeMethod("ignorableWhitespace()") ;
208 executeMethod("processingInstruction()") ;
210 boolean result = true ;
211 try {
212 oObj.endDocument() ;
213 } catch (SAXException e) {
214 e.printStackTrace(log) ;
215 log.println("Wrapped exception :" + e.WrappedException) ;
216 result = false ;
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() {
236 if (ToBeSkipped) {
237 tRes.tested("startElement()", Status.skipped(true));
238 return;
240 boolean result = true ;
242 try {
243 log.println("StartElement Processing XML data ...") ;
244 for(int i = 0; i < xmlData.length; i++) {
245 String[] elem = xmlData[i] ;
246 String xmlTag = "" ;
247 if ("start".equals(elem[0])) {
248 xmlTag += "<" ;
249 String tagName = elem[1] ;
250 xmlTag += tagName ;
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] + "\"" ;
257 xmlTag += ">" ;
259 log.println(xmlTag) ;
260 oObj.startElement(tagName, attr) ;
261 } else
262 if ("end".equals(elem[0])) {
263 log.println("</" + elem[1] + ">") ;
264 oObj.endElement(elem[1]) ;
265 } else
266 if ("chars".equals(elem[0])) {
267 log.println("'" + elem[1] + "'") ;
268 oObj.characters(elem[1]) ;
269 } else
270 if ("spaces".equals(elem[0])) {
271 log.println("(spaces)'" + elem[1] + "'") ;
272 oObj.ignorableWhitespace(elem[1]) ;
273 } else
274 if ("instruct".equals(elem[0])) {
275 log.println("<?" + elem[1] + " " + elem[2] + "?>") ;
276 oObj.processingInstruction(elem[1], elem[2]) ;
277 } else {
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) ;
285 result = false ;
288 tRes.tested("startElement()", result) ;
292 * Does nothing. <p>
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() {
299 if (ToBeSkipped) {
300 tRes.tested("endElement()", Status.skipped(true));
301 return;
303 requiredMethod("startElement()") ;
305 boolean result = true ;
307 tRes.tested("endElement()", result) ;
311 * Does nothing. <p>
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() {
318 if (ToBeSkipped) {
319 tRes.tested("characters()", Status.skipped(true));
320 return;
322 requiredMethod("startElement()") ;
324 boolean result = true ;
326 tRes.tested("characters()", result) ;
330 * Does nothing. <p>
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() {
337 if (ToBeSkipped) {
338 tRes.tested("ignorableWhitespace()", Status.skipped(true));
339 return;
341 requiredMethod("startElement()") ;
343 boolean result = true ;
345 tRes.tested("ignorableWhitespace()", result) ;
349 * Does nothing. <p>
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() {
356 if (ToBeSkipped) {
357 tRes.tested("processingInstruction()", Status.skipped(true));
358 return;
360 requiredMethod("startElement()") ;
362 boolean result = true ;
364 tRes.tested("processingInstruction()", result) ;
368 * Does nothing. <p>
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() {
375 if (ToBeSkipped) {
376 tRes.tested("setDocumentLocator()", Status.skipped(true));
377 return;
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) ;
387 result = false ;
390 tRes.tested("setDocumentLocator()", result) ;