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._sw;
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.XMultiServiceFactory;
37 import com.sun.star.text.XTextDocument;
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.Writer.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 
53  * </ul>
54  * @see com.sun.star.lang.XInitialization
55  * @see com.sun.star.document.XImporter
56  * @see com.sun.star.document.XFilter
57  * @see com.sun.star.document.ImportFilter
58  * @see com.sun.star.beans.XPropertySet
59  * @see com.sun.star.xml.sax.XDocumentHandler
60  * @see ifc.lang._XInitialization
61  * @see ifc.document._XImporter
62  * @see ifc.document._XFilter
63  * @see ifc.document._XExporter
64  * @see ifc.beans._XPropertySet
65  * @see ifc.xml.sax._XDocumentHandler
66  */
67 public class XMLMetaImporter extends TestCase {
68     XTextDocument xTextDoc;
69 
70     /**
71     * New text document created.
72     */
initialize( TestParameters tParam, PrintWriter log )73     protected void initialize( TestParameters tParam, PrintWriter log ) {
74         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
75 
76         try {
77             log.println( "creating a textdocument" );
78             xTextDoc = SOF.createTextDoc( null );
79         } catch ( com.sun.star.uno.Exception e ) {
80             // Some exception occures.FAILED
81             e.printStackTrace( log );
82             throw new StatusException( "Couldn't create document", e );
83         }
84     }
85 
86     /**
87     * Text document destroyed.
88     */
cleanup( TestParameters tParam, PrintWriter log )89     protected void cleanup( TestParameters tParam, PrintWriter log ) {
90         log.println( "    disposing xTextDoc " );
91         util.DesktopTools.closeDoc(xTextDoc);
92     }
93 
94     /**
95     * Creating a Testenvironment for the interfaces to be tested.
96     * Creates an instance of the service
97     * <code>com.sun.star.comp.Writer.XMLMetaImporter</code><p>
98     *
99     * The text document is set as a target document for importer.
100     * Imported XML-data contains only meta tags including
101     * title tag with test name.
102     * After import title name getting from target document is checked.
103     *     Object relations created :
104     * <ul>
105     *  <li> <code>'XDocumentHandler.XMLData'</code> for
106     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
107     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
108     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
109     *  <li> <code>'TargetDocument'</code> for
110     *      {@link ifc.document._XImporter} interface </li>
111     * </ul>
112     */
createTestEnvironment( TestParameters tParam, PrintWriter log )113     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
114                                                   PrintWriter log )
115                                                     throws StatusException {
116 
117         XInterface oObj = null;
118         Object oInt = null ;
119         final String impTitle = "XMLMetaImporter 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.Writer.XMLMetaImporter") ;
130             //XImporter imp = (XImporter) UnoRuntime.queryInterface
131             //    (XImporter.class, oInt) ;
132             //imp.setTargetDocument(xTextDoc) ;
133         } catch (com.sun.star.uno.Exception e) {
134             e.printStackTrace(log) ;
135             throw new StatusException("Can't create component.", e) ;
136         }
137 
138         oObj = (XInterface) oInt ;
139 
140         // create testobject here
141         log.println( "creating a new environment for Paragraph object" );
142         TestEnvironment tEnv = new TestEnvironment( oObj );
143 
144         // adding relation
145         tEnv.addObjRelation("TargetDocument", xTextDoc) ;
146 
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:dc", "CDATA", "http://purl.org/dc/elements/1.1/"},
152             {"start", "office:meta"} ,
153             {"start", "dc:title"},
154             {"chars", impTitle},
155             {"end", "dc:title"},
156             {"end", "office:meta"},
157             {"end", "office:document-meta"}} ;
158 
159         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
160 
161         XDocumentInfoSupplier infoSup = (XDocumentInfoSupplier) UnoRuntime.queryInterface
162             (XDocumentInfoSupplier.class, xTextDoc) ;
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("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