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._sc;
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 import util.XMLTools;
34 
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.container.XNamed;
37 import com.sun.star.document.XExporter;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.sheet.XSpreadsheet;
41 import com.sun.star.sheet.XSpreadsheetDocument;
42 import com.sun.star.sheet.XSpreadsheets;
43 import com.sun.star.uno.Any;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 import com.sun.star.xml.sax.XDocumentHandler;
49 
50 /**
51  * Test for object which is represented by service
52  * <code>com.sun.star.comp.Calc.XMLExporter</code>. <p>
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li><code>com::sun::star::lang::XInitialization</code></li>
56  *  <li><code>com::sun::star::document::ExportFilter</code></li>
57  *  <li><code>com::sun::star::document::XFilter</code></li>
58  *  <li><code>com::sun::star::document::XExporter</code></li>
59  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
60  * </ul>
61  * @see com.sun.star.lang.XInitialization
62  * @see com.sun.star.document.ExportFilter
63  * @see com.sun.star.document.XFilter
64  * @see com.sun.star.document.XExporter
65  * @see com.sun.star.beans.XPropertySet
66  * @see ifc.lang._XInitialization
67  * @see ifc.document._ExportFilter
68  * @see ifc.document._XFilter
69  * @see ifc.document._XExporter
70  * @see ifc.beans._XPropertySet
71  */
72 public class XMLExporter extends TestCase {
73     static XComponent xSheetDoc;
74 
75     /**
76      * New spreadsheet document created.
77      */
initialize( TestParameters tParam, PrintWriter log )78     protected void initialize( TestParameters tParam, PrintWriter log ) {
79         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
80         try {
81             log.println( "creating a calc document" );
82             xSheetDoc = SOF.openDoc("scalc","_blank");
83         } catch ( com.sun.star.uno.Exception e ) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91      * Spreadsheet document disposed
92      */
cleanup( TestParameters tParam, PrintWriter log )93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xCalcDoc " );
95         util.DesktopTools.closeDoc(xSheetDoc);
96     }
97 
98     /**
99     * Creating a Testenvironment for the interfaces to be tested.
100     * Creates an instance of the service
101     * <code>com.sun.star.comp.Calc.XMLExporter</code> with
102     * argument which is an implementation of <code>XDocumentHandler</code>
103     * and which can check if required tags and character data is
104     * exported. <p>
105     * The calc document is set as a source document for exporter
106     * created. Checking whether tags for table corresponding to some sheet
107     * is exported.<p>
108     *     Object relations created :
109     * <ul>
110     *  <li> <code>'MediaDescriptor'</code> for
111     *      {@link ifc.document._XFilter} interface </li>
112     *  <li> <code>'XFilter.Checker'</code> for
113     *      {@link ifc.document._XFilter} interface </li>
114     *  <li> <code>'SourceDocument'</code> for
115     *      {@link ifc.document._XExporter} interface </li>
116     * </ul>
117     */
createTestEnvironment(TestParameters tParam, PrintWriter log)118     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
119 
120         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
121         XInterface oObj = null;
122         FilterChecker Filter = new FilterChecker(log);
123         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
124         final String SHEET_NAME = "XMLExporter_SheetTestName";
125 
126         try {
127             oObj = (XInterface) xMSF.createInstanceWithArguments(
128                 "com.sun.star.comp.Calc.XMLExporter", new Object[] {arg} );
129             XExporter xEx = (XExporter) UnoRuntime.queryInterface
130                 (XExporter.class,oObj);
131             xEx.setSourceDocument(xSheetDoc);
132 
133             //set name of sheet
134             XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
135                 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
136             XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
137             XIndexAccess xSheetsIndexArray = (XIndexAccess)
138                 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
139             XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
140                     new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
141             XNamed xSheetNamed = (XNamed)
142                 UnoRuntime.queryInterface(XNamed.class, xSheet);
143             xSheetNamed.setName(SHEET_NAME);
144 
145             log.println("fill sheet with contnet...");
146             util.CalcTools.fillCalcSheetWithContent(xSheet, 3, 3, 50, 50);
147 
148         } catch (com.sun.star.uno.Exception e) {
149             e.printStackTrace(log) ;
150             throw new StatusException("Can't create component.", e) ;
151         } catch (java.lang.Exception e) {
152             e.printStackTrace(log);
153             throw new StatusException("Can't create environment.", e);
154         }
155 
156         // adding tags which must be contained in XML output
157         Filter.addTag( new XMLTools.Tag("office:document") );
158         Filter.addTagEnclosed(
159             new XMLTools.Tag("office:meta"),
160             new XMLTools.Tag("office:document") );
161         Filter.addTagEnclosed(
162             new XMLTools.Tag("office:settings"),
163             new XMLTools.Tag("office:document") );
164         Filter.addTagEnclosed(
165             new XMLTools.Tag("office:script"),
166             new XMLTools.Tag("office:document") );
167         Filter.addTagEnclosed(
168             new XMLTools.Tag("office:styles"),
169             new XMLTools.Tag("office:document") );
170         Filter.addTagEnclosed(
171             new XMLTools.Tag("office:body"),
172             new XMLTools.Tag("office:document") );
173         Filter.addTagEnclosed(
174             new XMLTools.Tag("table:table"),
175             new XMLTools.Tag("office:body") );
176         Filter.addTag(
177             new XMLTools.Tag("table:table", "table:name", SHEET_NAME) );
178 
179         // create testobject here
180         log.println( "creating a new environment" );
181         TestEnvironment tEnv = new TestEnvironment( oObj );
182 
183         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
184             new String[] {"FilterName"},
185             new Object[] {"scalc: StarOffice XML (Calc)"}));
186         tEnv.addObjRelation("SourceDocument",xSheetDoc);
187         tEnv.addObjRelation("XFilter.Checker", Filter) ;
188         return tEnv;
189     }
190 
191     /**
192      * This class checks the XML for tags and data required and returns
193      * checking result to <code>XFilter</code> interface test. All
194      * the information about errors occured in XML data is written
195      * to log specified.
196      * @see ifc.document._XFilter
197      */
198     protected class FilterChecker extends XMLTools.XMLChecker
199         implements ifc.document._XFilter.FilterChecker {
200 
201         /**
202          * Creates a class which will write information
203          * into log specified.
204          */
FilterChecker(PrintWriter log)205         public FilterChecker(PrintWriter log) {
206             super(log, false) ;
207         }
208         /**
209          * <code>_XFilter.FilterChecker</code> interface method
210          * which returns the result of XML checking.
211          * @return <code>true</code> if the XML data exported was
212          * valid (i.e. all necessary tags and character data exists),
213          * <code>false</code> if some errors occured.
214          */
checkFilter()215         public boolean checkFilter() {
216             return check();
217         }
218     }
219 }
220 
221