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._xmloff.Impress;
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.beans.XPropertySet;
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.Exception;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 import com.sun.star.xml.sax.XDocumentHandler;
46 
47 /**
48  * Test for object which is represented by service
49  * <code>com.sun.star.comp.Impress.XMLMetaExporter</code>. <p>
50  * Object implements the following interfaces :
51  * <ul>
52  *  <li><code>com::sun::star::lang::XInitialization</code></li>
53  *  <li><code>com::sun::star::document::ExportFilter</code></li>
54  *  <li><code>com::sun::star::document::XFilter</code></li>
55  *  <li><code>com::sun::star::document::XExporter</code></li>
56  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
57  * </ul>
58  * @see com.sun.star.lang.XInitialization
59  * @see com.sun.star.document.ExportFilter
60  * @see com.sun.star.document.XFilter
61  * @see com.sun.star.document.XExporter
62  * @see com.sun.star.beans.XPropertySet
63  * @see ifc.lang._XInitialization
64  * @see ifc.document._ExportFilter
65  * @see ifc.document._XFilter
66  * @see ifc.document._XExporter
67  * @see ifc.beans._XPropertySet
68  */
69 public class XMLMetaExporter extends TestCase {
70     XComponent xImpressDoc = null;
71 
72     /**
73     * New impress document created.
74     */
initialize( TestParameters tParam, PrintWriter log )75     protected void initialize( TestParameters tParam, PrintWriter log ) {
76 
77         // get a soffice factory object
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
79 
80         try {
81             log.println( "creating an impress document" );
82             xImpressDoc = SOF.createImpressDoc(null);
83         } catch ( 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     * Impress document disposed
92     */
cleanup( TestParameters tParam, PrintWriter log )93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "disposing xImpressDoc " );
95         xImpressDoc.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.Impress.XMLMetaExporter</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 impress document is set as a source document for exporter
106     * created. A meta property 'Title' is set to specific value. This made
107     * for checking if this value is successfully exported within
108     * the document meta information.
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     */
createTestEnvironment(TestParameters tParam, PrintWriter log)119     public synchronized TestEnvironment createTestEnvironment
120         (TestParameters tParam, PrintWriter log) throws StatusException {
121 
122         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
123         XInterface oObj = null;
124 
125         FilterChecker filter = new FilterChecker(log);
126         Any arg = new Any(new Type(XDocumentHandler.class), filter);
127         final String NAME = "XMLMetaExporter";
128 
129         try {
130             oObj = (XInterface) xMSF.createInstanceWithArguments(
131                 "com.sun.star.comp.Impress.XMLMetaExporter",
132                 new Object[]{arg});
133             XExporter xEx = (XExporter)
134                 UnoRuntime.queryInterface(XExporter.class,oObj);
135             xEx.setSourceDocument(xImpressDoc);
136 
137         //change title name
138             XDocumentInfoSupplier infoSup = (XDocumentInfoSupplier)
139                 UnoRuntime.queryInterface
140                 (XDocumentInfoSupplier.class, xImpressDoc) ;
141             XPropertySet docInfo = (XPropertySet) UnoRuntime.queryInterface
142                 (XPropertySet.class, infoSup.getDocumentInfo()) ;
143             docInfo.setPropertyValue("Title", NAME);
144 
145         } catch (com.sun.star.uno.Exception e) {
146             e.printStackTrace(log) ;
147             throw new StatusException("Can't create component.", e) ;
148         }
149 
150         // Checking tags existance and changed property value
151         filter.addTag(new XMLTools.Tag ("office:document-meta"));
152         filter.addTagEnclosed(
153             new XMLTools.Tag("office:meta"),
154             new XMLTools.Tag("office:document-meta") );
155         filter.addTagEnclosed(
156             new XMLTools.Tag("dc:title"),
157             new XMLTools.Tag("office:meta") );
158         filter.addCharactersEnclosed(
159             NAME,
160             new XMLTools.Tag ("dc:title") );
161 
162         // create testobject here
163         log.println( "creating a new environment" );
164         TestEnvironment tEnv = new TestEnvironment( oObj );
165 
166         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
167             new String[] {"FilterName"},
168             new Object[] {"simpress: StarOffice XML (Impress)"}));
169         tEnv.addObjRelation("SourceDocument", xImpressDoc);
170         tEnv.addObjRelation("XFilter.Checker", filter) ;
171         return tEnv;
172     }
173 
174     /**
175     * This class checks the XML for tags and data required and returns
176     * checking result to <code>XFilter</code> interface test. All
177     * the information about errors occured in XML data is written
178     * to log specified.
179     * @see ifc.document._XFilter
180     */
181     protected class FilterChecker extends XMLTools.XMLChecker
182             implements ifc.document._XFilter.FilterChecker {
183 
184         /**
185         * Creates a class which will write information
186         * into log specified.
187         */
FilterChecker(PrintWriter log)188         public FilterChecker(PrintWriter log) {
189             super(log, true);
190         }
191         /**
192         * <code>_XFilter.FilterChecker</code> interface method
193         * which returns the result of XML checking.
194         * @return <code>true</code> if the XML data exported was
195         * valid (i.e. all necessary tags and character data exists),
196         * <code>false</code> if some errors occured.
197         */
checkFilter()198         public boolean checkFilter() {
199             return check();
200         }
201     }
202 }
203