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._sm;
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.XDocumentInfo;
36 import com.sun.star.document.XDocumentInfoSupplier;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 
42 /**
43  * Test for object which is represented by service
44  * <code>com.sun.star.comp.Math.XMLMetaImporter</code>. <p>
45  * Object implements the following interfaces :
46  * <ul>
47  *  <li><code>com::sun::star::lang::XInitialization</code></li>
48  *  <li><code>com::sun::star::document::XImporter</code></li>
49  *  <li><code>com::sun::star::document::XFilter</code></li>
50  *  <li><code>com::sun::star::document::ImportFilter</code></li>
51  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
52  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
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     XComponent xMathDoc;
69 
70     /**
71     * New math document created.
72     */
initialize( TestParameters tParam, PrintWriter log )73     protected void initialize( TestParameters tParam, PrintWriter log ) {
74 
75         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
76         try {
77             xMathDoc = SOF.openDoc("smath","_blank");
78         } catch (com.sun.star.uno.Exception ex) {
79             ex.printStackTrace( log );
80             throw new StatusException( "Couldn't create document", ex );
81         }
82     }
83 
84     /**
85     * Disposes document.
86     */
cleanup( TestParameters tParam, PrintWriter log )87     protected void cleanup( TestParameters tParam, PrintWriter log ) {
88         log.println( "    disposing xMathDoc " );
89         xMathDoc.dispose();
90     }
91 
92     /**
93     * Creating a Testenvironment for the interfaces to be tested.
94     * Creates an instance of the service
95     * <code>com.sun.star.comp.Math.XMLMetaImporter</code><p>
96     *
97     * The math document is set as a target document for importer.
98     * Imported XML-data contains the tag which specifies new user info
99     * field and a title of document.
100     * After import user fields info and the title of target document
101     * is checked.
102     *     Object relations created :
103     * <ul>
104     *  <li> <code>'XDocumentHandler.XMLData'</code> for
105     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
106     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
107     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
108     *  <li> <code>'TargetDocument'</code> for
109     *      {@link ifc.document._XImporter} interface </li>
110     * </ul>
111     */
createTestEnvironment( TestParameters Param, PrintWriter log )112     public synchronized TestEnvironment createTestEnvironment
113             ( TestParameters Param, PrintWriter log )
114             throws StatusException {
115 
116         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
117         XInterface oObj = null;
118         final String impName = "XMLMetaImporter" ;
119         final String impValue = "XMLMetaImporter_Value" ;
120         final String impTitle = "XMLMetaImporter Title" ;
121 
122         final XDocumentInfoSupplier xDocInfoSup ;
123         try {
124             oObj = (XInterface)xMSF.createInstance(
125                     "com.sun.star.comp.Math.XMLMetaImporter");
126 
127             xDocInfoSup = (XDocumentInfoSupplier) UnoRuntime.queryInterface
128                 (XDocumentInfoSupplier.class, xMathDoc) ;
129         } catch (com.sun.star.uno.Exception e) {
130             e.printStackTrace(log);
131             throw new StatusException("Unexpected exception", e);
132         }
133 
134         TestEnvironment tEnv = new TestEnvironment(oObj);
135 
136         tEnv.addObjRelation("TargetDocument",xMathDoc);
137 
138         String[][] xml = new String[][] {
139             {"start", "office:document-meta",
140                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
141                 "xmlns:meta", "CDATA", "http://openoffice.org/2000/meta",
142                 "xmlns:xlink", "CDATA", "http://www.w3.org/1999/xlink",
143                 "xmlns:dc", "CDATA", "http://purl.org/dc/elements/1.1/"
144             },
145             {"start", "office:meta"},
146             {"start", "dc:title"},
147             {"chars", impTitle},
148             {"end", "dc:title"},
149             {"start", "meta:user-defined",
150                 "meta:name", "CDATA", impName},
151             {"chars", impValue},
152             {"end", "meta:user-defined"},
153             {"end", "office:meta"},
154             {"end", "office:document-meta"}} ;
155 
156         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
157 
158         final PrintWriter logF = log ;
159 
160         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
161             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
162                 public boolean checkImport() {
163                     try {
164                         XDocumentInfo xDocInfo = xDocInfoSup.getDocumentInfo() ;
165                         XPropertySet xDocInfoProp = (XPropertySet)
166                             UnoRuntime.queryInterface
167                             (XPropertySet.class, xDocInfo) ;
168                         boolean result = false ;
169                         for (short i = 0; i < xDocInfo.getUserFieldCount(); i++) {
170                             String gName = xDocInfo.getUserFieldName(i) ;
171                             String gValue = xDocInfo.getUserFieldValue(i) ;
172                             logF.println("Field '" + gName + "' = '"
173                                 + gValue + "'") ;
174                             if (impName.equals(gName) && impValue.equals(gValue))
175                                 result = true ;
176                         }
177                         String gTitle = (String) xDocInfoProp.getPropertyValue
178                             ("Title");
179                         logF.println("Title returned : '" + gTitle + "'");
180                         result &= impTitle.equals(gTitle) ;
181 
182                         return result ;
183                     } catch (com.sun.star.uno.Exception e) {
184                         logF.println("Exception occurred while checking filter :") ;
185                         e.printStackTrace(logF) ;
186                         return false ;
187                     }
188                 }
189             }) ;
190 
191         return tEnv;
192     }
193 }
194 
195