Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / xml / sax / _XDocumentHandler.java
blobbff921c278f3b48c0f05b0a6c34255f4047ad7ed
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 package ifc.xml.sax;
30 import java.io.PrintWriter;
32 import lib.MultiMethodTest;
33 import lib.Status;
34 import lib.StatusException;
35 import util.XMLTools;
37 import com.sun.star.xml.sax.SAXException;
38 import com.sun.star.xml.sax.XDocumentHandler;
39 import com.sun.star.xml.sax.XLocator;
41 /**
42 * Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
43 * interface methods :
44 * <ul>
45 * <li><code> startDocument()</code></li>
46 * <li><code> endDocument()</code></li>
47 * <li><code> startElement()</code></li>
48 * <li><code> endElement()</code></li>
49 * <li><code> characters()</code></li>
50 * <li><code> ignorableWhitespace()</code></li>
51 * <li><code> processingInstruction()</code></li>
52 * <li><code> setDocumentLocator()</code></li>
53 * </ul> <p>
54 * This test needs the following object relations :
55 * <ul>
56 * <li> <code>'XDocumentHandler.XMLData'</code> (of type <code>String[][]
57 * </code>):the XML data which will be passed to the handler. Each
58 * array of strings corresponds to some handler event. The fisrt
59 * string of event array is the type of the event they can have
60 * the following values :
61 * <ul>
62 * <li>'start' : startElement() event. The string with index 1
63 * is the name of element, the next array elements are attributes
64 * of XML element in order Name, Type, Value, Name, Type, Value, etc.
65 * </li>
66 * <li>'end' : endElement() event. The string with index 1
67 * is the name of element. </li>
68 * <li>'chars' : characters() event. The string with index 1
69 * is characters. </li>
70 * <li>'spaces' : ignorableWhitespace() event. The string with index 1
71 * is spaces. </li>
72 * <li>'instruct' : processingInstruction() event. The string with
73 * index 1 is the target of instruction. The string with index
74 * 2 is the data of instruction. </li>
75 * </ul> </li>
76 * <li> <code>'XDocumentHandler.ImportChecker'</code>
77 * (of type <code>ifc.xml.sax._XDocumentHandler.ImportChecker</code>) :
78 * this relation must be implementation of the interface above
79 * ant it must check if the XML data was successfully imported to
80 * the document. </li>
81 * </li>
82 * Test is <b> NOT </b> multithread compilant. <p>
83 * @see com.sun.star.xml.sax.XDocumentHandler
85 public class _XDocumentHandler extends MultiMethodTest {
87 private static class DocumentLocator implements XLocator {
88 public boolean aMethodCalled = false ;
90 private PrintWriter log = null ;
91 public DocumentLocator(PrintWriter log) {
92 this.log = log ;
94 public int getColumnNumber() {
95 log.println("getColumnNumber() method called.") ;
96 aMethodCalled = true ;
97 return 10 ;
99 public int getLineNumber() {
100 log.println("getLineNumber() method called.") ;
101 aMethodCalled = true ;
102 return 9 ;
104 public String getPublicId() {
105 log.println("getPublicId() method called.") ;
106 aMethodCalled = true ;
107 return "file://d:/file.txt";
109 public String getSystemId() {
110 log.println("getSystemId() method called.") ;
111 aMethodCalled = true ;
112 return "system";
117 * This interface implementation must be passed by component test
118 * for checking the whole import process.
120 public static interface ImportChecker {
122 * Returns <code>true</code> if the XML data was successfully
123 * imported, <code>false</code> in other case.
125 boolean checkImport() ;
129 * This interface implementation must be passed by component test
130 * for setting a target document to the import process
132 public static interface TargetDocumentSetter {
134 void setTargetDocument();
137 public XDocumentHandler oObj = null;
138 private String[][] xmlData = null ;
139 private DocumentLocator locator = null ;
140 private ImportChecker checker = null ;
141 private boolean locatorResult = true ;
142 private SAXException locatorException = null ;
143 private boolean ToBeSkipped = false;
146 * Retrieves object relations.
147 * @throws StatusException If one of relations not found.
149 public void before() {
150 locator = new DocumentLocator(log) ;
151 if (tEnv.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
152 log.println("Settings can't be imported in the current Implementation");
153 ToBeSkipped = true;
155 xmlData = (String[][])tEnv.getObjRelation("XDocumentHandler.XMLData") ;
156 checker = (ImportChecker)
157 tEnv.getObjRelation("XDocumentHandler.ImportChecker") ;
159 TargetDocumentSetter targetDocSet = (TargetDocumentSetter)
160 tEnv.getObjRelation("XDocumentHandler.TargetDocumentSetter");
162 if (xmlData == null || checker == null) throw new StatusException
163 (Status.failed("Relation wasn't found")) ;
165 if (targetDocSet != null){
167 }else{
168 log.println("object realtion 'XDocumentHandler.TargetDocumentSetter' not used.");
169 log.println("be shure that the test have a target to write throu");
174 * Sets document locator to dummy locator implementation and
175 * calls the <code>startDocument</code> method. <p>
177 * Has <b> OK </b> status if no runtime exceptions occurred.
179 public void _startDocument() {
180 if (ToBeSkipped) {
181 tRes.tested("startDocument()", Status.skipped(true));
182 return;
185 try {
186 oObj.setDocumentLocator(locator) ;
187 } catch (SAXException e) {
188 locatorException = e ;
189 locatorResult = false ;
192 boolean result = true ;
193 try {
194 oObj.startDocument() ;
195 } catch (SAXException e) {
196 e.printStackTrace(log) ;
197 log.println("Wrapped exception :" + e.WrappedException) ;
198 result = false ;
201 tRes.tested("startDocument()", result) ;
205 * This test is finally executed. It finishes XML data
206 * transfering with <code>endDocument</code> method call. <p>
208 * Has <b>OK</b> status if no exceptions occurred during
209 * the whole transfering and if the appropriate changes
210 * occurred in the document where XML data was trnsfered to.
211 * This check is performed by checker relation.
213 public void _endDocument() {
214 if (ToBeSkipped) {
215 tRes.tested("endDocument()", Status.skipped(true));
216 return;
218 requiredMethod("startElement()") ;
219 executeMethod("endElement()") ;
220 executeMethod("characters()") ;
221 executeMethod("ignorableWhitespace()") ;
222 executeMethod("processingInstruction()") ;
224 boolean result = true ;
225 try {
226 oObj.endDocument() ;
227 } catch (SAXException e) {
228 e.printStackTrace(log) ;
229 log.println("Wrapped exception :" + e.WrappedException) ;
230 result = false ;
233 log.println("Check if import was successful ...") ;
234 result &= checker.checkImport() ;
236 tRes.tested("endDocument()", result) ;
240 * Transfers XML data obtained from relation
241 * <code>'XDocumentHandler.XMLData'</code>. <p>
243 * Has <b>OK</b> status if no exceptions occurred during XML data
244 * transfering in <code>startDocument</code> and
245 * <code>startElement</code> method tests. <p>
247 * Exact checking of XML transfer is made in <code>endDocument</code>
249 public void _startElement() {
250 if (ToBeSkipped) {
251 tRes.tested("startElement()", Status.skipped(true));
252 return;
254 boolean result = true ;
256 try {
257 log.println("StartElement Processing XML data ...") ;
258 for(int i = 0; i < xmlData.length; i++) {
259 String[] elem = xmlData[i] ;
260 String xmlTag = "" ;
261 if ("start".equals(elem[0])) {
262 xmlTag += "<" ;
263 String tagName = elem[1] ;
264 xmlTag += tagName ;
265 XMLTools.AttributeList attr = new XMLTools.AttributeList() ;
266 for (int j = 2; j < elem.length; j+=3) {
267 attr.add(elem[j], elem[j+1], elem[j+2]);
268 xmlTag += " " + elem[j] + "(" + elem[j+1] +
269 ")=\"" + elem[j+2] + "\"" ;
271 xmlTag += ">" ;
273 log.println(xmlTag) ;
274 oObj.startElement(tagName, attr) ;
275 } else
276 if ("end".equals(elem[0])) {
277 log.println("</" + elem[1] + ">") ;
278 oObj.endElement(elem[1]) ;
279 } else
280 if ("chars".equals(elem[0])) {
281 log.println("'" + elem[1] + "'") ;
282 oObj.characters(elem[1]) ;
283 } else
284 if ("spaces".equals(elem[0])) {
285 log.println("(spaces)'" + elem[1] + "'") ;
286 oObj.ignorableWhitespace(elem[1]) ;
287 } else
288 if ("instruct".equals(elem[0])) {
289 log.println("<?" + elem[1] + " " + elem[2] + "?>") ;
290 oObj.processingInstruction(elem[1], elem[2]) ;
291 } else {
292 log.println("!!! Bad object relation !!!") ;
293 throw new StatusException(Status.failed("Bad relation")) ;
296 } catch (SAXException e) {
297 e.printStackTrace(log) ;
298 log.println("Wrapped exception :" + e.WrappedException) ;
299 result = false ;
302 tRes.tested("startElement()", result) ;
306 * Does nothing. <p>
308 * Has <b>OK</b> status if no exceptions occurred during XML data
309 * transfering in <code>startDocument</code> and
310 * <code>startElement</code> method tests.
312 public void _endElement() {
313 if (ToBeSkipped) {
314 tRes.tested("endElement()", Status.skipped(true));
315 return;
317 requiredMethod("startElement()") ;
319 boolean result = true ;
321 tRes.tested("endElement()", result) ;
325 * Does nothing. <p>
327 * Has <b>OK</b> status if no exceptions occurred during XML data
328 * transfering in <code>startDocument</code> and
329 * <code>startElement</code> method tests.
331 public void _characters() {
332 if (ToBeSkipped) {
333 tRes.tested("characters()", Status.skipped(true));
334 return;
336 requiredMethod("startElement()") ;
338 boolean result = true ;
340 tRes.tested("characters()", result) ;
344 * Does nothing. <p>
346 * Has <b>OK</b> status if no exceptions occurred during XML data
347 * transfering in <code>startDocument</code> and
348 * <code>startElement</code> method tests.
350 public void _ignorableWhitespace() {
351 if (ToBeSkipped) {
352 tRes.tested("ignorableWhitespace()", Status.skipped(true));
353 return;
355 requiredMethod("startElement()") ;
357 boolean result = true ;
359 tRes.tested("ignorableWhitespace()", result) ;
363 * Does nothing. <p>
365 * Has <b>OK</b> status if no exceptions occurred during XML data
366 * transfering in <code>startDocument</code> and
367 * <code>startElement</code> method tests.
369 public void _processingInstruction() {
370 if (ToBeSkipped) {
371 tRes.tested("processingInstruction()", Status.skipped(true));
372 return;
374 requiredMethod("startElement()") ;
376 boolean result = true ;
378 tRes.tested("processingInstruction()", result) ;
382 * Does nothing. <p>
384 * Has <b>OK</b> status if no exceptions occurred during XML data
385 * transfering in <code>startDocument</code> and
386 * <code>startElement</code> method tests.
388 public void _setDocumentLocator() {
389 if (ToBeSkipped) {
390 tRes.tested("setDocumentLocator()", Status.skipped(true));
391 return;
393 executeMethod("endDocument()") ;
395 boolean result = locatorResult ;
396 if (locatorException != null) {
397 log.println("Exception occurred during setDocumentLocator() call:") ;
398 locatorException.printStackTrace(log) ;
399 log.println("Wrapped exception :"
400 + locatorException.WrappedException) ;
401 result = false ;
404 tRes.tested("setDocumentLocator()", result) ;