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