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