bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / XMLStylesExporter.java
blobe2d7869ac372e8a0cbea92583747645f76ceacca
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.XNameAccess;
31 import com.sun.star.container.XNameContainer;
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.style.XStyle;
36 import com.sun.star.style.XStyleFamiliesSupplier;
37 import com.sun.star.uno.Any;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.xml.sax.XDocumentHandler;
44 /**
45 * Test for object which is represented by service
46 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code>. <p>
47 * Object implements the following interfaces :
48 * <ul>
49 * <li><code>com::sun::star::lang::XInitialization</code></li>
50 * <li><code>com::sun::star::document::ExportFilter</code></li>
51 * <li><code>com::sun::star::document::XFilter</code></li>
52 * <li><code>com::sun::star::document::XExporter</code></li>
53 * <li><code>com::sun::star::beans::XPropertySet</code></li>
54 * </ul>
55 * @see com.sun.star.lang.XInitialization
56 * @see com.sun.star.document.ExportFilter
57 * @see com.sun.star.document.XFilter
58 * @see com.sun.star.document.XExporter
59 * @see com.sun.star.beans.XPropertySet
60 * @see ifc.lang._XInitialization
61 * @see ifc.document._ExportFilter
62 * @see ifc.document._XFilter
63 * @see ifc.document._XExporter
64 * @see ifc.beans._XPropertySet
66 public class XMLStylesExporter extends TestCase {
67 static XComponent xSheetDoc;
68 protected int counter = 0;
70 /**
71 * New spreadsheet document created.
73 protected void initialize( TestParameters tParam, PrintWriter log ) {
74 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) 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 protected void cleanup( TestParameters tParam, PrintWriter log ) {
90 log.println( " disposing xCalcDoc " );
91 util.DesktopTools.closeDoc(xSheetDoc);
94 /**
95 * Creating a Testenvironment for the interfaces to be tested.
96 * Creates an instance of the service
97 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code> with
98 * argument which is an implementation of <code>XDocumentHandler</code>
99 * and which can check if required tags and character data is
100 * exported. <p>
101 * The calc document is set as a source document for exporter
102 * created. New style 'NewStyle' added to style family "Cell Styles".
103 * This made for checking if this style is successfully exported within
104 * the document styles information.
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 protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
117 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF() );
118 XMultiServiceFactory xMSF = (XMultiServiceFactory) tParam.getMSF() ;
119 XInterface oObj = null;
120 FilterChecker filter = new FilterChecker(log);
121 Any arg = new Any(new Type(XDocumentHandler.class),filter);
123 String newName = "NewStyle" + counter++;
124 // Adding tags for checking existance of head tag and property value
125 filter.addTag(new XMLTools.Tag("office:document-styles"));
126 filter.addTag(new XMLTools.Tag("style:style","style:name", newName));
128 try {
129 oObj = (XInterface) xMSF.createInstanceWithArguments(
130 "com.sun.star.comp.Calc.XMLStylesExporter", new Object[] {arg});
131 XExporter xEx = UnoRuntime.queryInterface(XExporter.class,oObj);
132 xEx.setSourceDocument(xSheetDoc);
134 // Obtaining and changing property values
135 XStyleFamiliesSupplier styleSup = UnoRuntime.queryInterface(
136 XStyleFamiliesSupplier.class, xSheetDoc);
137 XNameAccess StyleFamilies = styleSup.getStyleFamilies();
138 String[] styleFamiliesNames = StyleFamilies.getElementNames();
139 XNameContainer StyleFamilyName = (XNameContainer)
140 AnyConverter.toObject(new Type(XNameContainer.class),
141 StyleFamilies.getByName(styleFamiliesNames[0]));
142 Object SC = SOF.createInstance(
143 xSheetDoc, "com.sun.star.style.CellStyle");
144 XStyle StyleCell = UnoRuntime.queryInterface(XStyle.class,SC);
145 StyleFamilyName.insertByName(newName, StyleCell);
147 log.println("fill sheet 1 with content...");
148 util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 0, 3, 3, 50, 100);
150 } catch (com.sun.star.uno.Exception e) {
151 e.printStackTrace(log);
152 throw new StatusException("Can't create environment.", e);
153 } catch (java.lang.Exception e) {
154 e.printStackTrace(log);
155 throw new StatusException("Can't create environment.", e);
158 // create testobject here
159 log.println( "creating a new environment" );
160 TestEnvironment tEnv = new TestEnvironment( oObj );
162 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
163 new String[] {"FilterName"},
164 new Object[] {"scalc: StarOffice XML (Calc)"}));
165 tEnv.addObjRelation("SourceDocument",xSheetDoc);
166 tEnv.addObjRelation("XFilter.Checker", filter) ;
167 return tEnv;
171 * This class checks the XML for tags and data required and returns
172 * checking result to <code>XFilter</code> interface test. All
173 * the information about errors occurred in XML data is written
174 * to log specified.
175 * @see ifc.document._XFilter
177 protected class FilterChecker extends XMLTools.XMLChecker
178 implements ifc.document._XFilter.FilterChecker {
181 * Creates a class which will write information
182 * into log specified.
184 public FilterChecker(PrintWriter log) {
185 super(log,false) ;
188 * <code>_XFilter.FilterChecker</code> interface method
189 * which returns the result of XML checking.
190 * @return <code>true</code> if the XML data exported was
191 * valid (i.e. all necessary tags and character data exists),
192 * <code>false</code> if some errors occurred.
194 public boolean checkFilter() {
195 return check();