merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sc / XMLStylesExporter.java
blob71710c9a23ed1bd8c8a3d2948839ee2693a64bee
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: XMLStylesExporter.java,v $
10 * $Revision: 1.9 $
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.XNameAccess;
43 import com.sun.star.container.XNameContainer;
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.style.XStyle;
48 import com.sun.star.style.XStyleFamiliesSupplier;
49 import com.sun.star.uno.Any;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 import com.sun.star.xml.sax.XDocumentHandler;
56 /**
57 * Test for object which is represented by service
58 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code>. <p>
59 * Object implements the following interfaces :
60 * <ul>
61 * <li><code>com::sun::star::lang::XInitialization</code></li>
62 * <li><code>com::sun::star::document::ExportFilter</code></li>
63 * <li><code>com::sun::star::document::XFilter</code></li>
64 * <li><code>com::sun::star::document::XExporter</code></li>
65 * <li><code>com::sun::star::beans::XPropertySet</code></li>
66 * </ul>
67 * @see com.sun.star.lang.XInitialization
68 * @see com.sun.star.document.ExportFilter
69 * @see com.sun.star.document.XFilter
70 * @see com.sun.star.document.XExporter
71 * @see com.sun.star.beans.XPropertySet
72 * @see ifc.lang._XInitialization
73 * @see ifc.document._ExportFilter
74 * @see ifc.document._XFilter
75 * @see ifc.document._XExporter
76 * @see ifc.beans._XPropertySet
78 public class XMLStylesExporter extends TestCase {
79 static XComponent xSheetDoc;
80 protected int counter = 0;
82 /**
83 * New spreadsheet document created.
85 protected void initialize( TestParameters tParam, PrintWriter log ) {
86 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF() );
88 try {
89 log.println( "creating a calc document" );
90 xSheetDoc = SOF.openDoc("scalc","_blank");
91 } catch ( com.sun.star.uno.Exception e ) {
92 // Some exception occures.FAILED
93 e.printStackTrace( log );
94 throw new StatusException( "Couldn't create document", e );
98 /**
99 * Spreadsheet document disposed
101 protected void cleanup( TestParameters tParam, PrintWriter log ) {
102 log.println( " disposing xCalcDoc " );
103 util.DesktopTools.closeDoc(xSheetDoc);
107 * Creating a Testenvironment for the interfaces to be tested.
108 * Creates an instance of the service
109 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code> with
110 * argument which is an implementation of <code>XDocumentHandler</code>
111 * and which can check if required tags and character data is
112 * exported. <p>
113 * The calc document is set as a source document for exporter
114 * created. New style 'NewStyle' added to style family "Cell Styles".
115 * This made for checking if this style is successfully exported within
116 * the document styles information.
117 * Object relations created :
118 * <ul>
119 * <li> <code>'MediaDescriptor'</code> for
120 * {@link ifc.document._XFilter} interface </li>
121 * <li> <code>'XFilter.Checker'</code> for
122 * {@link ifc.document._XFilter} interface </li>
123 * <li> <code>'SourceDocument'</code> for
124 * {@link ifc.document._XExporter} interface </li>
125 * </ul>
127 protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
129 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF() );
130 XMultiServiceFactory xMSF = (XMultiServiceFactory) tParam.getMSF() ;
131 XInterface oObj = null;
132 FilterChecker filter = new FilterChecker(log);
133 Any arg = new Any(new Type(XDocumentHandler.class),filter);
135 String newName = "NewStyle" + counter++;
136 // Adding tags for checking existance of head tag and property value
137 filter.addTag(new XMLTools.Tag("office:document-styles"));
138 filter.addTag(new XMLTools.Tag("style:style","style:name", newName));
140 try {
141 oObj = (XInterface) xMSF.createInstanceWithArguments(
142 "com.sun.star.comp.Calc.XMLStylesExporter", new Object[] {arg});
143 XExporter xEx = (XExporter)
144 UnoRuntime.queryInterface(XExporter.class,oObj);
145 xEx.setSourceDocument(xSheetDoc);
147 // Obtaining and changing property values
148 XStyleFamiliesSupplier styleSup = (XStyleFamiliesSupplier)
149 UnoRuntime.queryInterface(
150 XStyleFamiliesSupplier.class, xSheetDoc);
151 XNameAccess StyleFamilies = styleSup.getStyleFamilies();
152 String[] styleFamiliesNames = StyleFamilies.getElementNames();
153 XNameContainer StyleFamilyName = (XNameContainer)
154 AnyConverter.toObject(new Type(XNameContainer.class),
155 StyleFamilies.getByName(styleFamiliesNames[0]));
156 Object SC = SOF.createInstance(
157 xSheetDoc, "com.sun.star.style.CellStyle");
158 XStyle StyleCell = (XStyle)
159 UnoRuntime.queryInterface(XStyle.class,SC);
160 StyleFamilyName.insertByName(newName, StyleCell);
162 log.println("fill sheet 1 with contnet...");
163 util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 1, 3, 3, 50, 100);
165 } catch (com.sun.star.uno.Exception e) {
166 e.printStackTrace(log);
167 throw new StatusException("Can't create environment.", e);
168 } catch (java.lang.Exception e) {
169 e.printStackTrace(log);
170 throw new StatusException("Can't create environment.", e);
173 // create testobject here
174 log.println( "creating a new environment" );
175 TestEnvironment tEnv = new TestEnvironment( oObj );
177 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
178 new String[] {"FilterName"},
179 new Object[] {"scalc: StarOffice XML (Calc)"}));
180 tEnv.addObjRelation("SourceDocument",xSheetDoc);
181 tEnv.addObjRelation("XFilter.Checker", filter) ;
182 return tEnv;
186 * This class checks the XML for tags and data required and returns
187 * checking result to <code>XFilter</code> interface test. All
188 * the information about errors occured in XML data is written
189 * to log specified.
190 * @see ifc.document._XFilter
192 protected class FilterChecker extends XMLTools.XMLChecker
193 implements ifc.document._XFilter.FilterChecker {
196 * Creates a class which will write information
197 * into log specified.
199 public FilterChecker(PrintWriter log) {
200 super(log,false) ;
203 * <code>_XFilter.FilterChecker</code> interface method
204 * which returns the result of XML checking.
205 * @return <code>true</code> if the XML data exported was
206 * valid (i.e. all necessary tags and character data exists),
207 * <code>false</code> if some errors occured.
209 public boolean checkFilter() {
210 return check();