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._sm;
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.lang.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 /**
45  * Test for object which is represented by service
46  * <code>com.sun.star.comp.Math.XMLSettingsImporter</code>. <p>
47  * Object implements the following interfaces :
48  * <ul>
49  *  <li><code>com::sun::star::lang::XInitialization</code></li>
50  *  <li><code>com::sun::star::document::XImporter</code></li>
51  *  <li><code>com::sun::star::document::XFilter</code></li>
52  *  <li><code>com::sun::star::document::ImportFilter</code></li>
53  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
54  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
55  * </ul>
56  * @see com.sun.star.lang.XInitialization
57  * @see com.sun.star.document.XImporter
58  * @see com.sun.star.document.XFilter
59  * @see com.sun.star.document.ImportFilter
60  * @see com.sun.star.beans.XPropertySet
61  * @see com.sun.star.xml.sax.XDocumentHandler
62  * @see ifc.lang._XInitialization
63  * @see ifc.document._XImporter
64  * @see ifc.document._XFilter
65  * @see ifc.document._XExporter
66  * @see ifc.beans._XPropertySet
67  * @see ifc.xml.sax._XDocumentHandler
68  */
69 public class XMLSettingsImporter extends TestCase {
70     XComponent xMathDoc;
71 
72     /**
73     * New math document created.
74     */
75     protected void initialize( TestParameters tParam, PrintWriter log ) {
76 
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78         try {
79             xMathDoc = SOF.openDoc("smath","_blank");
80         } catch (com.sun.star.uno.Exception ex) {
81             ex.printStackTrace( log );
82             throw new StatusException( "Couldn't create document", ex );
83         }
84     }
85 
86     /**
87     * Disposes document.
88     */
89     protected void cleanup( TestParameters tParam, PrintWriter log ) {
90         log.println( "    disposing xMathDoc " );
91         xMathDoc.dispose();
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.Math.XMLSettingsImporter</code><p>
98     *
99     * The math document is set as a target document for importer.
100     * Imported XML-data contains the tag which specifies new
101     * 'TopMargin' property value.
102     * After import 'TopMargin' property value of target document
103     * is checked.
104     *     Object relations created :
105     * <ul>
106     *  <li> <code>'XDocumentHandler.XMLData'</code> for
107     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
108     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
109     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
110     *  <li> <code>'TargetDocument'</code> for
111     *      {@link ifc.document._XImporter} interface </li>
112     * </ul>
113     */
114     public synchronized TestEnvironment createTestEnvironment
115             ( TestParameters Param, PrintWriter log )
116             throws StatusException {
117 
118         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
119         XInterface oObj = null;
120         final short impMargin = 67 ;
121 
122         try {
123             oObj = (XInterface)xMSF.createInstance(
124                     "com.sun.star.comp.Math.XMLSettingsImporter");
125 
126         } catch (com.sun.star.uno.Exception e) {
127             e.printStackTrace(log);
128             throw new StatusException("Unexpected exception", e);
129         }
130 
131         TestEnvironment tEnv = new TestEnvironment(oObj);
132 
133         tEnv.addObjRelation("TargetDocument",xMathDoc);
134 
135         String[][] xml = new String[][] {
136             {"start", "office:document-settings",
137                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
138                 "xmlns:config", "CDATA", "http://openoffice.org/2001/config",
139                 "xmlns:xlink", "CDATA", "http://www.w3.org/1999/xlink",
140                 "office:version", "CDATA", "1.0"
141             },
142             {"start", "office:settings"},
143             {"start", "config:config-item-set",
144                 "config:name", "CDATA", "configuration-settings"},
145 
146             {"start", "config:config-item",
147                 "config:name", "CDATA", "TopMargin",
148                 "config:type", "CDATA", "short"},
149             {"chars", String.valueOf(impMargin)},
150             {"end", "config:config-item"},
151             {"end", "config:config-item-set"},
152             {"end", "office:settings"},
153             {"end", "office:document-settings"}} ;
154 
155         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
156 
157         final PrintWriter logF = log ;
158         final XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface
159                 (XPropertySet.class, xMathDoc) ;
160 
161         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
162             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
163                 public boolean checkImport() {
164                     try {
165                         Short gMargin = (Short) xPS.getPropertyValue
166                             ("TopMargin") ;
167                         logF.println("Margin returned: " + gMargin);
168                         return impMargin == gMargin.shortValue() ;
169                     } catch (com.sun.star.uno.Exception e) {
170                         logF.println("Exception occured while checking filter :") ;
171                         e.printStackTrace(logF) ;
172                         return false ;
173                     }
174                 }
175             }) ;
176 
177         return tEnv;
178     }
179 }
180 
181