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 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.XFootnotesSupplier;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 
41 /**
42  * Test for object which is represented by service
43  * <code>com.sun.star.comp.Writer.XMLStylesImporter</code>. <p>
44  * Object implements the following interfaces :
45  * <ul>
46  *  <li><code>com::sun::star::lang::XInitialization</code></li>
47  *  <li><code>com::sun::star::document::XImporter</code></li>
48  *  <li><code>com::sun::star::document::XFilter</code></li>
49  *  <li><code>com::sun::star::document::ImportFilter</code></li>
50  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
51  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
52 
53  * </ul>
54  * @see com.sun.star.lang.XInitialization
55  * @see com.sun.star.document.XImporter
56  * @see com.sun.star.document.XFilter
57  * @see com.sun.star.document.ImportFilter
58  * @see com.sun.star.beans.XPropertySet
59  * @see com.sun.star.xml.sax.XDocumentHandler
60  * @see ifc.lang._XInitialization
61  * @see ifc.document._XImporter
62  * @see ifc.document._XFilter
63  * @see ifc.document._XExporter
64  * @see ifc.beans._XPropertySet
65  * @see ifc.xml.sax._XDocumentHandler
66  */
67 public class XMLStylesImporter extends TestCase {
68     XTextDocument xTextDoc;
69 
70     /**
71     * New text document created.
72     */
initialize( TestParameters tParam, PrintWriter log )73     protected void initialize( TestParameters tParam, PrintWriter log ) {
74         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
75 
76         try {
77             log.println( "creating a textdocument" );
78             xTextDoc = SOF.createTextDoc( null );
79         } catch ( com.sun.star.uno.Exception e ) {
80             // Some exception occures.FAILED
81             e.printStackTrace( log );
82             throw new StatusException( "Couldn't create document", e );
83         }
84     }
85 
86     /**
87     * Text document destroyed.
88     */
cleanup( TestParameters tParam, PrintWriter log )89     protected void cleanup( TestParameters tParam, PrintWriter log ) {
90         log.println( "    disposing xTextDoc " );
91         util.DesktopTools.closeDoc(xTextDoc);
92     }
93 
94     /**
95     * Creating a Testenvironment for the interfaces to be tested.
96     * Creates an instance of the service
97     * <code>com.sun.star.comp.Writer.XMLStylesImporter</code><p>
98     *
99     * The text document is set as a target document for importer.
100     * Imported XML-data contains only style tags including
101     * title tag with test prefix.
102     * After import prefixes getting from target document is checked.
103     *     Object relations created :
104     * <ul>
105     *  <li> <code>'XDocumentHandler.XMLData'</code> for
106     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
107     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
108     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
109     *  <li> <code>'TargetDocument'</code> for
110     *      {@link ifc.document._XImporter} interface </li>
111     * </ul>
112     */
createTestEnvironment( TestParameters tParam, PrintWriter log )113     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
114                                                   PrintWriter log )
115                                                     throws StatusException {
116 
117         XInterface oObj = null;
118         Object oInt = null ;
119         final String impPrefix = "Prefix" ;
120 
121         // creation of testobject here
122         // first we write what we are intend to do to log file
123         log.println( "creating a test environment" );
124 
125         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
126 
127         try {
128             oInt = xMSF.createInstance
129                 ("com.sun.star.comp.Writer.XMLStylesImporter") ;
130 
131         } catch (com.sun.star.uno.Exception e) {
132             e.printStackTrace(log) ;
133             throw new StatusException("Can't create component.", e) ;
134         }
135 
136         oObj = (XInterface) oInt ;
137 
138         // create testobject here
139         log.println( "creating a new environment for Paragraph object" );
140         TestEnvironment tEnv = new TestEnvironment( oObj );
141 
142         // adding relation
143         tEnv.addObjRelation("TargetDocument", xTextDoc) ;
144 
145         // adding relation for XDocumentHandler
146         String[][] xml = new String[][] {
147             {"start", "office:document-styles",
148                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
149                 "xmlns:style", "CDATA", "http://openoffice.org/2000/style",
150                 "xmlns:text", "CDATA", "http://openoffice.org/2000/text"
151                 },
152             {"start", "office:styles"},
153             {"start", "text:footnotes-configuration",
154                 "style:num-prefix", "CDATA", impPrefix},
155             {"end", "text:footnotes-configuration"},
156             {"end", "office:styles"},
157             {"end", "office:document-styles"}} ;
158 
159         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
160 
161         XFootnotesSupplier supp = (XFootnotesSupplier) UnoRuntime.queryInterface
162             (XFootnotesSupplier.class, xTextDoc);
163         final XPropertySet set = supp.getFootnoteSettings();
164         final PrintWriter logF = log ;
165 
166         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
167             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
168                 public boolean checkImport() {
169                     try {
170                         String prefix = (String) set.getPropertyValue("Prefix");
171                         logF.println("Prefix returned = '" + prefix + "'") ;
172                         return impPrefix.equals(prefix) ;
173                     } catch (com.sun.star.uno.Exception e) {
174                         logF.println("Exception occured while checking filter :") ;
175                         e.printStackTrace(logF) ;
176                         return false ;
177                     }
178                 }
179             }) ;
180 
181         return tEnv;
182     } // finish method getTestEnvironment
183 }
184 
185