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.Draw;
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.Draw.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 xDrawDoc = null;
68 
69     /**
70     * New draw 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 a drawdocument" );
79             xDrawDoc = SOF.createDrawDoc(null);
80         } catch ( Exception e ) {
81             // Some exception occured.FAILED
82             e.printStackTrace( log );
83             throw new StatusException( "Couldn't create document", e );
84         }
85     }
86 
87     /**
88     * Draw document destroyed.
89     */
cleanup( TestParameters tParam, PrintWriter log )90     protected void cleanup( TestParameters tParam, PrintWriter log ) {
91         log.println( "    disposing document " );
92         xDrawDoc.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.Draw.XMLMetaImporter</code><p>
99     *
100     * The draw document is set as a target document for importer.
101     * Imported XML-data contains only meta tags including
102     * title tag with test name.
103     * After import title name getting from 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 ) {
116 
117         XInterface oObj = null;
118         Object oInt = null ;
119 
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" );
123 
124         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
125 
126         try {
127             oInt = xMSF.createInstance("com.sun.star.comp.Draw.XMLMetaImporter") ;
128         } catch (com.sun.star.uno.Exception e) {
129             e.printStackTrace(log) ;
130             throw new StatusException("Can't create component.", e) ;
131         }
132 
133         oObj = (XInterface) oInt ;
134 
135         // create testobject here
136         log.println( "creating a new environment for Paragraph object" );
137         TestEnvironment tEnv = new TestEnvironment( oObj );
138 
139         // adding relation
140         tEnv.addObjRelation("TargetDocument", xDrawDoc) ;
141 
142         final String impTitle = "XMLMetaImporter" ;
143         // adding relation for XDocumentHandler
144         String[][] xml = new String[][] {
145             {"start", "office:document-meta",
146                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
147                 "xmlns:meta", "CDATA", "http://openoffice.org/2000/meta",
148                 "xmlns:dc", "CDATA", "http://purl.org/dc/elements/1.1/"
149                 },
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         //set some meta data
160         XDocumentInfoSupplier infoSup = (XDocumentInfoSupplier)
161             UnoRuntime.queryInterface
162             (XDocumentInfoSupplier.class, xDrawDoc);
163         final XPropertySet docInfo = (XPropertySet) UnoRuntime.queryInterface
164                         (XPropertySet.class, infoSup.getDocumentInfo());
165         final PrintWriter logF = log ;
166 
167         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
168             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
169                 public boolean checkImport() {
170                     try {
171                         String title = (String) docInfo.getPropertyValue
172                             ("Title") ;
173                         logF.println("Title returned = '" + title + "'") ;
174                         return impTitle.equals(title) ;
175                     } catch (com.sun.star.uno.Exception e) {
176                         logF.println("Exception occurred while checking filter :") ;
177                         e.printStackTrace(logF) ;
178                         return false ;
179                     }
180                 }
181             }) ;
182 
183         return tEnv;
184     } // finish method getTestEnvironment
185 }
186