bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / xml / sax / _XDocumentHandler.java
blob649f5dfa372d2290662257ba0a7fb7d195b45e85
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 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) {
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 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");
138 ToBeSkipped = true;
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){
152 }else{
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() {
165 if (ToBeSkipped) {
166 tRes.tested("startDocument()", Status.skipped(true));
167 return;
170 try {
171 oObj.setDocumentLocator(locator) ;
172 } catch (SAXException e) {
173 locatorException = e ;
174 locatorResult = false ;
177 boolean result = true ;
178 try {
179 oObj.startDocument() ;
180 } catch (SAXException e) {
181 e.printStackTrace(log) ;
182 log.println("Wrapped exception :" + e.WrappedException) ;
183 result = false ;
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() {
199 if (ToBeSkipped) {
200 tRes.tested("endDocument()", Status.skipped(true));
201 return;
203 requiredMethod("startElement()") ;
204 executeMethod("endElement()") ;
205 executeMethod("characters()") ;
206 executeMethod("ignorableWhitespace()") ;
207 executeMethod("processingInstruction()") ;
209 boolean result = true ;
210 try {
211 oObj.endDocument() ;
212 } catch (SAXException e) {
213 e.printStackTrace(log) ;
214 log.println("Wrapped exception :" + e.WrappedException) ;
215 result = false ;
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() {
235 if (ToBeSkipped) {
236 tRes.tested("startElement()", Status.skipped(true));
237 return;
239 boolean result = true ;
241 try {
242 log.println("StartElement Processing XML data ...") ;
243 for(int i = 0; i < xmlData.length; i++) {
244 String[] elem = xmlData[i] ;
245 String xmlTag = "" ;
246 if ("start".equals(elem[0])) {
247 xmlTag += "<" ;
248 String tagName = elem[1] ;
249 xmlTag += tagName ;
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] + "\"" ;
256 xmlTag += ">" ;
258 log.println(xmlTag) ;
259 oObj.startElement(tagName, attr) ;
260 } else
261 if ("end".equals(elem[0])) {
262 log.println("</" + elem[1] + ">") ;
263 oObj.endElement(elem[1]) ;
264 } else
265 if ("chars".equals(elem[0])) {
266 log.println("'" + elem[1] + "'") ;
267 oObj.characters(elem[1]) ;
268 } else
269 if ("spaces".equals(elem[0])) {
270 log.println("(spaces)'" + elem[1] + "'") ;
271 oObj.ignorableWhitespace(elem[1]) ;
272 } else
273 if ("instruct".equals(elem[0])) {
274 log.println("<?" + elem[1] + " " + elem[2] + "?>") ;
275 oObj.processingInstruction(elem[1], elem[2]) ;
276 } else {
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) ;
284 result = false ;
287 tRes.tested("startElement()", result) ;
291 * Does nothing. <p>
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() {
298 if (ToBeSkipped) {
299 tRes.tested("endElement()", Status.skipped(true));
300 return;
302 requiredMethod("startElement()") ;
304 boolean result = true ;
306 tRes.tested("endElement()", result) ;
310 * Does nothing. <p>
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() {
317 if (ToBeSkipped) {
318 tRes.tested("characters()", Status.skipped(true));
319 return;
321 requiredMethod("startElement()") ;
323 boolean result = true ;
325 tRes.tested("characters()", result) ;
329 * Does nothing. <p>
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() {
336 if (ToBeSkipped) {
337 tRes.tested("ignorableWhitespace()", Status.skipped(true));
338 return;
340 requiredMethod("startElement()") ;
342 boolean result = true ;
344 tRes.tested("ignorableWhitespace()", result) ;
348 * Does nothing. <p>
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() {
355 if (ToBeSkipped) {
356 tRes.tested("processingInstruction()", Status.skipped(true));
357 return;
359 requiredMethod("startElement()") ;
361 boolean result = true ;
363 tRes.tested("processingInstruction()", result) ;
367 * Does nothing. <p>
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() {
374 if (ToBeSkipped) {
375 tRes.tested("setDocumentLocator()", Status.skipped(true));
376 return;
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) ;
386 result = false ;
389 tRes.tested("setDocumentLocator()", result) ;