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