xref: /AOO41X/main/qadevOOo/tests/java/mod/_sc/XMLSettingsImporter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.document.XImporter;
40 import com.sun.star.frame.XController;
41 import com.sun.star.frame.XModel;
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49  * Test for object which is represented by service
50  * <code>com.sun.star.comp.Calc.XMLSettingsImporter</code>. <p>
51  * Object implements the following interfaces :
52  * <ul>
53  *  <li><code>com::sun::star::lang::XInitialization</code></li>
54  *  <li><code>com::sun::star::document::XImporter</code></li>
55  *  <li><code>com::sun::star::document::XFilter</code></li>
56  *  <li><code>com::sun::star::document::ImportFilter</code></li>
57  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
58  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
59 
60  * </ul>
61  * @see com.sun.star.lang.XInitialization
62  * @see com.sun.star.document.XImporter
63  * @see com.sun.star.document.XFilter
64  * @see com.sun.star.document.ImportFilter
65  * @see com.sun.star.beans.XPropertySet
66  * @see com.sun.star.xml.sax.XDocumentHandler
67  * @see ifc.lang._XInitialization
68  * @see ifc.document._XImporter
69  * @see ifc.document._XFilter
70  * @see ifc.document._XExporter
71  * @see ifc.beans._XPropertySet
72  * @see ifc.xml.sax._XDocumentHandler
73  */
74 public class XMLSettingsImporter extends TestCase {
75     static XSpreadsheetDocument xSheetDoc;
76     static XComponent comp ;
77 
78     /**
79     * New spreadsheet document created.
80     */
81     protected void initialize( TestParameters tParam, PrintWriter log ) {
82         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
83 
84         try {
85             log.println( "creating a Spreadsheet document" );
86             xSheetDoc = SOF.createCalcDoc( null );
87             comp = (XComponent) UnoRuntime.queryInterface
88                 (XComponent.class, xSheetDoc) ;
89         } catch ( com.sun.star.uno.Exception e ) {
90             // Some exception occures.FAILED
91             e.printStackTrace( log );
92             throw new StatusException( "Couldn't create document", e );
93         }
94     }
95 
96     /**
97     * Spreadsheet document destroyed.
98     */
99     protected void cleanup( TestParameters tParam, PrintWriter log ) {
100         log.println( "    disposing document " );
101         util.DesktopTools.closeDoc(comp);
102     }
103 
104     /**
105     * Creating a Testenvironment for the interfaces to be tested.
106     * Creates an instance of the service
107     * <code>com.sun.star.comp.Calc.XMLSettingsImporter</code><p>
108     *
109     * The calc document is set as a target document for importer.
110     * Imported XML-data contains only settings tags including tag
111     * with new number of iteration steps.
112     * After import 'IterationCount' proeprty getting from
113     * target document is checked.
114     *     Object relations created :
115     * <ul>
116     *  <li> <code>'XDocumentHandler.XMLData'</code> for
117     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
118     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
119     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
120     *  <li> <code>'TargetDocument'</code> for
121     *      {@link ifc.document._XImporter} interface </li>
122     * </ul>
123     */
124     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
125                                                   PrintWriter log )
126                                                     throws StatusException {
127 
128         XInterface oObj = null;
129         Object oInt = null ;
130 
131         // creation of testobject here
132         // first we write what we are intend to do to log file
133         log.println( "creating a test environment" );
134 
135         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
136         final XPropertySet xPropSet ;
137 
138         try {
139             oInt = xMSF.createInstance
140                 ("com.sun.star.comp.Calc.XMLSettingsImporter") ;
141             XImporter imp = (XImporter) UnoRuntime.queryInterface
142                 (XImporter.class, oInt) ;
143             imp.setTargetDocument(comp) ;
144 
145             XModel xSheetModel = (XModel)
146                 UnoRuntime.queryInterface(XModel.class, xSheetDoc);
147             XController xController = xSheetModel.getCurrentController();
148             xPropSet = (XPropertySet)
149                 UnoRuntime.queryInterface(XPropertySet.class, xController);
150         } catch (com.sun.star.uno.Exception e) {
151             e.printStackTrace(log) ;
152             throw new StatusException("Can't create component.", e) ;
153         }
154 
155         oObj = (XInterface) oInt ;
156 
157         // create testobject here
158         log.println( "creating a new environment for Paragraph object" );
159         TestEnvironment tEnv = new TestEnvironment( oObj );
160 
161         // adding relation
162         tEnv.addObjRelation("TargetDocument", comp) ;
163 
164         // adding relation for XDocumentHandler
165         String[][] xml = new String[][] {
166             {"start", "office:document-settings",
167                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
168                 "xmlns:xlink", "CDATA", "http://www.w3.org/1999/xlink",
169                 "xmlns:config", "CDATA", "http://openoffice.org/2001/config"},
170             {"start", "office:settings"},
171             {"start", "config:config-item-set",
172                 "config:name", "CDATA", "view-settings"},
173             {"start", "config:config-item-map-indexed",
174                 "config:name", "CDATA", "Views"},
175             {"start", "config:config-item-map-entry"},
176             {"start", "config:config-item",
177                 "config:name", "CDATA", "ShowGrid",
178                 "config:type", "CDATA", "boolean"},
179             {"chars", "false"},
180             {"end", "config:config-item"},
181             {"start", "config:config-item",
182                 "config:name", "CDATA", "ZoomType",
183                 "config:type", "CDATA", "short"},
184             {"chars", "0"},
185             {"start", "config:config-item",
186                 "config:name", "CDATA", "ZoomValue",
187                 "config:type", "CDATA", "int"},
188             {"chars", "124"},
189             {"end", "config:config-item"},
190             {"end", "config:config-item"},
191             {"end", "config:config-item-map-entry"},
192             {"end", "config:config-item-map-indexed"},
193             {"end", "config:config-item-set"},
194             {"end", "office:settings"},
195             {"end", "office:document-settings"}} ;
196 
197         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
198 
199         final PrintWriter logF = log ;
200 
201         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
202             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
203                 public boolean checkImport() {
204                     try {
205                         Object gVal = xPropSet.getPropertyValue("ShowGrid") ;
206                         logF.println("ShowGrid = " + gVal ) ;
207                         return  "false".equals(gVal) ;
208                     } catch (com.sun.star.uno.Exception e) {
209                         logF.println("Exception occured while checking filter :") ;
210                         e.printStackTrace(logF) ;
211                         return false ;
212                     }
213                 }
214             }) ;
215 
216         return tEnv;
217     } // finish method getTestEnvironment
218 }
219 
220