merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / XMLExporter.java
blob4d1f273447d9786bedc20dc74eb513a2ec6373bc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLExporter.java,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod._sc;
33 import java.io.PrintWriter;
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 import util.SOfficeFactory;
40 import util.XMLTools;
42 import com.sun.star.container.XIndexAccess;
43 import com.sun.star.container.XNamed;
44 import com.sun.star.document.XExporter;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.sheet.XSpreadsheets;
50 import com.sun.star.uno.Any;
51 import com.sun.star.uno.AnyConverter;
52 import com.sun.star.uno.Type;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 import com.sun.star.xml.sax.XDocumentHandler;
57 /**
58 * Test for object which is represented by service
59 * <code>com.sun.star.comp.Calc.XMLExporter</code>. <p>
60 * Object implements the following interfaces :
61 * <ul>
62 * <li><code>com::sun::star::lang::XInitialization</code></li>
63 * <li><code>com::sun::star::document::ExportFilter</code></li>
64 * <li><code>com::sun::star::document::XFilter</code></li>
65 * <li><code>com::sun::star::document::XExporter</code></li>
66 * <li><code>com::sun::star::beans::XPropertySet</code></li>
67 * </ul>
68 * @see com.sun.star.lang.XInitialization
69 * @see com.sun.star.document.ExportFilter
70 * @see com.sun.star.document.XFilter
71 * @see com.sun.star.document.XExporter
72 * @see com.sun.star.beans.XPropertySet
73 * @see ifc.lang._XInitialization
74 * @see ifc.document._ExportFilter
75 * @see ifc.document._XFilter
76 * @see ifc.document._XExporter
77 * @see ifc.beans._XPropertySet
79 public class XMLExporter extends TestCase {
80 static XComponent xSheetDoc;
82 /**
83 * New spreadsheet document created.
85 protected void initialize( TestParameters tParam, PrintWriter log ) {
86 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
87 try {
88 log.println( "creating a calc document" );
89 xSheetDoc = SOF.openDoc("scalc","_blank");
90 } catch ( com.sun.star.uno.Exception e ) {
91 // Some exception occures.FAILED
92 e.printStackTrace( log );
93 throw new StatusException( "Couldn't create document", e );
97 /**
98 * Spreadsheet document disposed
100 protected void cleanup( TestParameters tParam, PrintWriter log ) {
101 log.println( " disposing xCalcDoc " );
102 util.DesktopTools.closeDoc(xSheetDoc);
106 * Creating a Testenvironment for the interfaces to be tested.
107 * Creates an instance of the service
108 * <code>com.sun.star.comp.Calc.XMLExporter</code> with
109 * argument which is an implementation of <code>XDocumentHandler</code>
110 * and which can check if required tags and character data is
111 * exported. <p>
112 * The calc document is set as a source document for exporter
113 * created. Checking whether tags for table corresponding to some sheet
114 * is exported.<p>
115 * Object relations created :
116 * <ul>
117 * <li> <code>'MediaDescriptor'</code> for
118 * {@link ifc.document._XFilter} interface </li>
119 * <li> <code>'XFilter.Checker'</code> for
120 * {@link ifc.document._XFilter} interface </li>
121 * <li> <code>'SourceDocument'</code> for
122 * {@link ifc.document._XExporter} interface </li>
123 * </ul>
125 protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
127 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
128 XInterface oObj = null;
129 FilterChecker Filter = new FilterChecker(log);
130 Any arg = new Any(new Type(XDocumentHandler.class), Filter);
131 final String SHEET_NAME = "XMLExporter_SheetTestName";
133 try {
134 oObj = (XInterface) xMSF.createInstanceWithArguments(
135 "com.sun.star.comp.Calc.XMLExporter", new Object[] {arg} );
136 XExporter xEx = (XExporter) UnoRuntime.queryInterface
137 (XExporter.class,oObj);
138 xEx.setSourceDocument(xSheetDoc);
140 //set name of sheet
141 XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
142 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
143 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
144 XIndexAccess xSheetsIndexArray = (XIndexAccess)
145 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
146 XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
147 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
148 XNamed xSheetNamed = (XNamed)
149 UnoRuntime.queryInterface(XNamed.class, xSheet);
150 xSheetNamed.setName(SHEET_NAME);
152 log.println("fill sheet with contnet...");
153 util.CalcTools.fillCalcSheetWithContent(xSheet, 3, 3, 50, 50);
155 } catch (com.sun.star.uno.Exception e) {
156 e.printStackTrace(log) ;
157 throw new StatusException("Can't create component.", e) ;
158 } catch (java.lang.Exception e) {
159 e.printStackTrace(log);
160 throw new StatusException("Can't create environment.", e);
163 // adding tags which must be contained in XML output
164 Filter.addTag( new XMLTools.Tag("office:document") );
165 Filter.addTagEnclosed(
166 new XMLTools.Tag("office:meta"),
167 new XMLTools.Tag("office:document") );
168 Filter.addTagEnclosed(
169 new XMLTools.Tag("office:settings"),
170 new XMLTools.Tag("office:document") );
171 Filter.addTagEnclosed(
172 new XMLTools.Tag("office:script"),
173 new XMLTools.Tag("office:document") );
174 Filter.addTagEnclosed(
175 new XMLTools.Tag("office:styles"),
176 new XMLTools.Tag("office:document") );
177 Filter.addTagEnclosed(
178 new XMLTools.Tag("office:body"),
179 new XMLTools.Tag("office:document") );
180 Filter.addTagEnclosed(
181 new XMLTools.Tag("table:table"),
182 new XMLTools.Tag("office:body") );
183 Filter.addTag(
184 new XMLTools.Tag("table:table", "table:name", SHEET_NAME) );
186 // create testobject here
187 log.println( "creating a new environment" );
188 TestEnvironment tEnv = new TestEnvironment( oObj );
190 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
191 new String[] {"FilterName"},
192 new Object[] {"scalc: StarOffice XML (Calc)"}));
193 tEnv.addObjRelation("SourceDocument",xSheetDoc);
194 tEnv.addObjRelation("XFilter.Checker", Filter) ;
195 return tEnv;
199 * This class checks the XML for tags and data required and returns
200 * checking result to <code>XFilter</code> interface test. All
201 * the information about errors occured in XML data is written
202 * to log specified.
203 * @see ifc.document._XFilter
205 protected class FilterChecker extends XMLTools.XMLChecker
206 implements ifc.document._XFilter.FilterChecker {
209 * Creates a class which will write information
210 * into log specified.
212 public FilterChecker(PrintWriter log) {
213 super(log, false) ;
216 * <code>_XFilter.FilterChecker</code> interface method
217 * which returns the result of XML checking.
218 * @return <code>true</code> if the XML data exported was
219 * valid (i.e. all necessary tags and character data exists),
220 * <code>false</code> if some errors occured.
222 public boolean checkFilter() {
223 return check();