Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sm / XMLMetaExporter.java
blob7f360330fe90470180998419f5225c3eeb3ac1ac
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;
28 import util.XMLTools;
30 import com.sun.star.beans.XPropertyContainer;
31 import com.sun.star.document.XDocumentPropertiesSupplier;
32 import com.sun.star.document.XDocumentProperties;
33 import com.sun.star.document.XExporter;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.uno.Any;
37 import com.sun.star.uno.Type;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.xml.sax.XDocumentHandler;
42 /**
43 * Test for object which is represented by service
44 * <code>com.sun.star.comp.Math.XMLExporter</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::ExportFilter</code></li>
49 * <li><code>com::sun::star::document::XFilter</code></li>
50 * <li><code>com::sun::star::document::XExporter</code></li>
51 * <li><code>com::sun::star::beans::XPropertySet</code></li>
52 * </ul>
53 * @see com.sun.star.lang.XInitialization
54 * @see com.sun.star.document.ExportFilter
55 * @see com.sun.star.document.XFilter
56 * @see com.sun.star.document.XExporter
57 * @see com.sun.star.beans.XPropertySet
58 * @see ifc.lang._XInitialization
59 * @see ifc.document._ExportFilter
60 * @see ifc.document._XFilter
61 * @see ifc.document._XExporter
62 * @see ifc.beans._XPropertySet
64 public class XMLMetaExporter extends TestCase {
65 XComponent xMathDoc;
67 /**
68 * New math document created.
70 @Override
71 protected void initialize( TestParameters tParam, PrintWriter log ) {
72 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
74 try {
75 log.println( "creating a math document" );
76 xMathDoc = SOF.createMathDoc(null);
77 } catch ( com.sun.star.uno.Exception e ) {
78 // Some exception occurs.FAILED
79 e.printStackTrace( log );
80 throw new StatusException( "Couldn't create document", e );
84 /**
85 * Document disposed here.
87 @Override
88 protected void cleanup( TestParameters tParam, PrintWriter log ) {
89 log.println( " disposing xMathDoc " );
90 xMathDoc.dispose();
93 /**
94 * Creating a Testenvironment for the interfaces to be tested.
95 * Creates an instance of the service
96 * <code>com.sun.star.comp.Math.XMLExporter</code> with
97 * argument which is an implementation of <code>XDocumentHandler</code>
98 * and which can check if required tags and character data is
99 * exported. <p>
100 * The math document is set as a source document for exporter
101 * created. A new user info field inserted into document. This made
102 * for checking if this info field is successfully exported within
103 * the document content.
104 * Object relations created :
105 * <ul>
106 * <li> <code>'MediaDescriptor'</code> for
107 * {@link ifc.document._XFilter} interface </li>
108 * <li> <code>'XFilter.Checker'</code> for
109 * {@link ifc.document._XFilter} interface </li>
110 * <li> <code>'SourceDocument'</code> for
111 * {@link ifc.document._XExporter} interface </li>
112 * </ul>
114 @Override
115 protected TestEnvironment createTestEnvironment
116 (TestParameters tParam, PrintWriter log) {
117 XMultiServiceFactory xMSF = tParam.getMSF() ;
118 XInterface oObj = null;
119 final String expName = "XMLMetaExporterName" ;
120 final String expValue = "XMLMetaExporterValue" ;
122 FilterChecker filter = new FilterChecker(log);
123 Any arg = new Any(new Type(XDocumentHandler.class), filter);
125 try {
126 oObj = (XInterface) xMSF.createInstanceWithArguments(
127 "com.sun.star.comp.Math.XMLMetaExporter", new Object[] {arg});
128 XExporter xEx = UnoRuntime.queryInterface
129 (XExporter.class,oObj);
130 xEx.setSourceDocument(xMathDoc);
132 // setting a new name and value for user info field
133 XDocumentPropertiesSupplier xPropSup = UnoRuntime.queryInterface
134 (XDocumentPropertiesSupplier.class, xMathDoc);
135 final XDocumentProperties xDocProps = xPropSup.getDocumentProperties();
136 XPropertyContainer xProps = xDocProps.getUserDefinedProperties();
137 xProps.addProperty(expName, (short)0, expValue);
138 } catch (com.sun.star.uno.Exception e) {
139 e.printStackTrace(log) ;
140 throw new StatusException("Can't create component.", e) ;
143 // checking tags required
144 filter.addTag(new XMLTools.Tag("office:document-meta")) ;
145 filter.addCharactersEnclosed(expValue,
146 new XMLTools.Tag("meta:user-defined", "meta:name", expName)) ;
148 // create testobject here
149 log.println( "creating a new environment" );
150 TestEnvironment tEnv = new TestEnvironment( oObj );
153 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
154 new String[] {"FilterName"},
155 new Object[] {"smath: StarOffice XML (Math)"}));
156 tEnv.addObjRelation("SourceDocument",xMathDoc);
157 tEnv.addObjRelation("XFilter.Checker", filter) ;
159 log.println("Implementation Name: "+util.utils.getImplName(oObj));
161 return tEnv;
165 * This class checks the XML for tags and data required and returns
166 * checking result to <code>XFilter</code> interface test. All
167 * the information about errors occurred in XML data is written
168 * to log specified.
169 * @see ifc.document._XFilter
171 private class FilterChecker extends XMLTools.XMLChecker
172 implements ifc.document._XFilter.FilterChecker {
175 * Creates a class which will write information
176 * into log specified.
178 private FilterChecker(PrintWriter log) {
179 super(log, true) ;
182 * <code>_XFilter.FilterChecker</code> interface method
183 * which returns the result of XML checking.
184 * @return <code>true</code> if the XML data exported was
185 * valid (i.e. all necessary tags and character data exists),
186 * <code>false</code> if some errors occurred.
188 public boolean checkFilter() {
189 return check();
192 } // finish class TestCase