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._sw;
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.XMultiServiceFactory;
39 import com.sun.star.text.XTextDocument;
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.Calc.XMLMetaExporter</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 
70     XTextDocument xTextDoc;
71     MetaFilterChecker Filter;
72 
73     /**
74      * New text document created.
75      */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78         try {
79             log.println( "creating a textdocument" );
80             xTextDoc = SOF.createTextDoc( null );
81 
82         } catch ( com.sun.star.uno.Exception e ) {
83         // Some exception occures.FAILED
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document", e );
86         }
87     }
88 
89     /**
90      * Document disposed here.
91      */
cleanup( TestParameters tParam, PrintWriter log )92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93       log.println( "    disposing xTextDoc " );
94       util.DesktopTools.closeDoc(xTextDoc);
95     }
96 
97     /**
98     * Creating a Testenvironment for the interfaces to be tested.
99     * Creates an instance of the service
100     * <code>com.sun.star.comp.Calc.XMLMetaExporter</code> with
101     * argument which is an implementation of <code>XDocumentHandler</code>
102     * and which can check if required tags and character data is
103     * exported. <p>
104     * The text document is set as a source document for exporter
105     * created. The 'Title' metadata property is set to a value. This made
106     * for checking if this property is successfully exported within
107     * the document metadata.
108     *     Object relations created :
109     * <ul>
110     *  <li> <code>'MediaDescriptor'</code> for
111     *      {@link ifc.document._XFilter} interface </li>
112     *  <li> <code>'XFilter.Checker'</code> for
113     *      {@link ifc.document._XFilter} interface </li>
114     *  <li> <code>'SourceDocument'</code> for
115     *      {@link ifc.document._XExporter} interface </li>
116     * </ul>
117     */
createTestEnvironment( TestParameters tParam, PrintWriter log )118     public synchronized TestEnvironment createTestEnvironment
119             ( TestParameters tParam, PrintWriter log ) throws StatusException {
120         final String TITLE = "Title for testing of XMLMetaExporter";
121 
122         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
123         XInterface oObj = null;
124 
125         Filter = new MetaFilterChecker(log);
126         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
127 
128         try {
129             oObj = (XInterface) xMSF.createInstanceWithArguments(
130                 "com.sun.star.comp.Writer.XMLMetaExporter",
131                 new Object[] {arg});
132             XExporter xEx = (XExporter) UnoRuntime.queryInterface
133                 (XExporter.class,oObj);
134             xEx.setSourceDocument(xTextDoc);
135 
136             //set some meta data
137             XDocumentInfoSupplier infoSup = (XDocumentInfoSupplier)
138                 UnoRuntime.queryInterface
139                 (XDocumentInfoSupplier.class, xTextDoc) ;
140             XPropertySet docInfo = (XPropertySet) UnoRuntime.queryInterface
141                 (XPropertySet.class, infoSup.getDocumentInfo()) ;
142             docInfo.setPropertyValue("Title", TITLE);
143 
144         } catch (com.sun.star.uno.Exception e) {
145             e.printStackTrace(log) ;
146             throw new StatusException("Can't create component.", e) ;
147         }
148 
149         // adding tags which must be contained in XML output
150         Filter.addTag("office:document-meta");
151         Filter.addTagEnclosed("office:meta", "office:document-meta");
152         Filter.addCharactersEnclosed(TITLE, "dc:title");
153 
154         // create testobject here
155         log.println( "creating a new environment" );
156         TestEnvironment tEnv = new TestEnvironment( oObj );
157 
158         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
159             new String[] {"FilterName"},
160             new Object[] {"swriter: StarOffice XML (Writer)"}));
161         tEnv.addObjRelation("SourceDocument", xTextDoc);
162         tEnv.addObjRelation("XFilter.Checker", Filter);
163         return tEnv;
164     }
165 
166     /**
167      * This class checks the XML for tags and data required and returns
168      * checking result to <code>XFilter</code> interface test. All
169      * the information about errors occured in XML data is written
170      * to log specified.
171      * @see ifc.document._XFilter
172      */
173     protected class MetaFilterChecker extends XMLTools.XMLTagsChecker
174       implements ifc.document._XFilter.FilterChecker {
175 
176         /**
177          * Creates a class which will write information
178          * into log specified.
179          */
MetaFilterChecker(PrintWriter log)180         public MetaFilterChecker(PrintWriter log) {
181             super(log) ;
182         }
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          */
checkFilter()191         public boolean checkFilter() {
192             return checkTags();
193         }
194     }
195 }
196 
197