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