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.XMLImporter</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 XMLImporter 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 occured.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.XMLImporter</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 = "XMLImporter_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("com.sun.star.comp.Chart.XMLImporter") ;
136 
137             Object oTitle = xChartDoc.getTitle() ;
138             xTitleProp = (XPropertySet) UnoRuntime.queryInterface
139                 (XPropertySet.class, oTitle) ;
140         } catch (com.sun.star.uno.Exception e) {
141             e.printStackTrace(log) ;
142             throw new StatusException("Can't create component.", e) ;
143         }
144 
145         oObj = (XInterface) oInt ;
146 
147         // create testobject here
148         log.println( "creating a new environment for Paragraph object" );
149         TestEnvironment tEnv = new TestEnvironment( oObj );
150 
151         // adding relation
152         tEnv.addObjRelation("TargetDocument", comp) ;
153 
154         // adding relation for XDocumentHandler
155         String[][] xml = new String[][] {
156             {"start", "office:document",
157                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
158                 "xmlns:text", "CDATA", "http://openoffice.org/2000/text",
159                 "xmlns:chart", "CDATA", "http://openoffice.org/2000/chart",
160                 "xmlns:table", "CDATA", "http://openoffice.org/2000/table",
161                 "xmlns:svg", "CDATA", "http://openoffice.org/2000/svg",
162                 "office:class", "CDATA", "chart"
163                 ,"office:version", "CDATA", "1.0"
164                 },
165             {"start", "office:body"},
166             {"start", "chart:chart"},
167             {"start", "chart:title"},
168             {"start", "text:p"},
169             {"chars", impValue},
170             {"end", "text:p"},
171             {"end", "chart:title"},
172             {"end", "chart:chart"},
173             {"end", "office:body"},
174             {"end", "office:document-content"}} ;
175 
176         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
177 
178         final PrintWriter logF = log ;
179 
180         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
181             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
182                 public boolean checkImport() {
183                     try {
184                         String title = (String)
185                             xTitleProp.getPropertyValue("String") ;
186                         logF.println("Title returned = '" + title + "'") ;
187                         return impValue.equals(title) ;
188                     } catch (com.sun.star.uno.Exception e) {
189                         logF.println
190                             ("Exception occurred while checking filter :") ;
191                         e.printStackTrace(logF) ;
192                         return false ;
193                     }
194                 }
195             }) ;
196 
197 
198         return tEnv;
199     } // finish method getTestEnvironment
200 }
201