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.lang.XMultiServiceFactory;
38 import com.sun.star.text.XFootnotesSupplier;
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.XMLStylesExporter</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 XMLStylesExporter extends TestCase {
69 
70     XTextDocument xTextDoc;
71 
72     /**
73      * New text document created.
74      */
initialize( TestParameters tParam, PrintWriter log )75     protected void initialize( TestParameters tParam, PrintWriter log ) {
76         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
77 
78         try {
79             log.println( "creating a textdocument" );
80             xTextDoc = SOF.createTextDoc( null );
81         } catch ( com.sun.star.uno.Exception e ) {
82             // Some exception occured.FAILED
83             e.printStackTrace( log );
84             throw new StatusException( "Couldn't create document", e );
85         }
86     }
87 
88     /**
89      * Document disposed here.
90      */
cleanup( TestParameters tParam, PrintWriter log )91     protected void cleanup( TestParameters tParam, PrintWriter log ) {
92         log.println( "    disposing xTextDoc " );
93         util.DesktopTools.closeDoc(xTextDoc);
94     }
95 
96     /**
97     * Creating a Testenvironment for the interfaces to be tested.
98     * Creates an instance of the service
99     * <code>com.sun.star.comp.Calc.XMLStylesExporter</code> with
100     * argument which is an implementation of <code>XDocumentHandler</code>
101     * and which can check if required tags and character data is
102     * exported. <p>
103     * The text document is set as a source document for exporter
104     * created. Footnote property 'Prefix' is set to a new value. This made
105     * for checking if this property value is successfully exported within
106     * the document styles.
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 ) {
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 
125         try {
126             oObj = (XInterface) xMSF.createInstanceWithArguments(
127                 "com.sun.star.comp.Writer.XMLStylesExporter",
128                 new Object[] {arg});
129             XExporter xEx = (XExporter) UnoRuntime.queryInterface
130                 (XExporter.class,oObj);
131             xEx.setSourceDocument(xTextDoc);
132         } catch (com.sun.star.uno.Exception e) {
133             e.printStackTrace(log) ;
134             throw new StatusException("Can't create component.", e) ;
135         }
136 
137         // Cheching Head Tag existence and that property has changed
138         filter.addTag(new XMLTools.Tag ("office:document-styles"));
139         filter.addTag(new XMLTools.Tag ("text:footnotes-configuration",
140             "style:num-prefix","New Property")) ;
141 
142         // create testobject here
143         log.println( "creating a new environment" );
144         TestEnvironment tEnv = new TestEnvironment( oObj );
145 
146         // Obtain Footnotes properties
147         XFootnotesSupplier supp = (XFootnotesSupplier) UnoRuntime.queryInterface
148             (XFootnotesSupplier.class, xTextDoc);
149         XPropertySet set = supp.getFootnoteSettings();
150 
151         // Change property "Prefix"
152         try {
153             set.setPropertyValue("Prefix","New Property");
154         } catch (com.sun.star.uno.Exception e) {
155             e.printStackTrace( log );
156             throw new StatusException( "Couldn't change property value", e );
157         }
158 
159         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
160             new String[] {"FilterName"},
161             new Object[] {"swriter: StarOffice XML (Writer)"}));
162         tEnv.addObjRelation("SourceDocument",xTextDoc);
163         tEnv.addObjRelation("XFilter.Checker", filter) ;
164         return tEnv;
165 
166     }
167 
168     /**
169      * This class checks the XML for tags and data required and returns
170      * checking result to <code>XFilter</code> interface test. All
171      * the information about errors occurred in XML data is written
172      * to log specified.
173      * @see ifc.document._XFilter
174      */
175     protected class FilterChecker extends XMLTools.XMLChecker
176         implements ifc.document._XFilter.FilterChecker {
177 
178         /**
179          * Creates a class which will write information
180          * into log specified.
181          */
FilterChecker(PrintWriter log)182         public FilterChecker(PrintWriter log) {
183             super(log,false) ;
184         }
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 }
199 
200