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