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