bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / XMLExporter.java
blobcfc85e4e0719e47705678936a6aa8cdb71fdd247
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
28 import util.XMLTools;
30 import com.sun.star.container.XIndexAccess;
31 import com.sun.star.container.XNamed;
32 import com.sun.star.document.XExporter;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.sheet.XSpreadsheet;
36 import com.sun.star.sheet.XSpreadsheetDocument;
37 import com.sun.star.sheet.XSpreadsheets;
38 import com.sun.star.uno.Any;
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.Type;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 import com.sun.star.xml.sax.XDocumentHandler;
45 /**
46 * Test for object which is represented by service
47 * <code>com.sun.star.comp.Calc.XMLExporter</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::ExportFilter</code></li>
52 * <li><code>com::sun::star::document::XFilter</code></li>
53 * <li><code>com::sun::star::document::XExporter</code></li>
54 * <li><code>com::sun::star::beans::XPropertySet</code></li>
55 * </ul>
56 * @see com.sun.star.lang.XInitialization
57 * @see com.sun.star.document.ExportFilter
58 * @see com.sun.star.document.XFilter
59 * @see com.sun.star.document.XExporter
60 * @see com.sun.star.beans.XPropertySet
61 * @see ifc.lang._XInitialization
62 * @see ifc.document._ExportFilter
63 * @see ifc.document._XFilter
64 * @see ifc.document._XExporter
65 * @see ifc.beans._XPropertySet
67 public class XMLExporter extends TestCase {
68 static XComponent xSheetDoc;
70 /**
71 * New spreadsheet document created.
73 @Override
74 protected void initialize( TestParameters tParam, PrintWriter log ) {
75 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
76 try {
77 log.println( "creating a calc document" );
78 xSheetDoc = SOF.openDoc("scalc","_blank");
79 } catch ( com.sun.star.uno.Exception e ) {
80 // Some exception occurs.FAILED
81 e.printStackTrace( log );
82 throw new StatusException( "Couldn't create document", e );
86 /**
87 * Spreadsheet document disposed
89 @Override
90 protected void cleanup( TestParameters tParam, PrintWriter log ) {
91 log.println( " disposing xCalcDoc " );
92 util.DesktopTools.closeDoc(xSheetDoc);
95 /**
96 * Creating a Testenvironment for the interfaces to be tested.
97 * Creates an instance of the service
98 * <code>com.sun.star.comp.Calc.XMLExporter</code> with
99 * argument which is an implementation of <code>XDocumentHandler</code>
100 * and which can check if required tags and character data is
101 * exported. <p>
102 * The calc document is set as a source document for exporter
103 * created. Checking whether tags for table corresponding to some sheet
104 * is exported.<p>
105 * Object relations created :
106 * <ul>
107 * <li> <code>'MediaDescriptor'</code> for
108 * {@link ifc.document._XFilter} interface </li>
109 * <li> <code>'XFilter.Checker'</code> for
110 * {@link ifc.document._XFilter} interface </li>
111 * <li> <code>'SourceDocument'</code> for
112 * {@link ifc.document._XExporter} interface </li>
113 * </ul>
115 @Override
116 protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
118 XMultiServiceFactory xMSF = tParam.getMSF() ;
119 XInterface oObj = null;
120 FilterChecker Filter = new FilterChecker(log);
121 Any arg = new Any(new Type(XDocumentHandler.class), Filter);
122 final String SHEET_NAME = "XMLExporter_SheetTestName";
124 try {
125 oObj = (XInterface) xMSF.createInstanceWithArguments(
126 "com.sun.star.comp.Calc.XMLExporter", new Object[] {arg} );
127 XExporter xEx = UnoRuntime.queryInterface
128 (XExporter.class,oObj);
129 xEx.setSourceDocument(xSheetDoc);
131 //set name of sheet
132 XSpreadsheetDocument xSpreadsheetDoc = UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
133 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
134 XIndexAccess xSheetsIndexArray = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
135 XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
136 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
137 XNamed xSheetNamed = UnoRuntime.queryInterface(XNamed.class, xSheet);
138 xSheetNamed.setName(SHEET_NAME);
140 log.println("fill sheet with contnet...");
141 util.CalcTools.fillCalcSheetWithContent(xSheet, 3, 3, 50, 50);
143 } catch (com.sun.star.uno.Exception e) {
144 e.printStackTrace(log) ;
145 throw new StatusException("Can't create component.", e) ;
146 } catch (java.lang.Exception e) {
147 e.printStackTrace(log);
148 throw new StatusException("Can't create environment.", e);
151 // adding tags which must be contained in XML output
152 Filter.addTag( new XMLTools.Tag("office:document") );
153 Filter.addTagEnclosed(
154 new XMLTools.Tag("office:meta"),
155 new XMLTools.Tag("office:document") );
156 Filter.addTagEnclosed(
157 new XMLTools.Tag("office:settings"),
158 new XMLTools.Tag("office:document") );
159 Filter.addTagEnclosed(
160 new XMLTools.Tag("office:script"),
161 new XMLTools.Tag("office:document") );
162 Filter.addTagEnclosed(
163 new XMLTools.Tag("office:styles"),
164 new XMLTools.Tag("office:document") );
165 Filter.addTagEnclosed(
166 new XMLTools.Tag("office:body"),
167 new XMLTools.Tag("office:document") );
168 Filter.addTagEnclosed(
169 new XMLTools.Tag("table:table"),
170 new XMLTools.Tag("office:body") );
171 Filter.addTag(
172 new XMLTools.Tag("table:table", "table:name", SHEET_NAME) );
174 // create testobject here
175 log.println( "creating a new environment" );
176 TestEnvironment tEnv = new TestEnvironment( oObj );
178 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
179 new String[] {"FilterName"},
180 new Object[] {"scalc: StarOffice XML (Calc)"}));
181 tEnv.addObjRelation("SourceDocument",xSheetDoc);
182 tEnv.addObjRelation("XFilter.Checker", Filter) ;
183 return tEnv;
187 * This class checks the XML for tags and data required and returns
188 * checking result to <code>XFilter</code> interface test. All
189 * the information about errors occurred in XML data is written
190 * to log specified.
191 * @see ifc.document._XFilter
193 private class FilterChecker extends XMLTools.XMLChecker
194 implements ifc.document._XFilter.FilterChecker {
197 * Creates a class which will write information
198 * into log specified.
200 private FilterChecker(PrintWriter log) {
201 super(log, false) ;
204 * <code>_XFilter.FilterChecker</code> interface method
205 * which returns the result of XML checking.
206 * @return <code>true</code> if the XML data exported was
207 * valid (i.e. all necessary tags and character data exists),
208 * <code>false</code> if some errors occurred.
210 public boolean checkFilter() {
211 return check();