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