bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / XMLContentImporter.java
blob0716d95c4ca404df2aa989d48119f4311a219689
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;
37 /**
38 * Test for object which is represented by service
39 * <code>com.sun.star.comp.Calc.XMLContentImporter</code>. <p>
40 * Object implements the following interfaces :
41 * <ul>
42 * <li><code>com::sun::star::lang::XInitialization</code></li>
43 * <li><code>com::sun::star::document::XImporter</code></li>
44 * <li><code>com::sun::star::document::XFilter</code></li>
45 * <li><code>com::sun::star::document::ImportFilter</code></li>
46 * <li><code>com::sun::star::beans::XPropertySet</code></li>
47 * <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
49 * </ul>
50 * @see com.sun.star.lang.XInitialization
51 * @see com.sun.star.document.XImporter
52 * @see com.sun.star.document.XFilter
53 * @see com.sun.star.document.ImportFilter
54 * @see com.sun.star.beans.XPropertySet
55 * @see com.sun.star.xml.sax.XDocumentHandler
56 * @see ifc.lang._XInitialization
57 * @see ifc.document._XImporter
58 * @see ifc.document._XFilter
59 * @see ifc.document._XExporter
60 * @see ifc.beans._XPropertySet
61 * @see ifc.xml.sax._XDocumentHandler
63 public class XMLContentImporter extends TestCase {
64 private XSpreadsheetDocument xSheetDoc;
65 static XComponent comp ;
67 /**
68 * New spreadsheet document created.
70 protected void initialize( TestParameters tParam, PrintWriter log ) {
71 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
73 try {
74 log.println( "creating a Spreadsheet document" );
75 xSheetDoc = SOF.createCalcDoc( null );
76 comp = UnoRuntime.queryInterface
77 (XComponent.class, xSheetDoc) ;
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 protected void cleanup( TestParameters tParam, PrintWriter log ) {
89 log.println( " disposing document " );
90 util.DesktopTools.closeDoc(comp);
93 /**
94 * Creating a Testenvironment for the interfaces to be tested.
95 * Creates an instance of the service
96 * <code>com.sun.star.comp.Calc.XMLContentImporter</code><p>
98 * The calc document is set as a target document for importer.
99 * Imported XML-data contains only content tags including table
100 * with test 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 public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
114 PrintWriter log )
115 throws StatusException {
117 XInterface oObj = null;
118 Object oInt = null ;
120 // creation of testobject here
121 // first we write what we are intend to do to log file
122 log.println( "creating a test environment" );
124 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
126 try {
127 oInt = xMSF.createInstance
128 ("com.sun.star.comp.Calc.XMLContentImporter") ;
129 XImporter imp = UnoRuntime.queryInterface
130 (XImporter.class, oInt) ;
131 imp.setTargetDocument(comp);
132 } catch (com.sun.star.uno.Exception e) {
133 e.printStackTrace(log) ;
134 throw new StatusException("Can't create component.", e) ;
137 oObj = (XInterface) oInt ;
139 // create testobject here
140 log.println( "creating a new environment for Paragraph object" );
141 TestEnvironment tEnv = new TestEnvironment( oObj );
143 // adding relations
144 tEnv.addObjRelation("TargetDocument", comp) ;
146 final String impValue = "XMLContentImporter_test" ;
147 // adding relation for XDocumentHandler
148 String[][] xml = new String[][] {
149 {"start", "office:document-content",
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-content"}} ;
162 tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
164 //save to log content before import
165 final PrintWriter fLog = log;
166 final XSpreadsheets xSheets = xSheetDoc.getSheets();
168 log.println("Sheets before importing :") ;
169 String[] names = xSheets.getElementNames() ;
170 for (int i = 0; i < names.length; i++) {
171 log.println(" " + names[i]) ;
174 tEnv.addObjRelation("XDocumentHandler.ImportChecker",
175 new ifc.xml.sax._XDocumentHandler.ImportChecker() {
176 public boolean checkImport() {
177 fLog.println("Sheet names :") ;
178 String[] snames = xSheets.getElementNames() ;
179 for (int i = 0; i < snames.length; i++) {
180 fLog.println(" " + snames[i]) ;
182 return xSheets.hasByName(impValue) ;
184 }) ;
186 return tEnv;
187 } // finish method getTestEnvironment