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