1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package mod._sw;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.document.XImporter;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 /**
45  * Test for object which is represented by service
46  * <code>com.sun.star.comp.Writer.XMLImporter</code>. <p>
47  * Object implements the following interfaces :
48  * <ul>
49  *  <li><code>com::sun::star::lang::XInitialization</code></li>
50  *  <li><code>com::sun::star::document::XImporter</code></li>
51  *  <li><code>com::sun::star::document::XFilter</code></li>
52  *  <li><code>com::sun::star::document::ImportFilter</code></li>
53  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
54  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
55 
56  * </ul>
57  * @see com.sun.star.lang.XInitialization
58  * @see com.sun.star.document.XImporter
59  * @see com.sun.star.document.XFilter
60  * @see com.sun.star.document.ImportFilter
61  * @see com.sun.star.beans.XPropertySet
62  * @see com.sun.star.xml.sax.XDocumentHandler
63  * @see ifc.lang._XInitialization
64  * @see ifc.document._XImporter
65  * @see ifc.document._XFilter
66  * @see ifc.document._XExporter
67  * @see ifc.beans._XPropertySet
68  * @see ifc.xml.sax._XDocumentHandler
69  */
70 public class XMLImporter extends TestCase {
71     XTextDocument xTextDoc;
72 
73     /**
74     * New text document created.
75     */
76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78 
79         try {
80             log.println( "creating a textdocument" );
81             xTextDoc = SOF.createTextDoc( null );
82         } catch ( com.sun.star.uno.Exception e ) {
83             // Some exception occures.FAILED
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document", e );
86         }
87     }
88 
89     /**
90     * Text document destroyed.
91     */
92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         log.println( "    disposing xTextDoc " );
94         util.DesktopTools.closeDoc(xTextDoc);
95     }
96 
97     /**
98     * Creating a Testenvironment for the interfaces to be tested.
99     * Creates an instance of the service
100     * <code>com.sun.star.comp.Writer.XMLImporter</code><p>
101     *
102     * The text document is set as a target document for importer.
103     * Imported tags contain tag including tag with test text.
104     * After import text getting from target document is checked.
105     *     Object relations created :
106     * <ul>
107     *  <li> <code>'XDocumentHandler.XMLData'</code> for
108     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
109     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
110     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
111     *  <li> <code>'TargetDocument'</code> for
112     *      {@link ifc.document._XImporter} interface </li>
113     * </ul>
114     */
115     public synchronized TestEnvironment createTestEnvironment
116             ( TestParameters tParam, PrintWriter log) {
117 
118         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
119         XInterface oObj = null;
120 
121         try {
122             oObj = (XInterface) xMSF.createInstance
123                 ("com.sun.star.comp.Writer.XMLImporter");
124             XImporter xIm = (XImporter) UnoRuntime.queryInterface
125                 (XImporter.class,oObj);
126             xIm.setTargetDocument(xTextDoc);
127         } catch (com.sun.star.uno.Exception e) {
128             e.printStackTrace(log) ;
129             throw new StatusException("Can't create component.", e) ;
130         }
131 
132         // create testobject here
133         log.println( "creating a new environment" );
134         TestEnvironment tEnv = new TestEnvironment( oObj );
135 
136         final String impText = "XMLImporter test." ;
137         String[][] xml = new String[][] {
138             {"start", "office:document" ,
139                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
140                 "office:class", "CDATA", "text",
141                 "xmlns:text", "CDATA", "http://openoffice.org/2000/text"},
142             {"start", "office:body" },
143             {"start", "text:p" },
144             {"chars", impText},
145             {"end", "text:p"},
146             {"end", "office:body"},
147             {"end", "office:document"}} ;
148 
149         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
150 
151         tEnv.addObjRelation("TargetDocument",xTextDoc);
152         log.println("Implementation Name: "+util.utils.getImplName(oObj));
153 
154         final XTextDocument textDoc = (XTextDocument) UnoRuntime.queryInterface
155             (XTextDocument.class, xTextDoc) ;
156         final PrintWriter fLog = log ;
157         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
158             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
159                 public boolean checkImport() {
160                     String docText = textDoc.getText().getString() ;
161                     fLog.println("Document text returned = '" + docText + "'") ;
162                     return impText.equals(docText) ;
163                 }
164             }) ;
165 
166         return tEnv;
167 
168     }
169 
170 }
171 
172