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