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 import util.XMLTools;
38 
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.document.XExporter;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.uno.Any;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 import com.sun.star.xml.sax.XDocumentHandler;
48 
49 /**
50  * Test for object which is represented by service
51  * <code>com.sun.star.comp.Math.XMLExporter</code>. <p>
52  * Object implements the following interfaces :
53  * <ul>
54  *  <li><code>com::sun::star::lang::XInitialization</code></li>
55  *  <li><code>com::sun::star::document::ExportFilter</code></li>
56  *  <li><code>com::sun::star::document::XFilter</code></li>
57  *  <li><code>com::sun::star::document::XExporter</code></li>
58  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
59  * </ul>
60  * @see com.sun.star.lang.XInitialization
61  * @see com.sun.star.document.ExportFilter
62  * @see com.sun.star.document.XFilter
63  * @see com.sun.star.document.XExporter
64  * @see com.sun.star.beans.XPropertySet
65  * @see ifc.lang._XInitialization
66  * @see ifc.document._ExportFilter
67  * @see ifc.document._XFilter
68  * @see ifc.document._XExporter
69  * @see ifc.beans._XPropertySet
70  */
71 public class XMLExporter extends TestCase {
72     XComponent xMathDoc;
73 
74     /**
75      * New math document created.
76      */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79 
80         try {
81             log.println( "creating a math document" );
82             xMathDoc = SOF.createMathDoc(null);
83         } catch ( com.sun.star.uno.Exception e ) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91      * Document disposed here.
92      */
93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xMathDoc " );
95         xMathDoc.dispose();
96     }
97 
98     /**
99     * Creating a Testenvironment for the interfaces to be tested.
100     * Creates an instance of the service
101     * <code>com.sun.star.comp.Math.XMLExporter</code> with
102     * argument which is an implementation of <code>XDocumentHandler</code>
103     * and which can check if required tags and character data is
104     * exported. <p>
105     * The math document is set as a source document for exporter
106     * created. A new formula inserted into document. This made
107     * for checking if this formula is successfully exported within
108     * the document content.
109     *     Object relations created :
110     * <ul>
111     *  <li> <code>'MediaDescriptor'</code> for
112     *      {@link ifc.document._XFilter} interface </li>
113     *  <li> <code>'XFilter.Checker'</code> for
114     *      {@link ifc.document._XFilter} interface </li>
115     *  <li> <code>'SourceDocument'</code> for
116     *      {@link ifc.document._XExporter} interface </li>
117     * </ul>
118     */
119     protected TestEnvironment createTestEnvironment
120             (TestParameters tParam, PrintWriter log) {
121 
122         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
123         XInterface oObj = null;
124         final String expFormula = "a - b" ;
125 
126         FilterChecker filter = new FilterChecker(log);
127         Any arg = new Any(new Type(XDocumentHandler.class), filter);
128 
129         try {
130             oObj = (XInterface) xMSF.createInstanceWithArguments(
131                 "com.sun.star.comp.Math.XMLExporter", new Object[] {arg});
132             XExporter xEx = (XExporter) UnoRuntime.queryInterface
133                 (XExporter.class,oObj);
134             xEx.setSourceDocument(xMathDoc);
135 
136             // setting a formula in document
137             XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface
138                 (XPropertySet.class, xMathDoc) ;
139             xPS.setPropertyValue("Formula", expFormula) ;
140         } catch (com.sun.star.uno.Exception e) {
141             e.printStackTrace(log) ;
142             throw new StatusException("Can't create component.", e) ;
143         }
144 
145         // checking tags required
146         filter.addTag(new XMLTools.Tag("math:math")) ;
147         filter.addTagEnclosed(new XMLTools.Tag("math:annotation"),
148             new XMLTools.Tag("math:semantics")) ;
149         filter.addCharactersEnclosed(expFormula,
150             new XMLTools.Tag("math:annotation")) ;
151 
152         // create testobject here
153         log.println( "creating a new environment" );
154         TestEnvironment tEnv = new TestEnvironment( oObj );
155 
156         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
157             new String[] {"FilterName"},
158             new Object[] {"smath: StarOffice XML (Math)"}));
159         tEnv.addObjRelation("SourceDocument",xMathDoc);
160         tEnv.addObjRelation("XFilter.Checker", filter) ;
161 
162         log.println("Implementation Name: "+util.utils.getImplName(oObj));
163 
164         return tEnv;
165     }
166 
167     /**
168      * This class checks the XML for tags and data required and returns
169      * checking result to <code>XFilter</code> interface test. All
170      * the information about errors occured in XML data is written
171      * to log specified.
172      * @see ifc.document._XFilter
173      */
174     protected class FilterChecker extends XMLTools.XMLChecker
175         implements ifc.document._XFilter.FilterChecker {
176 
177         /**
178          * Creates a class which will write information
179          * into log specified.
180          */
181         public FilterChecker(PrintWriter log) {
182             super(log, true) ;
183         }
184         /**
185          * <code>_XFilter.FilterChecker</code> interface method
186          * which returns the result of XML checking.
187          * @return <code>true</code> if the XML data exported was
188          * valid (i.e. all necessary tags and character data exists),
189          * <code>false</code> if some errors occured.
190          */
191         public boolean checkFilter() {
192             return check();
193         }
194     }
195 
196 }    // finish class TestCase
197 
198