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.XExporter;
37 import com.sun.star.frame.XController;
38 import com.sun.star.frame.XModel;
39 import com.sun.star.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.Any;
42 import com.sun.star.uno.Exception;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 import com.sun.star.xml.sax.XDocumentHandler;
47 
48 /**
49  * Test for object which is represented by service
50  * <code>com.sun.star.comp.Impress.XMLSettingsExporter</code>. <p>
51  * Object implements the following interfaces :
52  * <ul>
53  *  <li><code>com::sun::star::lang::XInitialization</code></li>
54  *  <li><code>com::sun::star::document::ExportFilter</code></li>
55  *  <li><code>com::sun::star::document::XFilter</code></li>
56  *  <li><code>com::sun::star::document::XExporter</code></li>
57  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
58  * </ul>
59  * @see com.sun.star.lang.XInitialization
60  * @see com.sun.star.document.ExportFilter
61  * @see com.sun.star.document.XFilter
62  * @see com.sun.star.document.XExporter
63  * @see com.sun.star.beans.XPropertySet
64  * @see ifc.lang._XInitialization
65  * @see ifc.document._ExportFilter
66  * @see ifc.document._XFilter
67  * @see ifc.document._XExporter
68  * @see ifc.beans._XPropertySet
69  */
70 public class XMLSettingsExporter extends TestCase {
71     XComponent xImpressDoc = null;
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 
79         try {
80             log.println( "creating an impress document" );
81             xImpressDoc = SOF.createImpressDoc(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     * Document disposed here.
91     */
cleanup( TestParameters tParam, PrintWriter log )92     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93         log.println( "    disposing xImpressDoc " );
94         xImpressDoc.dispose();
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.Impress.XMLSettingsExporter</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 Impress document is set as a source document for exporter
105     * created. Then setting 'IsLayerMode' is changed to a new value.
106     * After this filter checks that value has changed in the XML output.
107     *     Object relations created :
108     * <ul>
109     *  <li> <code>'MediaDescriptor'</code> for
110     *      {@link ifc.document._XFilter} interface </li>
111     *  <li> <code>'XFilter.Checker'</code> for
112     *      {@link ifc.document._XFilter} interface </li>
113     *  <li> <code>'SourceDocument'</code> for
114     *      {@link ifc.document._XExporter} interface </li>
115     * </ul>
116     */
createTestEnvironment(TestParameters tParam, PrintWriter log )117     public synchronized TestEnvironment createTestEnvironment
118         (TestParameters tParam, PrintWriter log ) throws StatusException {
119 
120         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
121         XInterface oObj = null;
122         FilterChecker filter = new FilterChecker(log);
123         Any arg = new Any(new Type(XDocumentHandler.class),filter);
124         final boolean NewDataValue;
125 
126         try {
127             oObj = (XInterface) xMSF.createInstanceWithArguments(
128                 "com.sun.star.comp.Impress.XMLSettingsExporter",
129                 new Object[] {arg});
130             XExporter xEx = (XExporter)
131                 UnoRuntime.queryInterface(XExporter.class,oObj);
132             xEx.setSourceDocument(xImpressDoc);
133 
134             //set some settings
135             XModel xImpressModel = (XModel)
136                 UnoRuntime.queryInterface(XModel.class, xImpressDoc);
137             XController xController = xImpressModel.getCurrentController();
138             XPropertySet xPropSet = (XPropertySet)
139                 UnoRuntime.queryInterface(XPropertySet.class, xController);
140             NewDataValue = ! ((Boolean) xPropSet.getPropertyValue
141                 ("IsLayerMode")).booleanValue();
142             xPropSet.setPropertyValue("IsLayerMode",
143                 new Boolean (NewDataValue));
144 
145 /*            XViewDataSupplier xViewDataSupp = (XViewDataSupplier)
146                 UnoRuntime.queryInterface(XViewDataSupplier.class, xImpressDoc);
147             XIndexAccess xViewData = xViewDataSupp.getViewData();
148             PropertyValue[] xData = (PropertyValue[]) xViewData.getByIndex(0);
149             NewDataValue = ! ((Boolean) xData[1].Value).booleanValue();
150             xData[1].Value = new Boolean(NewDataValue);
151             XIndexReplace xViewDataReplacable = (XIndexReplace)
152                 UnoRuntime.queryInterface(XIndexReplace.class, xViewData);
153             xViewDataReplacable.replaceByIndex(0,xData);
154 */
155 
156         } catch (com.sun.star.uno.Exception e) {
157             e.printStackTrace(log) ;
158             throw new StatusException("Can't create component.", e) ;
159         }
160 
161         // Adding tags for checking existence of head tag and other tags
162         filter.addTagEnclosed(new XMLTools.Tag("office:settings"),
163             new XMLTools.Tag("office:document-settings"));
164         filter.addTagEnclosed(new XMLTools.Tag("config:config-item-set"),
165             new XMLTools.Tag("office:settings"));
166         filter.addTagEnclosed(new XMLTools.Tag("config:config-item-map-indexed"),
167             new XMLTools.Tag("config:config-item-set"));
168         filter.addTagEnclosed(new XMLTools.Tag("config:config-item-map-entry"),
169             new XMLTools.Tag("config:config-item-map-indexed"));
170         filter.addTagEnclosed(new XMLTools.Tag("config:config-item"),
171             new XMLTools.Tag("config:config-item-map-entry"));
172         filter.addCharactersEnclosed(String.valueOf(NewDataValue),
173             new XMLTools.Tag("config:config-item","config:name","IsLayerMode"));
174 
175         // create testobject here
176         log.println( "creating a new environment" );
177         TestEnvironment tEnv = new TestEnvironment( oObj );
178 
179         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
180             new String[] {"FilterName"},
181             new Object[] {"simpress: StarOffice XML (Impress)"}));
182         tEnv.addObjRelation("SourceDocument",xImpressDoc);
183         tEnv.addObjRelation("XFilter.Checker", filter);
184         return tEnv;
185     }
186 
187     /**
188     * This class checks the XML for tags and data required and returns
189     * checking result to <code>XFilter</code> interface test. All
190     * the information about errors occurred in XML data is written
191     * to log specified.
192     * @see ifc.document._XFilter
193     */
194     protected class FilterChecker extends XMLTools.XMLChecker
195         implements ifc.document._XFilter.FilterChecker {
196 
197         /**
198         * Creates a class which will write information
199         * into log specified.
200         */
FilterChecker(PrintWriter log)201         public FilterChecker(PrintWriter log) {
202             super(log,false) ;
203         }
204         /**
205         * <code>_XFilter.FilterChecker</code> interface method
206         * which returns the result of XML checking.
207         * @return <code>true</code> if the XML data exported was
208         * valid (i.e. all necessary tags and character data exists),
209         * <code>false</code> if some errors occurred.
210         */
checkFilter()211         public boolean checkFilter() {
212             return check();
213         }
214     }
215 }
216