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