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