Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / XMLImporter.java
blob6f54e1a21dfa63d68029dfc73e56fbb128d7e39f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.document.XImporter;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sheet.XSpreadsheetDocument;
33 import com.sun.star.sheet.XSpreadsheets;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XInterface;
36 /**
37 * Test for object which is represented by service
38 * <code>com.sun.star.comp.Calc.XMLImporter</code>. <p>
39 * Object implements the following interfaces :
40 * <ul>
41 * <li><code>com::sun::star::lang::XInitialization</code></li>
42 * <li><code>com::sun::star::document::XImporter</code></li>
43 * <li><code>com::sun::star::document::XFilter</code></li>
44 * <li><code>com::sun::star::document::ImportFilter</code></li>
45 * <li><code>com::sun::star::beans::XPropertySet</code></li>
46 * <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
48 * </ul>
49 * @see com.sun.star.lang.XInitialization
50 * @see com.sun.star.document.XImporter
51 * @see com.sun.star.document.XFilter
52 * @see com.sun.star.document.ImportFilter
53 * @see com.sun.star.beans.XPropertySet
54 * @see com.sun.star.xml.sax.XDocumentHandler
55 * @see ifc.lang._XInitialization
56 * @see ifc.document._XImporter
57 * @see ifc.document._XFilter
58 * @see ifc.document._XExporter
59 * @see ifc.beans._XPropertySet
60 * @see ifc.xml.sax._XDocumentHandler
62 public class XMLImporter extends TestCase {
64 static XComponent xSheetDoc;
66 /**
67 * New spreadsheet document created.
69 @Override
70 protected void initialize( TestParameters tParam, PrintWriter log ) {
71 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
73 try {
74 log.println( "creating a Spreadsheet document" );
75 XSpreadsheetDocument xSpreadsheetDoc = SOF.createCalcDoc( null );
76 xSheetDoc = UnoRuntime.queryInterface
77 (XComponent.class, xSpreadsheetDoc);
78 } catch ( com.sun.star.uno.Exception e ) {
79 // Some exception occurs.FAILED
80 e.printStackTrace( log );
81 throw new StatusException( "Couldn't create document", e );
85 /**
86 * Spreadsheet document destroyed.
88 @Override
89 protected void cleanup( TestParameters tParam, PrintWriter log ) {
90 log.println( " disposing document " );
91 util.DesktopTools.closeDoc(xSheetDoc);
94 /**
95 * Creating a Testenvironment for the interfaces to be tested.
96 * Creates an instance of the service
97 * <code>com.sun.star.comp.Calc.XMLImporter</code><p>
99 * The calc document is set as a target document for importer.
100 * Imported tags contain new value for table name.
101 * After import table name getting from
102 * target document is checked.
103 * Object relations created :
104 * <ul>
105 * <li> <code>'XDocumentHandler.XMLData'</code> for
106 * {@link ifc.xml.sax._XDocumentHandler} interface </li>
107 * <li> <code>'XDocumentHandler.ImportChecker'</code> for
108 * {@link ifc.xml.sax._XDocumentHandler} interface </li>
109 * <li> <code>'TargetDocument'</code> for
110 * {@link ifc.document._XImporter} interface </li>
111 * </ul>
113 @Override
114 public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
115 PrintWriter log )
116 throws StatusException {
118 XInterface oObj = null;
119 Object oInt = null ;
120 final String impValue = "XMLImporter_test" ;
122 // creation of testobject here
123 // first we write what we are intend to do to log file
124 log.println( "creating a test environment" );
126 XMultiServiceFactory xMSF = tParam.getMSF() ;
128 try {
129 oInt = xMSF.createInstance("com.sun.star.comp.Calc.XMLImporter") ;
130 XImporter imp = UnoRuntime.queryInterface
131 (XImporter.class, oInt) ;
132 imp.setTargetDocument(xSheetDoc);
133 } catch (com.sun.star.uno.Exception e) {
134 e.printStackTrace(log) ;
135 throw new StatusException("Can't create component.", e) ;
138 oObj = (XInterface) oInt ;
140 // create testobject here
141 log.println( "creating a new environment for Paragraph object" );
142 TestEnvironment tEnv = new TestEnvironment( oObj );
144 // adding relation
145 tEnv.addObjRelation("TargetDocument", xSheetDoc) ;
147 // adding relation for XDocumentHandler
148 String[][] xml = new String[][] {
149 {"start", "office:document",
150 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
151 "xmlns:text", "CDATA", "http://openoffice.org/2000/text",
152 "xmlns:table", "CDATA", "http://openoffice.org/2000/table",
153 "office:class", "CDATA", "spreadsheet"
155 {"start", "office:body"},
156 {"start", "table:table",
157 "table:name", "CDATA", impValue},
158 {"end", "table:table"},
159 {"end", "office:body"},
160 {"end", "office:document"}} ;
162 tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
164 final PrintWriter fLog = log ;
165 XSpreadsheetDocument xSpreadsheetDoc = UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
166 final XSpreadsheets sheets = xSpreadsheetDoc.getSheets() ;
167 log.println("Sheets before importing :") ;
168 String[] names = sheets.getElementNames() ;
169 for (int i = 0; i < names.length; i++) {
170 log.println(" " + names[i]) ;
172 tEnv.addObjRelation("XDocumentHandler.ImportChecker",
173 new ifc.xml.sax._XDocumentHandler.ImportChecker() {
174 public boolean checkImport() {
175 fLog.println("Sheet names :") ;
176 String[] snames = sheets.getElementNames() ;
177 for (int i = 0; i < snames.length; i++) {
178 fLog.println(" " + snames[i]) ;
181 return sheets.hasByName(impValue) ;
183 }) ;
185 return tEnv;
186 } // finish method getTestEnvironment