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._xmloff.Draw;
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 import util.XMLTools;
38 
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.document.XExporter;
41 import com.sun.star.frame.XController;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.uno.Any;
46 import com.sun.star.uno.Exception;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import com.sun.star.xml.sax.XDocumentHandler;
51 
52 /**
53  * Test for object which is represented by service
54  * <code>com.sun.star.comp.Draw.XMLSettingsExporter</code>. <p>
55  * Object implements the following interfaces :
56  * <ul>
57  *  <li><code>com::sun::star::lang::XInitialization</code></li>
58  *  <li><code>com::sun::star::document::ExportFilter</code></li>
59  *  <li><code>com::sun::star::document::XFilter</code></li>
60  *  <li><code>com::sun::star::document::XExporter</code></li>
61  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
62  * </ul>
63  * @see com.sun.star.lang.XInitialization
64  * @see com.sun.star.document.ExportFilter
65  * @see com.sun.star.document.XFilter
66  * @see com.sun.star.document.XExporter
67  * @see com.sun.star.beans.XPropertySet
68  * @see ifc.lang._XInitialization
69  * @see ifc.document._ExportFilter
70  * @see ifc.document._XFilter
71  * @see ifc.document._XExporter
72  * @see ifc.beans._XPropertySet
73  */
74 public class XMLSettingsExporter extends TestCase {
75     XComponent xDrawDoc = null;
76 
77     /**
78     * New draw document created.
79     */
80     protected void initialize( TestParameters tParam, PrintWriter log ) {
81 
82         // get a soffice factory object
83         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
84 
85         try {
86             log.println( "creating a drawdocument" );
87             xDrawDoc = SOF.createDrawDoc(null);
88         } catch ( Exception e ) {
89             // Some exception occures.FAILED
90             e.printStackTrace( log );
91             throw new StatusException( "Couldn't create document", e );
92         }
93     }
94 
95     /**
96     * Document disposed here.
97     */
98     protected void cleanup( TestParameters tParam, PrintWriter log ) {
99         log.println( "    disposing xDrawDoc " );
100         xDrawDoc.dispose();
101     }
102 
103     /**
104     * Creating a Testenvironment for the interfaces to be tested.
105     * Creates an instance of the service
106     * <code>om.sun.star.comp.Draw.XMLSettingsExporter</code> with
107     * argument which is an implementation of <code>XDocumentHandler</code>
108     * and which can check if required tags and character data is
109     * exported. <p>
110     * The draw document is set as a source document for exporter
111     * created. Property 'IsLayerMode' set to true.
112     * This made for checking if this property is successfully exported within
113     * the document styles information.
114     *     Object relations created :
115     * <ul>
116     *  <li> <code>'MediaDescriptor'</code> for
117     *      {@link ifc.document._XFilter} interface </li>
118     *  <li> <code>'XFilter.Checker'</code> for
119     *      {@link ifc.document._XFilter} interface </li>
120     *  <li> <code>'SourceDocument'</code> for
121     *      {@link ifc.document._XExporter} interface </li>
122     * </ul>
123     */
124     protected synchronized TestEnvironment createTestEnvironment
125             (TestParameters tParam, PrintWriter log) {
126 
127         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
128         XInterface oObj = null;
129 
130         FilterChecker filter = new FilterChecker(log) ;
131         Any arg = new Any(new Type(XDocumentHandler.class),filter);
132 
133         try {
134             oObj = (XInterface) xMSF.createInstanceWithArguments(
135                 "com.sun.star.comp.Draw.XMLSettingsExporter",
136                 new Object[] {arg});
137             XExporter xEx = (XExporter)
138                 UnoRuntime.queryInterface(XExporter.class, oObj);
139             xEx.setSourceDocument(xDrawDoc);
140 
141             //set some settings
142             XModel xDrawModel = (XModel)
143                 UnoRuntime.queryInterface(XModel.class, xDrawDoc);
144             XController xController = xDrawModel.getCurrentController();
145             XPropertySet xPropSet = (XPropertySet)
146                 UnoRuntime.queryInterface(XPropertySet.class, xController);
147             xPropSet.setPropertyValue("IsLayerMode", new Boolean("true"));
148 
149         } catch (com.sun.star.uno.Exception e) {
150             e.printStackTrace(log) ;
151             throw new StatusException("Can't create component.", e) ;
152         }
153 
154         // Checking Head Tag existance and that property has changed
155         filter.addTag(new XMLTools.Tag ("office:document-settings"));
156         filter.addTagEnclosed(
157             new XMLTools.Tag ("office:settings"),
158             new XMLTools.Tag ("office:document-settings") );
159         filter.addTagEnclosed(
160             new XMLTools.Tag ("config:config-item-set"),
161             new XMLTools.Tag ("office:settings") );
162         filter.addTagEnclosed(
163             new XMLTools.Tag ("config:config-item-map-indexed"),
164             new XMLTools.Tag ("config:config-item-set") );
165         filter.addCharactersEnclosed( "true",
166             new XMLTools.Tag( "config:config-item",
167                               "config:name",
168                               "IsLayerMode" ));
169 
170         // create testobject here
171         log.println( "creating a new environment" );
172         TestEnvironment tEnv = new TestEnvironment( oObj );
173 
174         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
175             new String[] {"FilterName"},
176             new Object[] {"sdraw: StarOffice XML (Draw)"}));
177         tEnv.addObjRelation("SourceDocument", xDrawDoc);
178         tEnv.addObjRelation("XFilter.Checker", filter);
179         return tEnv;
180     }
181 
182     /**
183      * This class checks the XML for tags and data required and returns
184      * checking result to <code>XFilter</code> interface test. All
185      * the information about errors occured in XML data is written
186      * to log specified.
187      * @see ifc.document._XFilter
188      */
189     protected class FilterChecker extends XMLTools.XMLChecker
190         implements ifc.document._XFilter.FilterChecker {
191 
192         /**
193          * Creates a class which will write information
194          * into log specified.
195          */
196         public FilterChecker(PrintWriter log) {
197             super(log, false) ;
198         }
199 
200         /**
201          * <code>_XFilter.FilterChecker</code> interface method
202          * which returns the result of XML checking.
203          * @return <code>true</code> if the XML data exported was
204          * valid (i.e. all necessary tags and character data exists),
205          * <code>false</code> if some errors occured.
206          */
207         public boolean checkFilter() {
208             return check();
209         }
210     }
211 }
212