1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 import util.XMLTools;
34 
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.document.XExporter;
37 import com.sun.star.frame.XController;
38 import com.sun.star.frame.XModel;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.Any;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 import com.sun.star.xml.sax.XDocumentHandler;
46 
47 /**
48  * Test for object which is represented by service
49  * <code>com.sun.star.comp.Calc.XMLSettingsExporter</code>. <p>
50  * Object implements the following interfaces :
51  * <ul>
52  *  <li><code>com::sun::star::lang::XInitialization</code></li>
53  *  <li><code>com::sun::star::document::ExportFilter</code></li>
54  *  <li><code>com::sun::star::document::XFilter</code></li>
55  *  <li><code>com::sun::star::document::XExporter</code></li>
56  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
57  * </ul>
58  * @see com.sun.star.lang.XInitialization
59  * @see com.sun.star.document.ExportFilter
60  * @see com.sun.star.document.XFilter
61  * @see com.sun.star.document.XExporter
62  * @see com.sun.star.beans.XPropertySet
63  * @see ifc.lang._XInitialization
64  * @see ifc.document._ExportFilter
65  * @see ifc.document._XFilter
66  * @see ifc.document._XExporter
67  * @see ifc.beans._XPropertySet
68  */
69 public class XMLSettingsExporter extends TestCase {
70 
71     static XComponent xSheetDoc;
72 
73     /**
74      * New spreadsheet document created.
75      */
76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78 
79         try {
80             log.println( "creating a calc document" );
81             xSheetDoc = SOF.openDoc("scalc","_blank");
82         } catch ( com.sun.star.uno.Exception e ) {
83             // Some exception occures.FAILED
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document", e );
86         }
87     }
88 
89     /**
90      * Spreadsheet document disposed
91      */
92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         log.println( "    disposing xCalcDoc " );
94         util.DesktopTools.closeDoc(xSheetDoc);
95     }
96 
97     /**
98     * Creating a Testenvironment for the interfaces to be tested.
99     * Creates an instance of the service
100     * <code>com.sun.star.comp.Calc.XMLSettingsExporter</code> with
101     * argument which is an implementation of <code>XDocumentHandler</code>
102     * and which can check if required tags and character data is
103     * exported. <p>
104     * The calc document is set as a source document for exporter
105     * created. Sets settings' property 'ShowGrid' to 'false', then checks
106     * it in the exported xml document.<p>
107     *     Object relations created :
108     * <ul>
109     *  <li> <code>'MediaDescriptor'</code> for
110     *      {@link ifc.document._XFilter} interface </li>
111     *  <li> <code>'XFilter.Checker'</code> for
112     *      {@link ifc.document._XFilter} interface </li>
113     *  <li> <code>'SourceDocument'</code> for
114     *      {@link ifc.document._XExporter} interface </li>
115     * </ul>
116     */
117     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
118                                                   PrintWriter log )
119                                                     throws StatusException {
120 
121         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
122         XInterface oObj = null;
123         SettingsFilterChecker filter = new SettingsFilterChecker(log);
124         Any arg = new Any(new Type(XDocumentHandler.class), filter);
125         try {
126             oObj = (XInterface) xMSF.createInstanceWithArguments(
127                 "com.sun.star.comp.Calc.XMLSettingsExporter",
128                 new Object[] {arg} );
129             XExporter xEx = (XExporter) UnoRuntime.queryInterface
130                 (XExporter.class,oObj);
131             xEx.setSourceDocument(xSheetDoc);
132 
133             //set some settings
134             XModel xSheetModel = (XModel)
135                 UnoRuntime.queryInterface(XModel.class, xSheetDoc);
136             XController xController = xSheetModel.getCurrentController();
137             XPropertySet xPropSet = (XPropertySet)
138                 UnoRuntime.queryInterface(XPropertySet.class, xController);
139             xPropSet.setPropertyValue("ShowGrid", "false");
140 
141             util.CalcTools.fillCalcSheetWithContent(xSheetDoc,1, 3, 3, 50, 50);
142 
143         } catch (com.sun.star.uno.Exception e) {
144             e.printStackTrace(log) ;
145             throw new StatusException("Can't create component.", e) ;
146         } catch (java.lang.Exception e) {
147             e.printStackTrace(log);
148             throw new StatusException("Can't create environment.", e);
149         }
150 
151         //Create and prepare filter
152         // adding tags which must be contained in XML output
153         filter.addTag(new XMLTools.Tag("office:document-settings") );
154         filter.addTagEnclosed(
155             new XMLTools.Tag("office:settings"),
156             new XMLTools.Tag("office:document-settings") );
157         filter.addCharactersEnclosed(
158             "false",
159             new XMLTools.Tag("config:config-item", "config:name", "ShowGrid") );
160 
161         // create testobject here
162         log.println( "creating a new environment" );
163         TestEnvironment tEnv = new TestEnvironment( oObj );
164 
165         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
166             new String[] {"FilterName"},
167             new Object[] {"scalc: StarOffice XML (Calc)"}));
168         tEnv.addObjRelation("SourceDocument",xSheetDoc);
169         tEnv.addObjRelation("XFilter.Checker", filter) ;
170         return tEnv;
171     }
172 
173     /**
174      * This class checks the XML for tags and data required and returns
175      * checking result to <code>XFilter</code> interface test. All
176      * the information about errors occured in XML data is written
177      * to log specified.
178      * @see ifc.document._XFilter
179      */
180     protected class SettingsFilterChecker extends XMLTools.XMLChecker
181       implements ifc.document._XFilter.FilterChecker {
182 
183         /**
184          * Creates a class which will write information
185          * into log specified.
186          */
187         public SettingsFilterChecker(PrintWriter log) {
188             super(log, false) ;
189         }
190         /**
191          * <code>_XFilter.FilterChecker</code> interface method
192          * which returns the result of XML checking.
193          * @return <code>true</code> if the XML data exported was
194          * valid (i.e. all necessary tags and character data exists),
195          * <code>false</code> if some errors occured.
196          */
197         public boolean checkFilter() {
198             return check();
199         }
200     }
201 }
202 
203