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.XExporter;
37 import com.sun.star.frame.XController;
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.view.XViewSettingsSupplier;
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.Calc.XMLSettingsExporter</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 XMLSettingsExporter extends TestCase {
70 
71     XTextDocument xTextDoc;
72     SettingsFilterChecker Filter;
73 
74     /**
75      * New text document created.
76      */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79 
80         try {
81             log.println( "creating a textdocument" );
82             xTextDoc = SOF.createTextDoc( null );
83 
84         } catch ( com.sun.star.uno.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     /**
93      * Document disposed here.
94      */
95     protected void cleanup( TestParameters tParam, PrintWriter log ) {
96         log.println( "    disposing xTextDoc " );
97         util.DesktopTools.closeDoc(xTextDoc);
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.Calc.XMLSettingsExporter</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 text document is set as a source document for exporter
108     * created. New document zoom is set as one of the 'View' settings. This made
109     * for checking if this setting is successfully exported within
110     * the document settings.
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     */
121     public synchronized TestEnvironment createTestEnvironment
122             (TestParameters tParam, PrintWriter log) {
123 
124         final short ZOOM = 50;
125 
126         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
127         XInterface oObj = null;
128 
129         Filter = new SettingsFilterChecker(log);
130         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
131         try {
132             oObj = (XInterface) xMSF.createInstanceWithArguments(
133                 "com.sun.star.comp.Writer.XMLSettingsExporter",
134                 new Object[] {arg});
135             XExporter xEx = (XExporter) UnoRuntime.queryInterface
136                 (XExporter.class,oObj);
137             xEx.setSourceDocument(xTextDoc);
138 
139             //set some settings
140             XController xController = xTextDoc.getCurrentController();
141             XViewSettingsSupplier xViewSetSup = (XViewSettingsSupplier)
142                 UnoRuntime.queryInterface(XViewSettingsSupplier.class,
143                 xController);
144             XPropertySet xPropSet = xViewSetSup.getViewSettings();
145             xPropSet.setPropertyValue("ZoomValue", new Short(ZOOM));
146 
147         } catch (com.sun.star.uno.Exception e) {
148             e.printStackTrace(log) ;
149             throw new StatusException("Can't create component.", e) ;
150         }
151 
152         // adding tags which must be contained in XML output
153         Filter.addTag( new XMLTools.Tag("office:document-settings") );
154         Filter.addTagEnclosed(
155             new XMLTools.Tag("office:settings"),
156             new XMLTools.Tag("office:document-settings") );
157         Filter.addCharactersEnclosed(
158             String.valueOf(ZOOM),
159             new XMLTools.Tag("config:config-item",
160                 "config:name", "ZoomFactor") );
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[] {"swriter: StarOffice XML (Writer)"}));
169         tEnv.addObjRelation("SourceDocument",xTextDoc);
170         tEnv.addObjRelation("XFilter.Checker", Filter);
171         return tEnv;
172 
173     }
174 
175     /**
176      * This class checks the XML for tags and data required and returns
177      * checking result to <code>XFilter</code> interface test. All
178      * the information about errors occurred in XML data is written
179      * to log specified.
180      * @see ifc.document._XFilter
181      */
182     protected class SettingsFilterChecker extends XMLTools.XMLChecker
183       implements ifc.document._XFilter.FilterChecker {
184 
185         /**
186          * Creates a class which will write information
187          * into log specified.
188          */
189         public SettingsFilterChecker(PrintWriter log) {
190             super(log, false) ;
191         }
192 
193         /**
194          * <code>_XFilter.FilterChecker</code> interface method
195          * which returns the result of XML checking.
196          * @return <code>true</code> if the XML data exported was
197          * valid (i.e. all necessary tags and character data exists),
198          * <code>false</code> if some errors occurred.
199          */
200         public boolean checkFilter() {
201             return check();
202         }
203     }
204 }
205 
206