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.container.XEnumerationAccess; 40 import com.sun.star.lang.XMultiServiceFactory; 41 import com.sun.star.text.XDependentTextField; 42 import com.sun.star.text.XText; 43 import com.sun.star.text.XTextContent; 44 import com.sun.star.text.XTextCursor; 45 import com.sun.star.text.XTextDocument; 46 import com.sun.star.text.XTextFieldsSupplier; 47 import com.sun.star.uno.UnoRuntime; 48 import com.sun.star.uno.XInterface; 49 50 51 /** 52 * Object implements the following interfaces : 53 * <ul> 54 * <li> <code>com::sun::star::container::XEnumeration</code></li> 55 * </ul> <p> 56 * This object test <b> is NOT </b> designed to be run in several 57 * threads concurently. 58 * @see com.sun.star.container.XEnumeration 59 * @see ifc.container._XEnumeration 60 */ 61 public class SwXFieldEnumeration extends TestCase { 62 XTextDocument xTextDoc; 63 64 /** 65 * Creates text document. 66 */ 67 protected void initialize( TestParameters tParam, PrintWriter log ) { 68 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 69 try { 70 log.println( "creating a textdocument" ); 71 xTextDoc = SOF.createTextDoc( null ); 72 } catch ( com.sun.star.uno.Exception e ) { 73 e.printStackTrace( log ); 74 throw new StatusException( "Couldn't create document", e ); 75 } 76 } 77 78 /** 79 * Disposes text document. 80 */ 81 protected void cleanup( TestParameters tParam, PrintWriter log ) { 82 log.println( " disposing xTextDoc " ); 83 util.DesktopTools.closeDoc(xTextDoc); 84 } 85 86 /** 87 * Creating a Testenvironment for the interfaces to be tested. FieldMaster 88 * object is created and attached to Bibliography database. After setting 89 * properties of created FieldMaster object, it is attached to the TextField 90 * object. Then TextField is inserted as TextContent to the text document. 91 * At the end, TextFields of text document are obtained and 92 * enumeration of these fields is created.<br> 93 * Creates instances of the services: 94 * <code>com.sun.star.text.FieldMaster.Database</code>, 95 * <code>com.sun.star.text.TextField.Database</code>.<br> 96 * Object relations created : 97 * <ul> 98 * <li> <code>'ENUM'</code> for 99 * {@link ifc.container._XEnumeration} : text fields</li> 100 * </ul> 101 */ 102 public TestEnvironment createTestEnvironment( 103 TestParameters tParam, PrintWriter log ) throws StatusException { 104 XInterface oObj = null; 105 Object FieldMaster = null; 106 XPropertySet PFieldMaster = null; 107 XDependentTextField xTF = null; 108 XEnumerationAccess xFEA = null; 109 XText the_Text; 110 XTextCursor the_Cursor; 111 XTextContent the_Field; 112 113 log.println( "creating a test environment" ); 114 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) 115 UnoRuntime.queryInterface( XMultiServiceFactory.class, xTextDoc ); 116 117 try { 118 FieldMaster = oDocMSF.createInstance 119 ( "com.sun.star.text.FieldMaster.Database" ); 120 PFieldMaster = (XPropertySet) UnoRuntime.queryInterface 121 (XPropertySet.class,(XInterface) FieldMaster); 122 oObj = (XInterface) 123 oDocMSF.createInstance("com.sun.star.text.TextField.Database"); 124 xTF = (XDependentTextField) 125 UnoRuntime.queryInterface(XDependentTextField.class,oObj); 126 } catch ( com.sun.star.uno.Exception e ) { 127 e.printStackTrace(log); 128 } 129 130 try { 131 PFieldMaster.setPropertyValue("DataBaseName","Bibliography"); 132 PFieldMaster.setPropertyValue("DataTableName","biblio"); 133 PFieldMaster.setPropertyValue("DataColumnName","Address"); 134 } catch ( com.sun.star.lang.WrappedTargetException e ) { 135 log.println("Error: can't set PropertyValue to a FieldMaster"); 136 e.printStackTrace(log); 137 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 138 log.println("Error: can't set PropertyValue to a FieldMaster"); 139 e.printStackTrace(log); 140 } catch ( com.sun.star.beans.UnknownPropertyException e ) { 141 log.println("Error: can't set PropertyValue to a FieldMaster"); 142 e.printStackTrace(log); 143 } catch ( com.sun.star.beans.PropertyVetoException e ) { 144 log.println("Error: can't set PropertyValue to a FieldMaster"); 145 e.printStackTrace(log); 146 } 147 148 the_Text = xTextDoc.getText(); 149 the_Cursor = the_Text.createTextCursor(); 150 the_Field = (XTextContent) 151 UnoRuntime.queryInterface(XTextContent.class, oObj); 152 153 try { 154 xTF.attachTextFieldMaster(PFieldMaster); 155 the_Text.insertTextContent(the_Cursor,the_Field,false); 156 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 157 e.printStackTrace(log); 158 throw new StatusException( "Couldn't create TextField", e ); 159 } 160 161 // create testobject here 162 XTextFieldsSupplier oTFS = (XTextFieldsSupplier) 163 UnoRuntime.queryInterface( XTextFieldsSupplier.class, xTextDoc ); 164 xFEA = oTFS.getTextFields(); 165 oObj = oTFS.getTextFields().createEnumeration(); 166 167 log.println( "creating a new environment for FieldEnumeration object" ); 168 TestEnvironment tEnv = new TestEnvironment( oObj ); 169 170 tEnv.addObjRelation("ENUM", xFEA); 171 172 return tEnv; 173 } // finish method getTestEnvironment 174 175 } // finish class SwXFieldEnumeration 176 177