1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package ifc.xml.sax;
import java.io.PrintWriter;
import lib.MultiMethodTest;
import lib.Status;
import lib.StatusException;
import util.XMLTools;
import com.sun.star.xml.sax.SAXException;
import com.sun.star.xml.sax.XDocumentHandler;
import com.sun.star.xml.sax.XLocator;
/**
* Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
* interface methods :
* <ul>
* <li><code> startDocument()</code></li>
* <li><code> endDocument()</code></li>
* <li><code> startElement()</code></li>
* <li><code> endElement()</code></li>
* <li><code> characters()</code></li>
* <li><code> ignorableWhitespace()</code></li>
* <li><code> processingInstruction()</code></li>
* <li><code> setDocumentLocator()</code></li>
* </ul> <p>
* This test needs the following object relations :
* <ul>
* <li> <code>'XDocumentHandler.XMLData'</code> (of type <code>String[][]
* </code>):the XML data which will be passed to the handler. Each
* array of strings corresponds to some handler event. The first
* string of event array is the type of the event they can have
* the following values :
* <ul>
* <li>'start' : startElement() event. The string with index 1
* is the name of element, the next array elements are attributes
* of XML element in order Name, Type, Value, Name, Type, Value, etc.
* </li>
* <li>'end' : endElement() event. The string with index 1
* is the name of element. </li>
* <li>'chars' : characters() event. The string with index 1
* is characters. </li>
* <li>'spaces' : ignorableWhitespace() event. The string with index 1
* is spaces. </li>
* <li>'instruct' : processingInstruction() event. The string with
* index 1 is the target of instruction. The string with index
* 2 is the data of instruction. </li>
* </ul> </li>
* <li> <code>'XDocumentHandler.ImportChecker'</code>
* (of type <code>ifc.xml.sax._XDocumentHandler.ImportChecker</code>) :
* this relation must be implementation of the interface above
* ant it must check if the XML data was successfully imported to
* the document. </li>
* </li>
* Test is <b> NOT </b> multithread compliant. <p>
* @see com.sun.star.xml.sax.XDocumentHandler
*/
public class _XDocumentHandler extends MultiMethodTest {
private static class DocumentLocator implements XLocator {
private final PrintWriter log;
public DocumentLocator(PrintWriter log) {
this.log = log ;
}
public int getColumnNumber() {
log.println("getColumnNumber() method called.") ;
return 10 ;
}
public int getLineNumber() {
log.println("getLineNumber() method called.") ;
return 9 ;
}
public String getPublicId() {
log.println("getPublicId() method called.") ;
return "file://d:/file.txt";
}
public String getSystemId() {
log.println("getSystemId() method called.") ;
return "system";
}
}
/**
* This interface implementation must be passed by component test
* for checking the whole import process.
*/
public interface ImportChecker {
/**
* Returns <code>true</code> if the XML data was successfully
* imported, <code>false</code> in other case.
*/
boolean checkImport() ;
}
/**
* This interface implementation must be passed by component test
* for setting a target document to the import process
*/
public interface TargetDocumentSetter {
void setTargetDocument();
}
public XDocumentHandler oObj = null;
private String[][] xmlData = null ;
private DocumentLocator locator = null ;
private ImportChecker checker = null ;
private boolean locatorResult = true ;
private SAXException locatorException = null ;
private boolean ToBeSkipped = false;
/**
* Retrieves object relations.
* @throws StatusException If one of relations not found.
*/
@Override
public void before() {
locator = new DocumentLocator(log) ;
if (tEnv.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
log.println("Settings can't be imported in the current Implementation");
ToBeSkipped = true;
}
xmlData = (String[][])tEnv.getObjRelation("XDocumentHandler.XMLData") ;
checker = (ImportChecker)
tEnv.getObjRelation("XDocumentHandler.ImportChecker") ;
TargetDocumentSetter targetDocSet = (TargetDocumentSetter)
tEnv.getObjRelation("XDocumentHandler.TargetDocumentSetter");
if (xmlData == null || checker == null) throw new StatusException
(Status.failed("Relation wasn't found")) ;
if (targetDocSet == null){
log.println("object relation 'XDocumentHandler.TargetDocumentSetter' not used.");
log.println("be sure that the test have a target to write through");
}
}
/**
* Sets document locator to dummy locator implementation and
* calls the <code>startDocument</code> method. <p>
*
* Has <b> OK </b> status if no runtime exceptions occurred.
*/
public void _startDocument() {
if (ToBeSkipped) {
tRes.tested("startDocument()", Status.skipped(true));
return;
}
try {
oObj.setDocumentLocator(locator) ;
} catch (SAXException e) {
locatorException = e ;
locatorResult = false ;
}
boolean result = true ;
try {
oObj.startDocument() ;
} catch (SAXException e) {
e.printStackTrace(log) ;
log.println("Wrapped exception :" + e.WrappedException) ;
result = false ;
}
tRes.tested("startDocument()", result) ;
}
/**
* This test is finally executed. It finishes XML data
* transferring with <code>endDocument</code> method call. <p>
*
* Has <b>OK</b> status if no exceptions occurred during
* the whole transferring and if the appropriate changes
* occurred in the document where XML data was transferred to.
* This check is performed by checker relation.
*/
public void _endDocument() {
if (ToBeSkipped) {
tRes.tested("endDocument()", Status.skipped(true));
return;
}
requiredMethod("startElement()") ;
executeMethod("endElement()") ;
executeMethod("characters()") ;
executeMethod("ignorableWhitespace()") ;
executeMethod("processingInstruction()") ;
boolean result = true ;
try {
oObj.endDocument() ;
} catch (SAXException e) {
e.printStackTrace(log) ;
log.println("Wrapped exception :" + e.WrappedException) ;
result = false ;
}
log.println("Check if import was successful ...") ;
result &= checker.checkImport() ;
tRes.tested("endDocument()", result) ;
}
/**
* Transfers XML data obtained from relation
* <code>'XDocumentHandler.XMLData'</code>. <p>
*
* Has <b>OK</b> status if no exceptions occurred during XML data
* transferring in <code>startDocument</code> and
* <code>startElement</code> method tests. <p>
*
* Exact checking of XML transfer is made in <code>endDocument</code>
*/
public void _startElement() {
if (ToBeSkipped) {
tRes.tested("startElement()", Status.skipped(true));
return;
}
boolean result = true ;
try {
log.println("StartElement Processing XML data ...") ;
for(int i = 0; i < xmlData.length; i++) {
String[] elem = xmlData[i] ;
if ("start".equals(elem[0])) {
StringBuilder xmlTag = new StringBuilder();
xmlTag.append("<");
String tagName = elem[1] ;
xmlTag.append(tagName);
XMLTools.AttributeList attr = new XMLTools.AttributeList() ;
for (int j = 2; j < elem.length; j+=3) {
attr.add(elem[j], elem[j+1], elem[j+2]);
xmlTag.append(" ").append(elem[j]).append("(").append(elem[j+1]).append(
")=\"").append(elem[j+2]).append("\"");
}
xmlTag.append(">");
log.println(xmlTag.toString()) ;
oObj.startElement(tagName, attr) ;
} else
if ("end".equals(elem[0])) {
log.println("</" + elem[1] + ">") ;
oObj.endElement(elem[1]) ;
} else
if ("chars".equals(elem[0])) {
log.println("'" + elem[1] + "'") ;
oObj.characters(elem[1]) ;
} else
if ("spaces".equals(elem[0])) {
log.println("(spaces)'" + elem[1] + "'") ;
oObj.ignorableWhitespace(elem[1]) ;
} else
if ("instruct".equals(elem[0])) {
log.println("<?" + elem[1] + " " + elem[2] + "?>") ;
oObj.processingInstruction(elem[1], elem[2]) ;
} else {
log.println("!!! Bad object relation !!!") ;
throw new StatusException(Status.failed("Bad relation")) ;
}
}
} catch (SAXException e) {
e.printStackTrace(log) ;
log.println("Wrapped exception :" + e.WrappedException) ;
result = false ;
}
tRes.tested("startElement()", result) ;
}
/**
* Does nothing. <p>
*
* Has <b>OK</b> status if no exceptions occurred during XML data
* transferring in <code>startDocument</code> and
* <code>startElement</code> method tests.
*/
public void _endElement() {
if (ToBeSkipped) {
tRes.tested("endElement()", Status.skipped(true));
return;
}
requiredMethod("startElement()") ;
boolean result = true ;
tRes.tested("endElement()", result) ;
}
/**
* Does nothing. <p>
*
* Has <b>OK</b> status if no exceptions occurred during XML data
* transferring in <code>startDocument</code> and
* <code>startElement</code> method tests.
*/
public void _characters() {
if (ToBeSkipped) {
tRes.tested("characters()", Status.skipped(true));
return;
}
requiredMethod("startElement()") ;
boolean result = true ;
tRes.tested("characters()", result) ;
}
/**
* Does nothing. <p>
*
* Has <b>OK</b> status if no exceptions occurred during XML data
* transferring in <code>startDocument</code> and
* <code>startElement</code> method tests.
*/
public void _ignorableWhitespace() {
if (ToBeSkipped) {
tRes.tested("ignorableWhitespace()", Status.skipped(true));
return;
}
requiredMethod("startElement()") ;
boolean result = true ;
tRes.tested("ignorableWhitespace()", result) ;
}
/**
* Does nothing. <p>
*
* Has <b>OK</b> status if no exceptions occurred during XML data
* transferring in <code>startDocument</code> and
* <code>startElement</code> method tests.
*/
public void _processingInstruction() {
if (ToBeSkipped) {
tRes.tested("processingInstruction()", Status.skipped(true));
return;
}
requiredMethod("startElement()") ;
boolean result = true ;
tRes.tested("processingInstruction()", result) ;
}
/**
* Does nothing. <p>
*
* Has <b>OK</b> status if no exceptions occurred during XML data
* transferring in <code>startDocument</code> and
* <code>startElement</code> method tests.
*/
public void _setDocumentLocator() {
if (ToBeSkipped) {
tRes.tested("setDocumentLocator()", Status.skipped(true));
return;
}
executeMethod("endDocument()") ;
boolean result = locatorResult ;
if (locatorException != null) {
log.println("Exception occurred during setDocumentLocator() call:") ;
locatorException.printStackTrace(log) ;
log.println("Wrapped exception :"
+ locatorException.WrappedException) ;
result = false ;
}
tRes.tested("setDocumentLocator()", result) ;
}
}
|