merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _sw / SwXAutoTextEntry.java
blob12895798e296e973e23bcac1dc0f845abaa9da44
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SwXAutoTextEntry.java,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod._sw;
33 import java.io.PrintWriter;
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 import util.DefaultDsc;
40 import util.InstCreator;
41 import util.SOfficeFactory;
42 import com.sun.star.container.XNameAccess;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.text.XAutoTextContainer;
45 import com.sun.star.text.XAutoTextEntry;
46 import com.sun.star.text.XAutoTextGroup;
47 import com.sun.star.text.XText;
48 import com.sun.star.text.XTextDocument;
49 import com.sun.star.text.XTextRange;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
56 /**
57 * Test for object which is represented by service
58 * <code>com.sun.star.text.AutoTextEntry</code>.<p>
59 * Object implements the following interfaces :
60 * <ul>
61 * <li> <code>com::sun::star::text::XSimpleText</code></li>
62 * <li> <code>com::sun::star::text::XAutoTextEntry</code></li>
63 * <li> <code>com::sun::star::text::XTextRange</code></li>
64 * <li> <code>com::sun::star::text::XText</code></li>
65 * </ul> <p>
66 * This object test <b> is NOT </b> designed to be run in several
67 * threads concurently.
68 * @see com.sun.star.text.AutoTextEntry
69 * @see com.sun.star.text.AutoTextContainer
70 * @see com.sun.star.text.XSimpleText
71 * @see com.sun.star.text.XAutoTextEntry
72 * @see com.sun.star.text.XTextRange
73 * @see com.sun.star.text.XText
74 * @see ifc.text._XSimpleText
75 * @see ifc.text._XAutoTextEntry
76 * @see ifc.text._XTextRange
77 * @see ifc.text._XText
79 public class SwXAutoTextEntry extends TestCase {
80 XTextDocument xTextDoc;
81 XAutoTextGroup oGroup;
83 /**
84 * Creates text document.
86 protected void initialize( TestParameters tParam, PrintWriter log ) {
87 XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF();
88 SOfficeFactory SOF = SOfficeFactory.getFactory( msf );
89 try {
90 log.println( "creating a textdocument" );
91 xTextDoc = SOF.createTextDoc( null );
92 } catch ( com.sun.star.uno.Exception e ) {
93 e.printStackTrace( log );
94 throw new StatusException( "Couldn't create document", e );
98 /**
99 * Removes added element from AutoTextGroup
101 protected void cleanup( TestParameters Param, PrintWriter log) {
102 try {
103 if ( oGroup.hasByName("NewEntryName") ) {
104 log.println("Removing 'NewEntryName' element");
105 oGroup.removeByName("NewEntryName");
107 } catch ( com.sun.star.container.NoSuchElementException e ) {
108 log.println("Cannot remove TextEntry from group...");
109 e.printStackTrace(log);
111 log.println( "disposing xTextDoc " );
112 util.DesktopTools.closeDoc(xTextDoc);
117 * Creating a Testenvironment for the interfaces to be tested.
118 * Creates an instance of the service
119 * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts'
120 * group from the given container using <code>XNameAccess</code> interface,
121 * and inserts some text entry to this group. Then entry passed as test
122 * component.<p>
123 * Object relations created :
124 * <ul>
125 * <li><code>'XTEXTINFO'</code> for
126 * {@link ifc.text._XText} : creates tables 6x4</li>
127 * <li><code>'TEXTDOC'</code> for
128 * {@link ifc.text._XAutoTextEntry} : text document</li>
129 * </ul>
131 protected synchronized TestEnvironment createTestEnvironment
132 (TestParameters Param, PrintWriter log) {
134 XAutoTextEntry oEntry = null;
135 XAutoTextContainer oContainer;
136 XInterface oObj = null;
137 int n = 0;
138 int nCount = 0;
140 log.println( "creating a test environment" );
141 try {
142 XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF();
143 Object oInst = myMSF.createInstance
144 ("com.sun.star.text.AutoTextContainer");
145 oContainer = (XAutoTextContainer)
146 UnoRuntime.queryInterface(XAutoTextContainer.class,oInst);
147 } catch (com.sun.star.uno.Exception e) {
148 e.printStackTrace(log);
149 throw new StatusException("Couldn't create AutoTextContainer", e);
152 XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer);
154 String contNames[] = oContNames.getElementNames();
155 for (int i =0; i < contNames.length; i++){
156 log.println("ContainerNames[ "+ i + "]: " + contNames[i]);
159 try{
160 oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName("mytexts"));
161 } catch (com.sun.star.uno.Exception e) {
162 e.printStackTrace(log);
163 throw new StatusException("Couldn't get AutoTextGroup", e);
166 oGroup = (XAutoTextGroup) UnoRuntime.queryInterface
167 (XAutoTextGroup.class, oObj);
168 String[] oENames = oGroup.getElementNames();
169 for (int i=0; i<oENames.length; i++) {
170 log.println("AutoTextEntryNames[" + i + "]: " + oENames[i]);
173 XText oText = xTextDoc.getText();
174 oText.insertString(oText.getStart(), "New AutoText", true);
175 XTextRange oTextRange = (XTextRange) oText;
176 try {
177 if ( oGroup.hasByName("NewEntryName") ) {
178 oGroup.removeByName("NewEntryName");
179 log.println("Element 'NewEntryName' exists, removing...");
181 log.println("Adding new element 'NewEntryName' to group...");
182 oGroup.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange);
183 oEntry = (XAutoTextEntry) AnyConverter.toObject(
184 new Type(XAutoTextEntry.class),oGroup.getByName("NewEntryName"));
185 } catch ( com.sun.star.container.ElementExistException e ) {
186 e.printStackTrace(log);
187 } catch ( com.sun.star.container.NoSuchElementException e ) {
188 e.printStackTrace(log);
189 } catch ( com.sun.star.lang.WrappedTargetException e ) {
190 e.printStackTrace(log);
191 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
192 e.printStackTrace(log);
195 oObj = oEntry;
197 log.println("Trying to use XText as TextRange in the method applyTo");
198 oEntry.applyTo(oTextRange);
200 oTextRange = oText.createTextCursor();
201 log.println("Trying to use XTextCursor as TextRange in the method applyTo");
202 oEntry.applyTo(oTextRange);
204 log.println( "creating a new environment for AutoTextEntry object" );
205 TestEnvironment tEnv = new TestEnvironment( oObj );
207 // adding relation for XText
208 DefaultDsc tDsc = new DefaultDsc("com.sun.star.text.XTextContent",
209 "com.sun.star.text.TextField.DateTime");
210 log.println( " adding InstCreator object" );
211 tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) );
213 log.println( "adding TextDocument as mod relation to environment" );
214 tEnv.addObjRelation("TEXTDOC", xTextDoc);
216 return tEnv;
217 } // finish method getTestEnvironment
220 } // finish class SwXAutoTextEntry