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 import util.XMLTools; 38 39 import com.sun.star.document.XDocumentInfo; 40 import com.sun.star.document.XDocumentInfoSupplier; 41 import com.sun.star.document.XExporter; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.lang.XMultiServiceFactory; 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.xml.sax.XDocumentHandler; 49 50 /** 51 * Test for object which is represented by service 52 * <code>com.sun.star.comp.Math.XMLExporter</code>. <p> 53 * Object implements the following interfaces : 54 * <ul> 55 * <li><code>com::sun::star::lang::XInitialization</code></li> 56 * <li><code>com::sun::star::document::ExportFilter</code></li> 57 * <li><code>com::sun::star::document::XFilter</code></li> 58 * <li><code>com::sun::star::document::XExporter</code></li> 59 * <li><code>com::sun::star::beans::XPropertySet</code></li> 60 * </ul> 61 * @see com.sun.star.lang.XInitialization 62 * @see com.sun.star.document.ExportFilter 63 * @see com.sun.star.document.XFilter 64 * @see com.sun.star.document.XExporter 65 * @see com.sun.star.beans.XPropertySet 66 * @see ifc.lang._XInitialization 67 * @see ifc.document._ExportFilter 68 * @see ifc.document._XFilter 69 * @see ifc.document._XExporter 70 * @see ifc.beans._XPropertySet 71 */ 72 public class XMLMetaExporter extends TestCase { 73 XComponent xMathDoc; 74 75 /** 76 * New math 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 math document" ); 83 xMathDoc = SOF.createMathDoc(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 * Document disposed here. 93 */ 94 protected void cleanup( TestParameters tParam, PrintWriter log ) { 95 log.println( " disposing xMathDoc " ); 96 xMathDoc.dispose(); 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.Math.XMLExporter</code> with 103 * argument which is an implementation of <code>XDocumentHandler</code> 104 * and which can check if required tags and character data is 105 * exported. <p> 106 * The math document is set as a source document for exporter 107 * created. A new user info field inserted into document. This made 108 * for checking if this info field is successfully exported within 109 * the document content. 110 * Object relations created : 111 * <ul> 112 * <li> <code>'MediaDescriptor'</code> for 113 * {@link ifc.document._XFilter} interface </li> 114 * <li> <code>'XFilter.Checker'</code> for 115 * {@link ifc.document._XFilter} interface </li> 116 * <li> <code>'SourceDocument'</code> for 117 * {@link ifc.document._XExporter} interface </li> 118 * </ul> 119 */ 120 protected TestEnvironment createTestEnvironment 121 (TestParameters tParam, PrintWriter log) { 122 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ; 123 XInterface oObj = null; 124 final String expName = "XMLMetaExporterName" ; 125 final String expValue = "XMLMetaExporterValue" ; 126 127 FilterChecker filter = new FilterChecker(log); 128 Any arg = new Any(new Type(XDocumentHandler.class), filter); 129 130 try { 131 oObj = (XInterface) xMSF.createInstanceWithArguments( 132 "com.sun.star.comp.Math.XMLMetaExporter", new Object[] {arg}); 133 XExporter xEx = (XExporter) UnoRuntime.queryInterface 134 (XExporter.class,oObj); 135 xEx.setSourceDocument(xMathDoc); 136 137 // setting a new name and value for user info field 138 XDocumentInfoSupplier xDocInfoSup = (XDocumentInfoSupplier) 139 UnoRuntime.queryInterface(XDocumentInfoSupplier.class, xMathDoc) ; 140 XDocumentInfo xDocInfo = xDocInfoSup.getDocumentInfo() ; 141 xDocInfo.setUserFieldName((short) 0, expName) ; 142 xDocInfo.setUserFieldValue((short) 0, expValue) ; 143 } catch (com.sun.star.uno.Exception e) { 144 e.printStackTrace(log) ; 145 throw new StatusException("Can't create component.", e) ; 146 } 147 148 // checking tags required 149 filter.addTag(new XMLTools.Tag("office:document-meta")) ; 150 filter.addCharactersEnclosed(expValue, 151 new XMLTools.Tag("meta:user-defined", "meta:name", expName)) ; 152 153 // create testobject here 154 log.println( "creating a new environment" ); 155 TestEnvironment tEnv = new TestEnvironment( oObj ); 156 157 158 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor( 159 new String[] {"FilterName"}, 160 new Object[] {"smath: StarOffice XML (Math)"})); 161 tEnv.addObjRelation("SourceDocument",xMathDoc); 162 tEnv.addObjRelation("XFilter.Checker", filter) ; 163 164 log.println("Implementation Name: "+util.utils.getImplName(oObj)); 165 166 return tEnv; 167 } 168 169 /** 170 * This class checks the XML for tags and data required and returns 171 * checking result to <code>XFilter</code> interface test. All 172 * the information about errors occured in XML data is written 173 * to log specified. 174 * @see ifc.document._XFilter 175 */ 176 protected class FilterChecker extends XMLTools.XMLChecker 177 implements ifc.document._XFilter.FilterChecker { 178 179 /** 180 * Creates a class which will write information 181 * into log specified. 182 */ 183 public FilterChecker(PrintWriter log) { 184 super(log, true) ; 185 } 186 /** 187 * <code>_XFilter.FilterChecker</code> interface method 188 * which returns the result of XML checking. 189 * @return <code>true</code> if the XML data exported was 190 * valid (i.e. all necessary tags and character data exists), 191 * <code>false</code> if some errors occured. 192 */ 193 public boolean checkFilter() { 194 return check(); 195 } 196 } 197 } // finish class TestCase 198 199