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.document.XExporter;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.text.XSimpleText;
38 import com.sun.star.text.XTextCursor;
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.XMLExporter</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 XMLExporter extends TestCase {
69     XTextDocument xTextDoc;
70     FilterChecker Filter;
71 
72     /**
73      * New text document created.
74      */
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 occures.FAILED
83             e.printStackTrace( log );
84             throw new StatusException( "Couldn't create document", e );
85         }
86     }
87 
88     /**
89      * Document disposed here.
90      */
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.XMLExporter</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. A string inserted into document. This made
105     * for checking if this string is successfully exported within
106     * the document content.
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     */
117     public synchronized TestEnvironment createTestEnvironment
118             (TestParameters tParam, PrintWriter log) {
119 
120         final String TEST_STR = "XMLExporter";
121 
122         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
123         XInterface oObj = null;
124 
125         Filter = new FilterChecker(log);
126         Any arg = new Any(new Type(XDocumentHandler.class),Filter);
127 
128         try {
129             oObj = (XInterface) xMSF.createInstanceWithArguments(
130                 "com.sun.star.comp.Writer.XMLExporter", new Object[] {arg});
131             XExporter xEx = (XExporter) UnoRuntime.queryInterface
132                 (XExporter.class,oObj);
133             xEx.setSourceDocument(xTextDoc);
134 
135             XSimpleText aText = xTextDoc.getText();
136             XTextCursor curs = (XTextCursor) aText.createTextCursor();
137                         aText.insertString(curs, TEST_STR, false);
138         } catch (com.sun.star.uno.Exception e) {
139             e.printStackTrace(log) ;
140             throw new StatusException("Can't create component.", e) ;
141         }
142 
143         // adding tags which must be contained in XML output
144         Filter.addTag("office:document") ;
145         Filter.addTagEnclosed("office:meta", "office:document") ;
146         Filter.addTagEnclosed("office:settings", "office:document") ;
147         Filter.addTagEnclosed("office:script", "office:document") ;
148         Filter.addTagEnclosed("office:styles", "office:document") ;
149         Filter.addTagEnclosed("office:body", "office:document") ;
150         Filter.addCharactersEnclosed(TEST_STR, "text:p") ;
151 
152         // create testobject here
153         log.println( "creating a new environment" );
154         TestEnvironment tEnv = new TestEnvironment( oObj );
155 
156         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
157                         new String[] {"FilterName"},
158             new Object[] {"swriter: StarOffice XML (Writer)"}));
159         tEnv.addObjRelation("SourceDocument",xTextDoc);
160         tEnv.addObjRelation("XFilter.Checker", Filter) ;
161         log.println("Implementation Name: "+util.utils.getImplName(oObj));
162         return tEnv;
163 
164     } // finish method getTestEnvironment
165 
166     /**
167      * This class checks the XML for tags and data required and returns
168      * checking result to <code>XFilter</code> interface test. All
169      * the information about errors occurred in XML data is written
170      * to log specified.
171      * @see ifc.document._XFilter
172      */
173     protected class FilterChecker extends XMLTools.XMLTagsChecker
174         implements ifc.document._XFilter.FilterChecker {
175 
176         /**
177          * Creates a class which will write information
178          * into log specified.
179          */
180         public FilterChecker(PrintWriter log) {
181             super(log) ;
182         }
183         /**
184          * <code>_XFilter.FilterChecker</code> interface method
185          * which returns the result of XML checking.
186          * @return <code>true</code> if the XML data exported was
187          * valid (i.e. all necessary tags and character data exists),
188          * <code>false</code> if some errors occurred.
189          */
190         public boolean checkFilter() {
191             return checkTags() ;
192         }
193     }
194 }
195 
196