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