merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / xml / sax / _XDocumentHandler.java
blob5ad09414c81e3a5fd96129fbfdda94de03c59f76
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDocumentHandler.java,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.xml.sax;
33 import java.io.PrintWriter;
35 import lib.MultiMethodTest;
36 import lib.Status;
37 import lib.StatusException;
38 import util.XMLTools;
40 import com.sun.star.xml.sax.SAXException;
41 import com.sun.star.xml.sax.XDocumentHandler;
42 import com.sun.star.xml.sax.XLocator;
44 /**
45 * Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
46 * interface methods :
47 * <ul>
48 * <li><code> startDocument()</code></li>
49 * <li><code> endDocument()</code></li>
50 * <li><code> startElement()</code></li>
51 * <li><code> endElement()</code></li>
52 * <li><code> characters()</code></li>
53 * <li><code> ignorableWhitespace()</code></li>
54 * <li><code> processingInstruction()</code></li>
55 * <li><code> setDocumentLocator()</code></li>
56 * </ul> <p>
57 * This test needs the following object relations :
58 * <ul>
59 * <li> <code>'XDocumentHandler.XMLData'</code> (of type <code>String[][]
60 * </code>):the XML data which will be passed to the handler. Each
61 * array of strings corresponds to some handler event. The fisrt
62 * string of event array is the type of the event they can have
63 * the following values :
64 * <ul>
65 * <li>'start' : startElement() event. The string with index 1
66 * is the name of element, the next array elements are attributes
67 * of XML element in order Name, Type, Value, Name, Type, Value, etc.
68 * </li>
69 * <li>'end' : endElement() event. The string with index 1
70 * is the name of element. </li>
71 * <li>'chars' : characters() event. The string with index 1
72 * is characters. </li>
73 * <li>'spaces' : ignorableWhitespace() event. The string with index 1
74 * is spaces. </li>
75 * <li>'instruct' : processingInstruction() event. The string with
76 * index 1 is the target of instruction. The string with index
77 * 2 is the data of instruction. </li>
78 * </ul> </li>
79 * <li> <code>'XDocumentHandler.ImportChecker'</code>
80 * (of type <code>ifc.xml.sax._XDocumentHandler.ImportChecker</code>) :
81 * this relation must be implementation of the interface above
82 * ant it must check if the XML data was successfully imported to
83 * the document. </li>
84 * </li>
85 * Test is <b> NOT </b> multithread compilant. <p>
86 * @see com.sun.star.xml.sax.XDocumentHandler
88 public class _XDocumentHandler extends MultiMethodTest {
90 private static class DocumentLocator implements XLocator {
91 public boolean aMethodCalled = false ;
93 private PrintWriter log = null ;
94 public DocumentLocator(PrintWriter log) {
95 this.log = log ;
97 public int getColumnNumber() {
98 log.println("getColumnNumber() method called.") ;
99 aMethodCalled = true ;
100 return 10 ;
102 public int getLineNumber() {
103 log.println("getLineNumber() method called.") ;
104 aMethodCalled = true ;
105 return 9 ;
107 public String getPublicId() {
108 log.println("getPublicId() method called.") ;
109 aMethodCalled = true ;
110 return "file://d:/file.txt";
112 public String getSystemId() {
113 log.println("getSystemId() method called.") ;
114 aMethodCalled = true ;
115 return "system";
120 * This interface implementation must be passed by component test
121 * for checking the whole import process.
123 public static interface ImportChecker {
125 * Returns <code>true</code> if the XML data was successfully
126 * imported, <code>false</code> in other case.
128 boolean checkImport() ;
132 * This interface implementation must be passed by component test
133 * for setting a target document to the import process
135 public static interface TargetDocumentSetter {
137 void setTargetDocument();
140 public XDocumentHandler oObj = null;
141 private String[][] xmlData = null ;
142 private DocumentLocator locator = null ;
143 private ImportChecker checker = null ;
144 private boolean locatorResult = true ;
145 private SAXException locatorException = null ;
146 private boolean ToBeSkipped = false;
149 * Retrieves object relations.
150 * @throws StatusException If one of relations not found.
152 public void before() {
153 locator = new DocumentLocator(log) ;
154 if (tEnv.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
155 log.println("Settings can't be imported in the current Implementation");
156 ToBeSkipped = true;
158 xmlData = (String[][])tEnv.getObjRelation("XDocumentHandler.XMLData") ;
159 checker = (ImportChecker)
160 tEnv.getObjRelation("XDocumentHandler.ImportChecker") ;
162 TargetDocumentSetter targetDocSet = (TargetDocumentSetter)
163 tEnv.getObjRelation("XDocumentHandler.TargetDocumentSetter");
165 if (xmlData == null || checker == null) throw new StatusException
166 (Status.failed("Relation wasn't found")) ;
168 if (targetDocSet != null){
170 }else{
171 log.println("object realtion 'XDocumentHandler.TargetDocumentSetter' not used.");
172 log.println("be shure that the test have a target to write throu");
177 * Sets document locator to dummy locator implementation and
178 * calls the <code>startDocument</code> method. <p>
180 * Has <b> OK </b> status if no runtime exceptions occured.
182 public void _startDocument() {
183 if (ToBeSkipped) {
184 tRes.tested("startDocument()", Status.skipped(true));
185 return;
188 try {
189 oObj.setDocumentLocator(locator) ;
190 } catch (SAXException e) {
191 locatorException = e ;
192 locatorResult = false ;
195 boolean result = true ;
196 try {
197 oObj.startDocument() ;
198 } catch (SAXException e) {
199 e.printStackTrace(log) ;
200 log.println("Wrapped exception :" + e.WrappedException) ;
201 result = false ;
204 tRes.tested("startDocument()", result) ;
208 * This test is finally executed. It finishes XML data
209 * transfering with <code>endDocument</code> method call. <p>
211 * Has <b>OK</b> status if no exceptions occured during
212 * the whole transfering and if the appropriate changes
213 * occured in the document where XML data was trnsfered to.
214 * This check is performed by checker relation.
216 public void _endDocument() {
217 if (ToBeSkipped) {
218 tRes.tested("endDocument()", Status.skipped(true));
219 return;
221 requiredMethod("startElement()") ;
222 executeMethod("endElement()") ;
223 executeMethod("characters()") ;
224 executeMethod("ignorableWhitespace()") ;
225 executeMethod("processingInstruction()") ;
227 boolean result = true ;
228 try {
229 oObj.endDocument() ;
230 } catch (SAXException e) {
231 e.printStackTrace(log) ;
232 log.println("Wrapped exception :" + e.WrappedException) ;
233 result = false ;
236 log.println("Check if import was successful ...") ;
237 result &= checker.checkImport() ;
239 tRes.tested("endDocument()", result) ;
243 * Transfers XML data obtained from relation
244 * <code>'XDocumentHandler.XMLData'</code>. <p>
246 * Has <b>OK</b> status if no exceptions occured during XML data
247 * transfering in <code>startDocument</code> and
248 * <code>startElement</code> method tests. <p>
250 * Exact checking of XML transfer is made in <code>endDocument</code>
252 public void _startElement() {
253 if (ToBeSkipped) {
254 tRes.tested("startElement()", Status.skipped(true));
255 return;
257 boolean result = true ;
259 try {
260 log.println("StartElement Processing XML data ...") ;
261 for(int i = 0; i < xmlData.length; i++) {
262 String[] elem = xmlData[i] ;
263 String xmlTag = "" ;
264 if ("start".equals(elem[0])) {
265 xmlTag += "<" ;
266 String tagName = elem[1] ;
267 xmlTag += tagName ;
268 XMLTools.AttributeList attr = new XMLTools.AttributeList() ;
269 for (int j = 2; j < elem.length; j+=3) {
270 attr.add(elem[j], elem[j+1], elem[j+2]);
271 xmlTag += " " + elem[j] + "(" + elem[j+1] +
272 ")=\"" + elem[j+2] + "\"" ;
274 xmlTag += ">" ;
276 log.println(xmlTag) ;
277 oObj.startElement(tagName, attr) ;
278 } else
279 if ("end".equals(elem[0])) {
280 log.println("</" + elem[1] + ">") ;
281 oObj.endElement(elem[1]) ;
282 } else
283 if ("chars".equals(elem[0])) {
284 log.println("'" + elem[1] + "'") ;
285 oObj.characters(elem[1]) ;
286 } else
287 if ("spaces".equals(elem[0])) {
288 log.println("(spaces)'" + elem[1] + "'") ;
289 oObj.ignorableWhitespace(elem[1]) ;
290 } else
291 if ("instruct".equals(elem[0])) {
292 log.println("<?" + elem[1] + " " + elem[2] + "?>") ;
293 oObj.processingInstruction(elem[1], elem[2]) ;
294 } else {
295 log.println("!!! Bad object relation !!!") ;
296 throw new StatusException(Status.failed("Bad relation")) ;
299 } catch (SAXException e) {
300 e.printStackTrace(log) ;
301 log.println("Wrapped exception :" + e.WrappedException) ;
302 result = false ;
305 tRes.tested("startElement()", result) ;
309 * Does nothing. <p>
311 * Has <b>OK</b> status if no exceptions occured during XML data
312 * transfering in <code>startDocument</code> and
313 * <code>startElement</code> method tests.
315 public void _endElement() {
316 if (ToBeSkipped) {
317 tRes.tested("endElement()", Status.skipped(true));
318 return;
320 requiredMethod("startElement()") ;
322 boolean result = true ;
324 tRes.tested("endElement()", result) ;
328 * Does nothing. <p>
330 * Has <b>OK</b> status if no exceptions occured during XML data
331 * transfering in <code>startDocument</code> and
332 * <code>startElement</code> method tests.
334 public void _characters() {
335 if (ToBeSkipped) {
336 tRes.tested("characters()", Status.skipped(true));
337 return;
339 requiredMethod("startElement()") ;
341 boolean result = true ;
343 tRes.tested("characters()", result) ;
347 * Does nothing. <p>
349 * Has <b>OK</b> status if no exceptions occured during XML data
350 * transfering in <code>startDocument</code> and
351 * <code>startElement</code> method tests.
353 public void _ignorableWhitespace() {
354 if (ToBeSkipped) {
355 tRes.tested("ignorableWhitespace()", Status.skipped(true));
356 return;
358 requiredMethod("startElement()") ;
360 boolean result = true ;
362 tRes.tested("ignorableWhitespace()", result) ;
366 * Does nothing. <p>
368 * Has <b>OK</b> status if no exceptions occured during XML data
369 * transfering in <code>startDocument</code> and
370 * <code>startElement</code> method tests.
372 public void _processingInstruction() {
373 if (ToBeSkipped) {
374 tRes.tested("processingInstruction()", Status.skipped(true));
375 return;
377 requiredMethod("startElement()") ;
379 boolean result = true ;
381 tRes.tested("processingInstruction()", result) ;
385 * Does nothing. <p>
387 * Has <b>OK</b> status if no exceptions occured during XML data
388 * transfering in <code>startDocument</code> and
389 * <code>startElement</code> method tests.
391 public void _setDocumentLocator() {
392 if (ToBeSkipped) {
393 tRes.tested("setDocumentLocator()", Status.skipped(true));
394 return;
396 executeMethod("endDocument()") ;
398 boolean result = locatorResult ;
399 if (locatorException != null) {
400 log.println("Exception occured during setDocumentLocator() call:") ;
401 locatorException.printStackTrace(log) ;
402 log.println("Wrapped exception :"
403 + locatorException.WrappedException) ;
404 result = false ;
407 tRes.tested("setDocumentLocator()", result) ;