Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sm / XMLMetaImporter.java
blob9ba5608aba43520375b5437e4180b4de03d0d55b
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sm;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.beans.XPropertySetInfo;
31 import com.sun.star.beans.Property;
32 import com.sun.star.document.XDocumentPropertiesSupplier;
33 import com.sun.star.document.XDocumentProperties;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
39 /**
40 * Test for object which is represented by service
41 * <code>com.sun.star.comp.Math.XMLMetaImporter</code>. <p>
42 * Object implements the following interfaces :
43 * <ul>
44 * <li><code>com::sun::star::lang::XInitialization</code></li>
45 * <li><code>com::sun::star::document::XImporter</code></li>
46 * <li><code>com::sun::star::document::XFilter</code></li>
47 * <li><code>com::sun::star::document::ImportFilter</code></li>
48 * <li><code>com::sun::star::beans::XPropertySet</code></li>
49 * <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
50 * </ul>
51 * @see com.sun.star.lang.XInitialization
52 * @see com.sun.star.document.XImporter
53 * @see com.sun.star.document.XFilter
54 * @see com.sun.star.document.ImportFilter
55 * @see com.sun.star.beans.XPropertySet
56 * @see com.sun.star.xml.sax.XDocumentHandler
57 * @see ifc.lang._XInitialization
58 * @see ifc.document._XImporter
59 * @see ifc.document._XFilter
60 * @see ifc.document._XExporter
61 * @see ifc.beans._XPropertySet
62 * @see ifc.xml.sax._XDocumentHandler
64 public class XMLMetaImporter extends TestCase {
65 XComponent xMathDoc;
67 /**
68 * New math document created.
70 @Override
71 protected void initialize( TestParameters tParam, PrintWriter log ) {
73 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
74 try {
75 xMathDoc = SOF.openDoc("smath","_blank");
76 } catch (com.sun.star.uno.Exception ex) {
77 ex.printStackTrace( log );
78 throw new StatusException( "Couldn't create document", ex );
82 /**
83 * Disposes document.
85 @Override
86 protected void cleanup( TestParameters tParam, PrintWriter log ) {
87 log.println( " disposing xMathDoc " );
88 xMathDoc.dispose();
91 /**
92 * Creating a Testenvironment for the interfaces to be tested.
93 * Creates an instance of the service
94 * <code>com.sun.star.comp.Math.XMLMetaImporter</code><p>
96 * The math document is set as a target document for importer.
97 * Imported XML-data contains the tag which specifies new user info
98 * field and a title of document.
99 * After import user fields info and the title of target document
100 * is checked.
101 * Object relations created :
102 * <ul>
103 * <li> <code>'XDocumentHandler.XMLData'</code> for
104 * {@link ifc.xml.sax._XDocumentHandler} interface </li>
105 * <li> <code>'XDocumentHandler.ImportChecker'</code> for
106 * {@link ifc.xml.sax._XDocumentHandler} interface </li>
107 * <li> <code>'TargetDocument'</code> for
108 * {@link ifc.document._XImporter} interface </li>
109 * </ul>
111 @Override
112 public synchronized TestEnvironment createTestEnvironment
113 ( TestParameters Param, PrintWriter log )
114 throws StatusException {
116 XMultiServiceFactory xMSF = Param.getMSF();
117 XInterface oObj = null;
118 final String impName = "XMLMetaImporter" ;
119 final String impValue = "XMLMetaImporter_Value" ;
120 final String impTitle = "XMLMetaImporter Title" ;
122 try {
123 oObj = (XInterface)xMSF.createInstance(
124 "com.sun.star.comp.Math.XMLMetaImporter");
125 } catch (com.sun.star.uno.Exception e) {
126 e.printStackTrace(log);
127 throw new StatusException("Unexpected exception", e);
130 TestEnvironment tEnv = new TestEnvironment(oObj);
132 tEnv.addObjRelation("TargetDocument",xMathDoc);
134 String[][] xml = new String[][] {
135 {"start", "office:document-meta",
136 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
137 "xmlns:meta", "CDATA", "http://openoffice.org/2000/meta",
138 "xmlns:xlink", "CDATA", "http://www.w3.org/1999/xlink",
139 "xmlns:dc", "CDATA", "http://purl.org/dc/elements/1.1/"
141 {"start", "office:meta"},
142 {"start", "dc:title"},
143 {"chars", impTitle},
144 {"end", "dc:title"},
145 {"start", "meta:user-defined",
146 "meta:name", "CDATA", impName},
147 {"chars", impValue},
148 {"end", "meta:user-defined"},
149 {"end", "office:meta"},
150 {"end", "office:document-meta"}} ;
152 tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
154 final PrintWriter logF = log ;
156 tEnv.addObjRelation("XDocumentHandler.ImportChecker",
157 new ifc.xml.sax._XDocumentHandler.ImportChecker() {
158 public boolean checkImport() {
159 try {
160 XDocumentPropertiesSupplier xPropSup =
161 UnoRuntime.queryInterface
162 (XDocumentPropertiesSupplier.class, xMathDoc);
163 final XDocumentProperties xDocProps =
164 xPropSup.getDocumentProperties();
166 XPropertySet xUDProps = UnoRuntime.queryInterface(
167 XPropertySet.class,
168 xDocProps.getUserDefinedProperties());
169 XPropertySetInfo xInfo =
170 xUDProps.getPropertySetInfo();
171 Property[] props = xInfo.getProperties();
172 boolean result = false;
173 for (int i = 0; i < props.length; i++) {
174 String gName = props[i].Name;
175 String gValue = (String)
176 xUDProps.getPropertyValue(gName);
177 logF.println("Field '" + gName + "' = '"
178 + gValue + "'") ;
179 if (impName.equals(gName) && impValue.equals(gValue))
180 result = true ;
182 String gTitle = xDocProps.getTitle();
183 logF.println("Title returned : '" + gTitle + "'");
184 result &= impTitle.equals(gTitle) ;
186 return result ;
187 } catch (com.sun.star.uno.Exception e) {
188 logF.println("Exception occurred while checking filter :") ;
189 e.printStackTrace(logF) ;
190 return false ;
193 }) ;
195 return tEnv;