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.Draw;
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.Draw.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 xDrawDoc = null;
72 
73     /**
74     * New draw document created.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77 
78         // get a soffice factory object
79         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
80 
81         try {
82             log.println( "creating a drawdocument" );
83             xDrawDoc = SOF.createDrawDoc(null);
84         } catch ( Exception e ) {
85             // Some exception occures.FAILED
86             e.printStackTrace( log );
87             throw new StatusException( "Couldn't create document", e );
88         }
89     }
90 
91     /**
92     * Document disposed here.
93     */
cleanup( TestParameters tParam, PrintWriter log )94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing xDrawDoc " );
96         xDrawDoc.dispose();
97     }
98 
99     /**
100     * Creating a Testenvironment for the interfaces to be tested.
101     * Creates an instance of the service
102     * <code>om.sun.star.comp.Draw.XMLSettingsExporter</code> with
103     * argument which is an implementation of <code>XDocumentHandler</code>
104     * and which can check if required tags and character data is
105     * exported. <p>
106     * The draw document is set as a source document for exporter
107     * created. Property 'IsLayerMode' set to true.
108     * This made for checking if this property is successfully exported within
109     * the document styles information.
110     *     Object relations created :
111     * <ul>
112     *  <li> <code>'MediaDescriptor'</code> for
113     *      {@link ifc.document._XFilter} interface </li>
114     *  <li> <code>'XFilter.Checker'</code> for
115     *      {@link ifc.document._XFilter} interface </li>
116     *  <li> <code>'SourceDocument'</code> for
117     *      {@link ifc.document._XExporter} interface </li>
118     * </ul>
119     */
createTestEnvironment(TestParameters tParam, PrintWriter log)120     protected synchronized TestEnvironment createTestEnvironment
121             (TestParameters tParam, PrintWriter log) {
122 
123         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
124         XInterface oObj = null;
125 
126         FilterChecker filter = new FilterChecker(log) ;
127         Any arg = new Any(new Type(XDocumentHandler.class),filter);
128 
129         try {
130             oObj = (XInterface) xMSF.createInstanceWithArguments(
131                 "com.sun.star.comp.Draw.XMLSettingsExporter",
132                 new Object[] {arg});
133             XExporter xEx = (XExporter)
134                 UnoRuntime.queryInterface(XExporter.class, oObj);
135             xEx.setSourceDocument(xDrawDoc);
136 
137             //set some settings
138             XModel xDrawModel = (XModel)
139                 UnoRuntime.queryInterface(XModel.class, xDrawDoc);
140             XController xController = xDrawModel.getCurrentController();
141             XPropertySet xPropSet = (XPropertySet)
142                 UnoRuntime.queryInterface(XPropertySet.class, xController);
143             xPropSet.setPropertyValue("IsLayerMode", new Boolean("true"));
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 Head Tag existance and that property has changed
151         filter.addTag(new XMLTools.Tag ("office:document-settings"));
152         filter.addTagEnclosed(
153             new XMLTools.Tag ("office:settings"),
154             new XMLTools.Tag ("office:document-settings") );
155         filter.addTagEnclosed(
156             new XMLTools.Tag ("config:config-item-set"),
157             new XMLTools.Tag ("office:settings") );
158         filter.addTagEnclosed(
159             new XMLTools.Tag ("config:config-item-map-indexed"),
160             new XMLTools.Tag ("config:config-item-set") );
161         filter.addCharactersEnclosed( "true",
162             new XMLTools.Tag( "config:config-item",
163                               "config:name",
164                               "IsLayerMode" ));
165 
166         // create testobject here
167         log.println( "creating a new environment" );
168         TestEnvironment tEnv = new TestEnvironment( oObj );
169 
170         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
171             new String[] {"FilterName"},
172             new Object[] {"sdraw: StarOffice XML (Draw)"}));
173         tEnv.addObjRelation("SourceDocument", xDrawDoc);
174         tEnv.addObjRelation("XFilter.Checker", filter);
175         return tEnv;
176     }
177 
178     /**
179      * This class checks the XML for tags and data required and returns
180      * checking result to <code>XFilter</code> interface test. All
181      * the information about errors occured in XML data is written
182      * to log specified.
183      * @see ifc.document._XFilter
184      */
185     protected class FilterChecker extends XMLTools.XMLChecker
186         implements ifc.document._XFilter.FilterChecker {
187 
188         /**
189          * Creates a class which will write information
190          * into log specified.
191          */
FilterChecker(PrintWriter log)192         public FilterChecker(PrintWriter log) {
193             super(log, false) ;
194         }
195 
196         /**
197          * <code>_XFilter.FilterChecker</code> interface method
198          * which returns the result of XML checking.
199          * @return <code>true</code> if the XML data exported was
200          * valid (i.e. all necessary tags and character data exists),
201          * <code>false</code> if some errors occured.
202          */
checkFilter()203         public boolean checkFilter() {
204             return check();
205         }
206     }
207 }
208