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