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.Chart;
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.chart.XChartDocument;
37 import com.sun.star.document.XExporter;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.Any;
40 import com.sun.star.uno.Exception;
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.Chart.XMLContentExporter</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 XMLContentExporter extends TestCase {
69     XChartDocument xChartDoc = null;
70 
71     /**
72      * New text document created.
73      */
initialize( TestParameters tParam, PrintWriter log )74     protected void initialize( TestParameters tParam, PrintWriter log ) {
75 
76         // get a soffice factory object
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
78 
79         try {
80             log.println( "creating a chartdocument" );
81             xChartDoc = SOF.createChartDoc(null);
82         } catch ( Exception e ) {
83             // Some exception occured.FAILED
84             e.printStackTrace( log );
85             throw new StatusException( "Couldn't create document", e );
86         }
87     }
88 
89     /**
90      * Close document
91      */
cleanup( TestParameters tParam, PrintWriter log )92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         if( xChartDoc!=null ) {
94             log.println( "    closing xChartDoc" );
95             util.DesktopTools.closeDoc(xChartDoc);
96             xChartDoc = null;
97         }
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Creates an instance of the service
103     * <code>com.sun.star.comp.Chart.XMLContentExporter</code> with
104     * argument which is an implementation of <code>XDocumentHandler</code>
105     * and which can check if required tags and character data is
106     * exported. <p>
107     * The chart document is set as a source document for exporter
108     * created. A new 'main title' is set for chart. This made
109     * for checking if this title is successfully exported within
110     * the document content.
111     *     Object relations created :
112     * <ul>
113     *  <li> <code>'MediaDescriptor'</code> for
114     *      {@link ifc.document._XFilter} interface </li>
115     *  <li> <code>'XFilter.Checker'</code> for
116     *      {@link ifc.document._XFilter} interface </li>
117     *  <li> <code>'SourceDocument'</code> for
118     *      {@link ifc.document._XExporter} interface </li>
119     * </ul>
120     */
createTestEnvironment(TestParameters tParam, PrintWriter log)121     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
122 
123         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
124         XInterface oObj = null;
125         final String exportStr = "XMLContentExporter" ;
126 
127         FilterChecker filter = new FilterChecker(log);
128         Any arg = new Any(new Type(XDocumentHandler.class),filter);
129 
130         try {
131             oObj = (XInterface) xMSF.createInstanceWithArguments(
132                 "com.sun.star.comp.Chart.XMLContentExporter",
133                 new Object[] {arg});
134             XExporter xEx = (XExporter)
135                 UnoRuntime.queryInterface(XExporter.class,oObj);
136             xEx.setSourceDocument(xChartDoc);
137 
138             Object oTitle = xChartDoc.getTitle() ;
139             XPropertySet xTitleProp = (XPropertySet) UnoRuntime.queryInterface
140                 (XPropertySet.class, oTitle) ;
141             xTitleProp.setPropertyValue("String", exportStr) ;
142         } catch (com.sun.star.uno.Exception e) {
143             e.printStackTrace(log) ;
144             throw new StatusException("Can't create component.", e) ;
145         }
146 
147         filter.addTag(new XMLTools.Tag("office:document-content")) ;
148         filter.addTagEnclosed(new XMLTools.Tag("office:body"),
149             new XMLTools.Tag("office:document-content"));
150         filter.addCharactersEnclosed(exportStr,
151             new XMLTools.Tag("chart:title")) ;
152 
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[] {"schart: StarOffice XML (Chart)"}));
161         tEnv.addObjRelation("SourceDocument",xChartDoc);
162         tEnv.addObjRelation("XFilter.Checker", filter) ;
163         log.println("Implementation Name: "+util.utils.getImplName(oObj));
164 
165         return tEnv;
166 
167     }
168 
169     /**
170      * This class checks the XML for tags and data required and returns
171      * checking result to <code>XFilter</code> interface test. All
172      * the information about errors occurred in XML data is written
173      * to log specified.
174      * @see ifc.document._XFilter
175      */
176     protected class FilterChecker extends XMLTools.XMLChecker
177         implements ifc.document._XFilter.FilterChecker {
178 
179         /**
180          * Creates a class which will write information
181          * into log specified.
182          */
FilterChecker(PrintWriter log)183         public FilterChecker(PrintWriter log) {
184             super(log, true) ;
185         }
186         /**
187          * <code>_XFilter.FilterChecker</code> interface method
188          * which returns the result of XML checking.
189          * @return <code>true</code> if the XML data exported was
190          * valid (i.e. all necessary tags and character data exists),
191          * <code>false</code> if some errors occurred.
192          */
checkFilter()193         public boolean checkFilter() {
194             return check() ;
195         }
196     }
197 }
198