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.DefaultDsc; 37 import util.InstCreator; 38 import util.SOfficeFactory; 39 import com.sun.star.container.XNameAccess; 40 import com.sun.star.lang.XMultiServiceFactory; 41 import com.sun.star.text.XAutoTextContainer; 42 import com.sun.star.text.XAutoTextEntry; 43 import com.sun.star.text.XAutoTextGroup; 44 import com.sun.star.text.XText; 45 import com.sun.star.text.XTextDocument; 46 import com.sun.star.text.XTextRange; 47 import com.sun.star.uno.AnyConverter; 48 import com.sun.star.uno.Type; 49 import com.sun.star.uno.UnoRuntime; 50 import com.sun.star.uno.XInterface; 51 52 53 /** 54 * Test for object which is represented by service 55 * <code>com.sun.star.text.AutoTextEntry</code>.<p> 56 * Object implements the following interfaces : 57 * <ul> 58 * <li> <code>com::sun::star::text::XSimpleText</code></li> 59 * <li> <code>com::sun::star::text::XAutoTextEntry</code></li> 60 * <li> <code>com::sun::star::text::XTextRange</code></li> 61 * <li> <code>com::sun::star::text::XText</code></li> 62 * </ul> <p> 63 * This object test <b> is NOT </b> designed to be run in several 64 * threads concurently. 65 * @see com.sun.star.text.AutoTextEntry 66 * @see com.sun.star.text.AutoTextContainer 67 * @see com.sun.star.text.XSimpleText 68 * @see com.sun.star.text.XAutoTextEntry 69 * @see com.sun.star.text.XTextRange 70 * @see com.sun.star.text.XText 71 * @see ifc.text._XSimpleText 72 * @see ifc.text._XAutoTextEntry 73 * @see ifc.text._XTextRange 74 * @see ifc.text._XText 75 */ 76 public class SwXAutoTextEntry extends TestCase { 77 XTextDocument xTextDoc; 78 XAutoTextGroup oGroup; 79 80 /** 81 * Creates text document. 82 */ 83 protected void initialize( TestParameters tParam, PrintWriter log ) { 84 XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF(); 85 SOfficeFactory SOF = SOfficeFactory.getFactory( msf ); 86 try { 87 log.println( "creating a textdocument" ); 88 xTextDoc = SOF.createTextDoc( null ); 89 } catch ( com.sun.star.uno.Exception e ) { 90 e.printStackTrace( log ); 91 throw new StatusException( "Couldn't create document", e ); 92 } 93 } 94 95 /** 96 * Removes added element from AutoTextGroup 97 */ 98 protected void cleanup( TestParameters Param, PrintWriter log) { 99 try { 100 if ( oGroup.hasByName("NewEntryName") ) { 101 log.println("Removing 'NewEntryName' element"); 102 oGroup.removeByName("NewEntryName"); 103 } 104 } catch ( com.sun.star.container.NoSuchElementException e ) { 105 log.println("Cannot remove TextEntry from group..."); 106 e.printStackTrace(log); 107 } 108 log.println( "disposing xTextDoc " ); 109 util.DesktopTools.closeDoc(xTextDoc); 110 } 111 112 113 /** 114 * Creating a Testenvironment for the interfaces to be tested. 115 * Creates an instance of the service 116 * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts' 117 * group from the given container using <code>XNameAccess</code> interface, 118 * and inserts some text entry to this group. Then entry passed as test 119 * component.<p> 120 * Object relations created : 121 * <ul> 122 * <li><code>'XTEXTINFO'</code> for 123 * {@link ifc.text._XText} : creates tables 6x4</li> 124 * <li><code>'TEXTDOC'</code> for 125 * {@link ifc.text._XAutoTextEntry} : text document</li> 126 * </ul> 127 */ 128 protected synchronized TestEnvironment createTestEnvironment 129 (TestParameters Param, PrintWriter log) { 130 131 XAutoTextEntry oEntry = null; 132 XAutoTextContainer oContainer; 133 XInterface oObj = null; 134 int n = 0; 135 int nCount = 0; 136 137 log.println( "creating a test environment" ); 138 try { 139 XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF(); 140 Object oInst = myMSF.createInstance 141 ("com.sun.star.text.AutoTextContainer"); 142 oContainer = (XAutoTextContainer) 143 UnoRuntime.queryInterface(XAutoTextContainer.class,oInst); 144 } catch (com.sun.star.uno.Exception e) { 145 e.printStackTrace(log); 146 throw new StatusException("Couldn't create AutoTextContainer", e); 147 } 148 149 XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer); 150 151 String contNames[] = oContNames.getElementNames(); 152 for (int i =0; i < contNames.length; i++){ 153 log.println("ContainerNames[ "+ i + "]: " + contNames[i]); 154 } 155 156 try{ 157 oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName("mytexts")); 158 } catch (com.sun.star.uno.Exception e) { 159 e.printStackTrace(log); 160 throw new StatusException("Couldn't get AutoTextGroup", e); 161 } 162 163 oGroup = (XAutoTextGroup) UnoRuntime.queryInterface 164 (XAutoTextGroup.class, oObj); 165 String[] oENames = oGroup.getElementNames(); 166 for (int i=0; i<oENames.length; i++) { 167 log.println("AutoTextEntryNames[" + i + "]: " + oENames[i]); 168 } 169 170 XText oText = xTextDoc.getText(); 171 oText.insertString(oText.getStart(), "New AutoText", true); 172 XTextRange oTextRange = (XTextRange) oText; 173 try { 174 if ( oGroup.hasByName("NewEntryName") ) { 175 oGroup.removeByName("NewEntryName"); 176 log.println("Element 'NewEntryName' exists, removing..."); 177 } 178 log.println("Adding new element 'NewEntryName' to group..."); 179 oGroup.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange); 180 oEntry = (XAutoTextEntry) AnyConverter.toObject( 181 new Type(XAutoTextEntry.class),oGroup.getByName("NewEntryName")); 182 } catch ( com.sun.star.container.ElementExistException e ) { 183 e.printStackTrace(log); 184 } catch ( com.sun.star.container.NoSuchElementException e ) { 185 e.printStackTrace(log); 186 } catch ( com.sun.star.lang.WrappedTargetException e ) { 187 e.printStackTrace(log); 188 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 189 e.printStackTrace(log); 190 } 191 192 oObj = oEntry; 193 194 log.println("Trying to use XText as TextRange in the method applyTo"); 195 oEntry.applyTo(oTextRange); 196 197 oTextRange = oText.createTextCursor(); 198 log.println("Trying to use XTextCursor as TextRange in the method applyTo"); 199 oEntry.applyTo(oTextRange); 200 201 log.println( "creating a new environment for AutoTextEntry object" ); 202 TestEnvironment tEnv = new TestEnvironment( oObj ); 203 204 // adding relation for XText 205 DefaultDsc tDsc = new DefaultDsc("com.sun.star.text.XTextContent", 206 "com.sun.star.text.TextField.DateTime"); 207 log.println( " adding InstCreator object" ); 208 tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) ); 209 210 log.println( "adding TextDocument as mod relation to environment" ); 211 tEnv.addObjRelation("TEXTDOC", xTextDoc); 212 213 return tEnv; 214 } // finish method getTestEnvironment 215 216 217 } // finish class SwXAutoTextEntry 218