Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / XMLContentExporter.java
blobaed4929d92331290f5e1000e6916e9dbb33848cc
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.document.XExporter;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.sheet.XSpreadsheet;
35 import com.sun.star.sheet.XSpreadsheetDocument;
36 import com.sun.star.sheet.XSpreadsheets;
37 import com.sun.star.table.XCell;
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.XMLContentExporter</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 XMLContentExporter extends TestCase {
69 static XComponent xSheetDoc;
70 static ContentFilterChecker Filter;
72 /**
73 * New spreadsheet document created.
75 @Override
76 protected void initialize( TestParameters tParam, PrintWriter log ) {
77 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
78 try {
79 log.println( "creating a calc document" );
80 xSheetDoc = SOF.openDoc("scalc","_blank");
82 } catch ( com.sun.star.uno.Exception e ) {
83 // Some exception occurs.FAILED
84 e.printStackTrace( log );
85 throw new StatusException( "Couldn't create document", e );
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.XMLContentExporter</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. A cell in the sheet is set to some value. This made
104 * for checking if this value is successfully exported within
105 * the document content.
106 * Object relations created :
107 * <ul>
108 * <li> <code>'MediaDescriptor'</code> for
109 * {@link ifc.document._XFilter} interface </li>
110 * <li> <code>'XFilter.Checker'</code> for
111 * {@link ifc.document._XFilter} interface </li>
112 * <li> <code>'SourceDocument'</code> for
113 * {@link ifc.document._XExporter} interface </li>
114 * </ul>
116 @Override
117 protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
119 XMultiServiceFactory xMSF = tParam.getMSF() ;
120 XInterface oObj = null;
121 final String CELL_TEXT = "XMLContentExporter";
123 ContentFilterChecker Filter = new ContentFilterChecker(log);
125 Any arg = new Any(new Type(XDocumentHandler.class), Filter);
126 try {
127 oObj = (XInterface) xMSF.createInstanceWithArguments(
128 "com.sun.star.comp.Calc.XMLContentExporter",
129 new Object[] {arg} );
130 XExporter xEx = UnoRuntime.queryInterface
131 (XExporter.class,oObj);
132 xEx.setSourceDocument(xSheetDoc);
134 // Setting some string to a cell
135 XSpreadsheetDocument xSpreadsheetDoc = UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
136 XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
137 XIndexAccess xSheetsIndexArray = UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
138 XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
139 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
140 XCell xCell = xSheet.getCellByPosition(0, 0);
141 xCell.setFormula(CELL_TEXT);
143 log.println("fill sheet 1 with contnet...");
144 util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 1, 1, 1, 5, 5);
146 } catch (com.sun.star.uno.Exception e) {
147 e.printStackTrace(log) ;
148 throw new StatusException("Can't create component.", e) ;
149 } catch (java.lang.Exception e) {
150 e.printStackTrace(log);
151 throw new StatusException("Can't create environment.", e);
154 // adding tags which must be contained in XML output
155 Filter.addTag("office:document-content");
156 Filter.addTagEnclosed("office:body", "office:document-content");
157 Filter.addTagEnclosed("office:script", "office:document-content");
158 Filter.addTagEnclosed("table:table", "office:body");
159 Filter.addTagEnclosed("table:table-column", "table:table");
160 Filter.addTagEnclosed("table:table-row", "table:table");
161 Filter.addTagEnclosed("table:table-cell", "table:table-row");
162 Filter.addTagEnclosed("text:p", "table:table-cell");
163 Filter.addCharactersEnclosed(CELL_TEXT, "text:p");
165 // create testobject here
166 log.println( "creating a new environment" );
167 TestEnvironment tEnv = new TestEnvironment( oObj );
169 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
170 new String[] {"FilterName"},
171 new Object[] {"scalc: StarOffice XML (Calc)"}));
172 tEnv.addObjRelation("SourceDocument",xSheetDoc);
173 tEnv.addObjRelation("XFilter.Checker", Filter);
174 return tEnv;
179 * This class checks the XML for tags and data required and returns
180 * checking result to <code>XFilter</code> interface test. All
181 * the information about errors occurred in XML data is written
182 * to log specified.
183 * @see ifc.document._XFilter
185 private class ContentFilterChecker extends XMLTools.XMLTagsChecker
186 implements ifc.document._XFilter.FilterChecker {
189 * Creates a class which will write information
190 * into log specified.
192 private ContentFilterChecker(PrintWriter log) {
193 super(log) ;
197 * <code>_XFilter.FilterChecker</code> interface method
198 * which returns the result of XML checking.
199 * @return <code>true</code> if the XML data exported was
200 * valid (i.e. all necessary tags and character data exists),
201 * <code>false</code> if some errors occurred.
203 public boolean checkFilter() {
204 return checkTags();