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