1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 import util.XMLTools;
38 
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.document.XExporter;
41 import com.sun.star.frame.XController;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.uno.Any;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 import com.sun.star.xml.sax.XDocumentHandler;
50 
51 /**
52  * Test for object which is represented by service
53  * <code>com.sun.star.comp.Calc.XMLSettingsExporter</code>. <p>
54  * Object implements the following interfaces :
55  * <ul>
56  *  <li><code>com::sun::star::lang::XInitialization</code></li>
57  *  <li><code>com::sun::star::document::ExportFilter</code></li>
58  *  <li><code>com::sun::star::document::XFilter</code></li>
59  *  <li><code>com::sun::star::document::XExporter</code></li>
60  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
61  * </ul>
62  * @see com.sun.star.lang.XInitialization
63  * @see com.sun.star.document.ExportFilter
64  * @see com.sun.star.document.XFilter
65  * @see com.sun.star.document.XExporter
66  * @see com.sun.star.beans.XPropertySet
67  * @see ifc.lang._XInitialization
68  * @see ifc.document._ExportFilter
69  * @see ifc.document._XFilter
70  * @see ifc.document._XExporter
71  * @see ifc.beans._XPropertySet
72  */
73 public class XMLSettingsExporter extends TestCase {
74 
75     static XComponent xSheetDoc;
76 
77     /**
78      * New spreadsheet document created.
79      */
80     protected void initialize( TestParameters tParam, PrintWriter log ) {
81         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
82 
83         try {
84             log.println( "creating a calc document" );
85             xSheetDoc = SOF.openDoc("scalc","_blank");
86         } catch ( com.sun.star.uno.Exception e ) {
87             // Some exception occures.FAILED
88             e.printStackTrace( log );
89             throw new StatusException( "Couldn't create document", e );
90         }
91     }
92 
93     /**
94      * Spreadsheet document disposed
95      */
96     protected void cleanup( TestParameters tParam, PrintWriter log ) {
97         log.println( "    disposing xCalcDoc " );
98         util.DesktopTools.closeDoc(xSheetDoc);
99     }
100 
101     /**
102     * Creating a Testenvironment for the interfaces to be tested.
103     * Creates an instance of the service
104     * <code>com.sun.star.comp.Calc.XMLSettingsExporter</code> with
105     * argument which is an implementation of <code>XDocumentHandler</code>
106     * and which can check if required tags and character data is
107     * exported. <p>
108     * The calc document is set as a source document for exporter
109     * created. Sets settings' property 'ShowGrid' to 'false', then checks
110     * it in the exported xml document.<p>
111     *     Object relations created :
112     * <ul>
113     *  <li> <code>'MediaDescriptor'</code> for
114     *      {@link ifc.document._XFilter} interface </li>
115     *  <li> <code>'XFilter.Checker'</code> for
116     *      {@link ifc.document._XFilter} interface </li>
117     *  <li> <code>'SourceDocument'</code> for
118     *      {@link ifc.document._XExporter} interface </li>
119     * </ul>
120     */
121     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
122                                                   PrintWriter log )
123                                                     throws StatusException {
124 
125         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
126         XInterface oObj = null;
127         SettingsFilterChecker filter = new SettingsFilterChecker(log);
128         Any arg = new Any(new Type(XDocumentHandler.class), filter);
129         try {
130             oObj = (XInterface) xMSF.createInstanceWithArguments(
131                 "com.sun.star.comp.Calc.XMLSettingsExporter",
132                 new Object[] {arg} );
133             XExporter xEx = (XExporter) UnoRuntime.queryInterface
134                 (XExporter.class,oObj);
135             xEx.setSourceDocument(xSheetDoc);
136 
137             //set some settings
138             XModel xSheetModel = (XModel)
139                 UnoRuntime.queryInterface(XModel.class, xSheetDoc);
140             XController xController = xSheetModel.getCurrentController();
141             XPropertySet xPropSet = (XPropertySet)
142                 UnoRuntime.queryInterface(XPropertySet.class, xController);
143             xPropSet.setPropertyValue("ShowGrid", "false");
144 
145             util.CalcTools.fillCalcSheetWithContent(xSheetDoc,1, 3, 3, 50, 50);
146 
147         } catch (com.sun.star.uno.Exception e) {
148             e.printStackTrace(log) ;
149             throw new StatusException("Can't create component.", e) ;
150         } catch (java.lang.Exception e) {
151             e.printStackTrace(log);
152             throw new StatusException("Can't create environment.", e);
153         }
154 
155         //Create and prepare filter
156         // adding tags which must be contained in XML output
157         filter.addTag(new XMLTools.Tag("office:document-settings") );
158         filter.addTagEnclosed(
159             new XMLTools.Tag("office:settings"),
160             new XMLTools.Tag("office:document-settings") );
161         filter.addCharactersEnclosed(
162             "false",
163             new XMLTools.Tag("config:config-item", "config:name", "ShowGrid") );
164 
165         // create testobject here
166         log.println( "creating a new environment" );
167         TestEnvironment tEnv = new TestEnvironment( oObj );
168 
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;
175     }
176 
177     /**
178      * This class checks the XML for tags and data required and returns
179      * checking result to <code>XFilter</code> interface test. All
180      * the information about errors occured in XML data is written
181      * to log specified.
182      * @see ifc.document._XFilter
183      */
184     protected class SettingsFilterChecker extends XMLTools.XMLChecker
185       implements ifc.document._XFilter.FilterChecker {
186 
187         /**
188          * Creates a class which will write information
189          * into log specified.
190          */
191         public SettingsFilterChecker(PrintWriter log) {
192             super(log, false) ;
193         }
194         /**
195          * <code>_XFilter.FilterChecker</code> interface method
196          * which returns the result of XML checking.
197          * @return <code>true</code> if the XML data exported was
198          * valid (i.e. all necessary tags and character data exists),
199          * <code>false</code> if some errors occured.
200          */
201         public boolean checkFilter() {
202             return check();
203         }
204     }
205 }
206 
207