Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / XMLSettingsExporter.java
blob287049d7e3bbbe56edbb5488a8f37b45f35cad10
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.beans.XPropertySet;
31 import com.sun.star.document.XExporter;
32 import com.sun.star.frame.XController;
33 import com.sun.star.frame.XModel;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.uno.Any;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.xml.sax.XDocumentHandler;
42 /**
43 * Test for object which is represented by service
44 * <code>com.sun.star.comp.Calc.XMLSettingsExporter</code>. <p>
45 * Object implements the following interfaces :
46 * <ul>
47 * <li><code>com::sun::star::lang::XInitialization</code></li>
48 * <li><code>com::sun::star::document::ExportFilter</code></li>
49 * <li><code>com::sun::star::document::XFilter</code></li>
50 * <li><code>com::sun::star::document::XExporter</code></li>
51 * <li><code>com::sun::star::beans::XPropertySet</code></li>
52 * </ul>
53 * @see com.sun.star.lang.XInitialization
54 * @see com.sun.star.document.ExportFilter
55 * @see com.sun.star.document.XFilter
56 * @see com.sun.star.document.XExporter
57 * @see com.sun.star.beans.XPropertySet
58 * @see ifc.lang._XInitialization
59 * @see ifc.document._ExportFilter
60 * @see ifc.document._XFilter
61 * @see ifc.document._XExporter
62 * @see ifc.beans._XPropertySet
64 public class XMLSettingsExporter extends TestCase {
66 static XComponent xSheetDoc;
68 /**
69 * New spreadsheet document created.
71 @Override
72 protected void initialize( TestParameters tParam, PrintWriter log ) {
73 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
75 try {
76 log.println( "creating a calc document" );
77 xSheetDoc = SOF.openDoc("scalc","_blank");
78 } catch ( com.sun.star.uno.Exception e ) {
79 // Some exception occurs.FAILED
80 e.printStackTrace( log );
81 throw new StatusException( "Couldn't create document", e );
85 /**
86 * Spreadsheet document disposed
88 @Override
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.XMLSettingsExporter</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. Sets settings' property 'ShowGrid' to 'false', then checks
103 * it in the exported xml document.<p>
104 * Object relations created :
105 * <ul>
106 * <li> <code>'MediaDescriptor'</code> for
107 * {@link ifc.document._XFilter} interface </li>
108 * <li> <code>'XFilter.Checker'</code> for
109 * {@link ifc.document._XFilter} interface </li>
110 * <li> <code>'SourceDocument'</code> for
111 * {@link ifc.document._XExporter} interface </li>
112 * </ul>
114 @Override
115 public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
116 PrintWriter log )
117 throws StatusException {
119 XMultiServiceFactory xMSF = tParam.getMSF() ;
120 XInterface oObj = null;
121 SettingsFilterChecker filter = new SettingsFilterChecker(log);
122 Any arg = new Any(new Type(XDocumentHandler.class), filter);
123 try {
124 oObj = (XInterface) xMSF.createInstanceWithArguments(
125 "com.sun.star.comp.Calc.XMLSettingsExporter",
126 new Object[] {arg} );
127 XExporter xEx = UnoRuntime.queryInterface
128 (XExporter.class,oObj);
129 xEx.setSourceDocument(xSheetDoc);
131 //set some settings
132 XModel xSheetModel = UnoRuntime.queryInterface(XModel.class, xSheetDoc);
133 XController xController = xSheetModel.getCurrentController();
134 XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xController);
135 xPropSet.setPropertyValue("ShowGrid", "false");
137 util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 0, 3, 3, 50, 50);
139 } catch (com.sun.star.uno.Exception e) {
140 e.printStackTrace(log) ;
141 throw new StatusException("Can't create component.", e) ;
142 } catch (java.lang.Exception e) {
143 e.printStackTrace(log);
144 throw new StatusException("Can't create environment.", e);
147 //Create and prepare filter
148 // adding tags which must be contained in XML output
149 filter.addTag(new XMLTools.Tag("office:document-settings") );
150 filter.addTagEnclosed(
151 new XMLTools.Tag("office:settings"),
152 new XMLTools.Tag("office:document-settings") );
153 filter.addCharactersEnclosed(
154 "false",
155 new XMLTools.Tag("config:config-item", "config:name", "ShowGrid") );
157 // create testobject here
158 log.println( "creating a new environment" );
159 TestEnvironment tEnv = new TestEnvironment( oObj );
161 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
162 new String[] {"FilterName"},
163 new Object[] {"scalc: StarOffice XML (Calc)"}));
164 tEnv.addObjRelation("SourceDocument",xSheetDoc);
165 tEnv.addObjRelation("XFilter.Checker", filter) ;
166 return tEnv;
170 * This class checks the XML for tags and data required and returns
171 * checking result to <code>XFilter</code> interface test. All
172 * the information about errors occurred in XML data is written
173 * to log specified.
174 * @see ifc.document._XFilter
176 private class SettingsFilterChecker extends XMLTools.XMLChecker
177 implements ifc.document._XFilter.FilterChecker {
180 * Creates a class which will write information
181 * into log specified.
183 private SettingsFilterChecker(PrintWriter log) {
184 super(log, false) ;
187 * <code>_XFilter.FilterChecker</code> interface method
188 * which returns the result of XML checking.
189 * @return <code>true</code> if the XML data exported was
190 * valid (i.e. all necessary tags and character data exists),
191 * <code>false</code> if some errors occurred.
193 public boolean checkFilter() {
194 return check();