xref: /AOO41X/main/qadevOOo/tests/java/mod/_xmloff/Chart/XMLContentImporter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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._xmloff.Chart;
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.beans.XPropertySet;
39 import com.sun.star.chart.XChartDocument;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 
45 /**
46  * Test for object which is represented by service
47  * <code>com.sun.star.comp.Chart.XMLContentImporter</code>. <p>
48  * Object implements the following interfaces :
49  * <ul>
50  *  <li><code>com::sun::star::lang::XInitialization</code></li>
51  *  <li><code>com::sun::star::document::XImporter</code></li>
52  *  <li><code>com::sun::star::document::XFilter</code></li>
53  *  <li><code>com::sun::star::document::ImportFilter</code></li>
54  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
55  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
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 XMLContentImporter extends TestCase {
71     XComponent comp ;
72     XChartDocument xChartDoc = null;
73 
74     /**
75     * New chart document created.
76     */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         // get a soffice factory object
79         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
80 
81         try {
82             log.println( "creating a chartdocument" );
83             xChartDoc = SOF.createChartDoc(null);
84         } catch ( Exception e ) {
85             // Some exception occures.FAILED
86             e.printStackTrace( log );
87             throw new StatusException( "Couldn't create document", e );
88         }
89 
90         comp = xChartDoc;
91     }
92 
93     /**
94      * Close document
95      */
96     protected void cleanup( TestParameters tParam, PrintWriter log ) {
97         if( xChartDoc!=null ) {
98             log.println( "    closing xChartDoc" );
99             util.DesktopTools.closeDoc(xChartDoc);
100             xChartDoc = null;
101             comp = null;
102         }
103     }
104 
105     /**
106     * Creating a Testenvironment for the interfaces to be tested.
107     * Creates an instance of the service
108     * <code>com.sun.star.comp.Chart.XMLContentImporter</code><p>
109     *
110     * The chart document is set as a target document for importer.
111     * Imported XML-data contains the tag which specifies the title
112     * of a chart.
113     * After import title name getting from target document is checked.
114     *     Object relations created :
115     * <ul>
116     *  <li> <code>'XDocumentHandler.XMLData'</code> for
117     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
118     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
119     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
120     *  <li> <code>'TargetDocument'</code> for
121     *      {@link ifc.document._XImporter} interface </li>
122     * </ul>
123     */
124     public synchronized TestEnvironment createTestEnvironment
125             (TestParameters tParam, PrintWriter log ) {
126 
127         XInterface oObj = null;
128         Object oInt = null ;
129         final String impValue = "XMLContentImporter_test" ;
130 
131         // creation of testobject here
132         // first we write what we are intend to do to log file
133         log.println( "creating a test environment" );
134 
135         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
136 
137         final XPropertySet xTitleProp ;
138         try {
139             oInt = xMSF.createInstance
140                 ("com.sun.star.comp.Chart.XMLContentImporter") ;
141 
142 
143             Object oTitle = xChartDoc.getTitle() ;
144             xTitleProp = (XPropertySet) UnoRuntime.queryInterface
145                 (XPropertySet.class, oTitle) ;
146         } catch (com.sun.star.uno.Exception e) {
147             e.printStackTrace(log) ;
148             throw new StatusException("Can't create component.", e) ;
149         }
150 
151         oObj = (XInterface) oInt ;
152 
153         // create testobject here
154         log.println( "creating a new environment for Paragraph object" );
155         TestEnvironment tEnv = new TestEnvironment( oObj );
156 
157         // adding relation
158         tEnv.addObjRelation("TargetDocument", comp) ;
159 
160         // adding relation for XDocumentHandler
161         String[][] xml = new String[][] {
162             {"start", "office:document-content",
163                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
164                 "xmlns:text", "CDATA", "http://openoffice.org/2000/text",
165                 "xmlns:chart", "CDATA", "http://openoffice.org/2000/chart",
166                 "xmlns:table", "CDATA", "http://openoffice.org/2000/table",
167                 "xmlns:svg", "CDATA", "http://openoffice.org/2000/svg",
168                 "office:class", "CDATA", "chart"
169                 ,"office:version", "CDATA", "1.0"
170                 },
171             {"start", "office:body"},
172             {"start", "chart:chart"},
173             {"start", "chart:title"},
174             {"start", "text:p"},
175             {"chars", impValue},
176             {"end", "text:p"},
177             {"end", "chart:title"},
178             {"end", "chart:chart"},
179             {"end", "office:body"},
180             {"end", "office:document-content"}} ;
181 
182         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
183 
184         final PrintWriter logF = log ;
185 
186         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
187             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
188                 public boolean checkImport() {
189                     try {
190                         String title = (String)
191                             xTitleProp.getPropertyValue("String") ;
192                         logF.println("Title returned = '" + title + "'") ;
193                         return impValue.equals(title) ;
194                     } catch (com.sun.star.uno.Exception e) {
195                         logF.println
196                             ("Exception occured while checking filter :") ;
197                         e.printStackTrace(logF) ;
198                         return false ;
199                     }
200                 }
201             }) ;
202 
203 
204         return tEnv;
205     } // finish method getTestEnvironment
206 }
207