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