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.document.XImporter;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XSpreadsheetDocument;
38 import com.sun.star.sheet.XSpreadsheets;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 /**
42  * Test for object which is represented by service
43  * <code>com.sun.star.comp.Calc.XMLImporter</code>. <p>
44  * Object implements the following interfaces :
45  * <ul>
46  *  <li><code>com::sun::star::lang::XInitialization</code></li>
47  *  <li><code>com::sun::star::document::XImporter</code></li>
48  *  <li><code>com::sun::star::document::XFilter</code></li>
49  *  <li><code>com::sun::star::document::ImportFilter</code></li>
50  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
51  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
52 
53  * </ul>
54  * @see com.sun.star.lang.XInitialization
55  * @see com.sun.star.document.XImporter
56  * @see com.sun.star.document.XFilter
57  * @see com.sun.star.document.ImportFilter
58  * @see com.sun.star.beans.XPropertySet
59  * @see com.sun.star.xml.sax.XDocumentHandler
60  * @see ifc.lang._XInitialization
61  * @see ifc.document._XImporter
62  * @see ifc.document._XFilter
63  * @see ifc.document._XExporter
64  * @see ifc.beans._XPropertySet
65  * @see ifc.xml.sax._XDocumentHandler
66  */
67 public class XMLImporter extends TestCase {
68 
69     static XComponent xSheetDoc;
70 
71     /**
72      * New spreadsheet document created.
73      */
initialize( TestParameters tParam, PrintWriter log )74     protected void initialize( TestParameters tParam, PrintWriter log ) {
75         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
76 
77         try {
78             log.println( "creating a Spreadsheet document" );
79             XSpreadsheetDocument xSpreadsheetDoc = SOF.createCalcDoc( null );
80             xSheetDoc = (XComponent) UnoRuntime.queryInterface
81                 (XComponent.class, xSpreadsheetDoc);
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 destroyed.
91     */
cleanup( TestParameters tParam, PrintWriter log )92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         log.println( "    disposing document " );
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.XMLImporter</code><p>
101     *
102     * The calc document is set as a target document for importer.
103     * Imported tags contain new value for table name.
104     * After import table name getting from
105     * target document is checked.
106     *     Object relations created :
107     * <ul>
108     *  <li> <code>'XDocumentHandler.XMLData'</code> for
109     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
110     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
111     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
112     *  <li> <code>'TargetDocument'</code> for
113     *      {@link ifc.document._XImporter} interface </li>
114     * </ul>
115     */
createTestEnvironment( TestParameters tParam, PrintWriter log )116     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
117                                                   PrintWriter log )
118                                                     throws StatusException {
119 
120         XInterface oObj = null;
121         Object oInt = null ;
122         final String impValue = "XMLImporter_test" ;
123 
124         // creation of testobject here
125         // first we write what we are intend to do to log file
126         log.println( "creating a test environment" );
127 
128         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
129 
130         try {
131             oInt = xMSF.createInstance("com.sun.star.comp.Calc.XMLImporter") ;
132             XImporter imp = (XImporter) UnoRuntime.queryInterface
133                 (XImporter.class, oInt) ;
134             imp.setTargetDocument(xSheetDoc);
135         } catch (com.sun.star.uno.Exception e) {
136             e.printStackTrace(log) ;
137             throw new StatusException("Can't create component.", e) ;
138         }
139 
140         oObj = (XInterface) oInt ;
141 
142         // create testobject here
143         log.println( "creating a new environment for Paragraph object" );
144         TestEnvironment tEnv = new TestEnvironment( oObj );
145 
146         // adding relation
147         tEnv.addObjRelation("TargetDocument", xSheetDoc) ;
148 
149         // adding relation for XDocumentHandler
150         String[][] xml = new String[][] {
151             {"start", "office:document",
152                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
153                 "xmlns:text", "CDATA", "http://openoffice.org/2000/text",
154                 "xmlns:table", "CDATA", "http://openoffice.org/2000/table",
155                 "office:class", "CDATA", "spreadsheet"
156                 },
157             {"start", "office:body"},
158             {"start", "table:table",
159                 "table:name", "CDATA", impValue},
160             {"end", "table:table"},
161             {"end", "office:body"},
162             {"end", "office:document"}} ;
163 
164         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
165 
166         final PrintWriter fLog = log ;
167         XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
168             UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
169         final XSpreadsheets sheets = xSpreadsheetDoc.getSheets() ;
170         log.println("Sheets before importing :") ;
171         String[] names = sheets.getElementNames() ;
172         for (int i = 0; i < names.length; i++) {
173             log.println("  " + names[i]) ;
174         }
175         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
176             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
177                 public boolean checkImport() {
178                     fLog.println("Sheet names :") ;
179                     String[] snames = sheets.getElementNames() ;
180                     for (int i = 0; i < snames.length; i++) {
181                         fLog.println("  " + snames[i]) ;
182                     }
183 
184                     return sheets.hasByName(impValue) ;
185                 }
186             }) ;
187 
188         return tEnv;
189     } // finish method getTestEnvironment
190 }
191 
192