1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sw;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.frame.XController;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 import com.sun.star.view.XViewSettingsSupplier;
45 
46 /**
47  * Test for object which is represented by service
48  * <code>com.sun.star.comp.Writer.XMLSettingsImporter</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::XImporter</code></li>
53  *  <li><code>com::sun::star::document::XFilter</code></li>
54  *  <li><code>com::sun::star::document::ImportFilter</code></li>
55  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
56  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
57 
58  * </ul>
59  * @see com.sun.star.lang.XInitialization
60  * @see com.sun.star.document.XImporter
61  * @see com.sun.star.document.XFilter
62  * @see com.sun.star.document.ImportFilter
63  * @see com.sun.star.beans.XPropertySet
64  * @see com.sun.star.xml.sax.XDocumentHandler
65  * @see ifc.lang._XInitialization
66  * @see ifc.document._XImporter
67  * @see ifc.document._XFilter
68  * @see ifc.document._XExporter
69  * @see ifc.beans._XPropertySet
70  * @see ifc.xml.sax._XDocumentHandler
71  */
72 public class XMLSettingsImporter extends TestCase {
73     XTextDocument xTextDoc;
74 
75     /**
76     * New text document created.
77     */
78     protected void initialize( TestParameters tParam, PrintWriter log ) {
79         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
80 
81         try {
82             log.println( "creating a textdocument" );
83             xTextDoc = SOF.createTextDoc( null );
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     * Text document destroyed.
93     */
94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing xTextDoc " );
96         util.DesktopTools.closeDoc(xTextDoc);
97     }
98 
99     /**
100     * Creating a Testenvironment for the interfaces to be tested.
101     * Creates an instance of the service
102     * <code>com.sun.star.comp.Writer.XMLSettingsImporter</code><p>
103     *
104     * The text document is set as a target document for importer.
105     * Imported XML-data contains only settings tags including
106     * title tag with test zoom factor.
107     * After import zoom factor getting from target document is checked.
108     *     Object relations created :
109     * <ul>
110     *  <li> <code>'XDocumentHandler.XMLData'</code> for
111     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
112     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
113     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
114     *  <li> <code>'TargetDocument'</code> for
115     *      {@link ifc.document._XImporter} interface </li>
116     * </ul>
117     */
118     public synchronized TestEnvironment createTestEnvironment
119         (TestParameters tParam, PrintWriter log ) throws StatusException {
120 
121         XInterface oObj = null;
122         Object oInt = null ;
123         final short impZoom = 50 ;
124 
125         // creation of testobject here
126         // first we write what we are intend to do to log file
127         log.println( "creating a test environment" );
128 
129         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
130 
131         try {
132             oInt = xMSF.createInstance
133                 ("com.sun.star.comp.Writer.XMLSettingsImporter") ;
134 
135         } catch (com.sun.star.uno.Exception e) {
136             e.printStackTrace(log) ;
137             throw new StatusException("Can't create component.", e) ;
138         }
139 
140 
141         oObj = (XInterface) oInt ;
142 
143         // create testobject here
144         log.println( "creating a new environment for Paragraph object" );
145         TestEnvironment tEnv = new TestEnvironment( oObj );
146 
147         // adding relation
148         tEnv.addObjRelation("TargetDocument", xTextDoc) ;
149 
150         // adding relation for XDocumentHandler
151         String[][] xml = new String[][] {
152             {"start", "office:document-settings",
153                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
154                 "xmlns:config", "CDATA", "http://openoffice.org/2001/config",
155                 "xmlns:xlink", "CDATA", "http://www.w3.org/1999/xlink",
156                 },
157                 {"start", "office:settings"},
158                     {"start", "config:config-item-set",
159                         "config:name", "CDATA", "view-settings"
160                     },
161                         {"start", "config:config-item-map-indexed",
162                             "config:name", "CDATA", "Views"},
163                             {"start", "config:config-item-map-entry"},
164                                 {"start", "config:config-item",
165                                     "config:name", "CDATA", "ZoomFactor",
166                                     "config:type", "CDATA", "short"
167                                 },
168                                     {"chars", String.valueOf(impZoom)},
169                                 {"end", "config:config-item"},
170                                 {"start", "config:config-item",
171                                     "config:name", "CDATA", "ZoomType",
172                                     "config:type", "CDATA", "short"
173                                 },
174                                     {"chars", "0"},
175                                 {"end", "config:config-item"},
176                             {"end", "config:config-item-map-entry"},
177                         {"end", "config:config-item-map-indexed"},
178                     {"end", "config:config-item-set"},
179                 {"end", "office:settings"},
180             {"end", "office:document-settings"}} ;
181 
182         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
183 
184         final PrintWriter logF = log ;
185         XController xController = xTextDoc.getCurrentController();
186         XViewSettingsSupplier xViewSetSup = (XViewSettingsSupplier)
187             UnoRuntime.queryInterface(XViewSettingsSupplier.class, xController);
188         final XPropertySet xPropSet = xViewSetSup.getViewSettings();
189         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
190             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
191                 public boolean checkImport() {
192                     try {
193                         Short gValue = (Short)
194                             xPropSet.getPropertyValue("ZoomValue");
195                         logF.println("ZoomValue property value = " + gValue) ;
196                         return impZoom == gValue.shortValue() ;
197                     } catch (com.sun.star.uno.Exception e) {
198                         logF.println("Exception while checking import :") ;
199                         e.printStackTrace(logF) ;
200                         return false ;
201                     }
202                 }
203             }) ;
204 
205         return tEnv;
206     } // finish method getTestEnvironment
207 }
208 
209