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