merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / text / _XAutoTextGroup.java
blobf6dc824c10557911a49bd1fac7fe71ff5b56840d
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: _XAutoTextGroup.java,v $
10 * $Revision: 1.4 $
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 ifc.text;
33 import lib.MultiMethodTest;
34 import lib.Status;
35 import lib.StatusException;
36 import util.ValueComparer;
38 import com.sun.star.text.XAutoTextGroup;
39 import com.sun.star.text.XTextRange;
41 /**
42 * Testing <code>com.sun.star.text.XAutoTextGroup</code>
43 * interface methods :
44 * <ul>
45 * <li><code> getTitles()</code></li>
46 * <li><code> renameByName()</code></li>
47 * <li><code> insertNewByName()</code></li>
48 * <li><code> removeByName()</code></li>
49 * </ul> <p>
50 * This test needs the following object relations :
51 * <ul>
52 * <li> <code>'TextRange'</code> (of type <code>XTextRange</code>):
53 * the range for which an entry is added. </li>
54 * <ul> <p>
55 * Test is multithread compilant. <p>
56 * @see com.sun.star.text.XAutoTextGroup
58 public class _XAutoTextGroup extends MultiMethodTest {
60 public XAutoTextGroup oObj = null;
62 /**
63 * Unique number among different interface threads.
65 protected static int uniq = 0 ;
67 /**
68 * Unique string for AutoTextEntry names among different
69 * threads.
71 protected String str = null ;
73 /**
74 * Prefix for unique string.
75 * @see #str
77 protected static final String pref = "XAutoTextGroup" ;
78 protected XTextRange oRange = null;
80 /**
81 * Constructs a unique string for current interface thread
82 * for naming purposes. All old entries which names are
83 * started with prefix used for entry names, are deleted
84 * from the group (they can remain after previous unsuccessfull
85 * test runs). The relation is obtained.
87 * @throws StatusException if the relation is not found.
89 public void before() {
90 str = pref + uniq++ ;
91 String[] names = oObj.getElementNames() ;
92 for (int i = 0; i < names.length; i++) {
93 log.println(" " + names[i]);
94 if (names[i].toUpperCase().indexOf(pref.toUpperCase()) > 0) {
95 try {
96 log.println(" ... removing ...");
97 oObj.removeByName(names[i]) ;
98 } catch (com.sun.star.container.NoSuchElementException e) {
99 log.println("Element '" + names[i] + "' not found.");
104 oRange = (XTextRange) tEnv.getObjRelation("TextRange");
105 if (oRange == null) {
106 throw new StatusException(Status.failed("No relation found")) ;
111 * Test calls the method. <p>
112 * Has <b> OK </b> status if the method returns not
113 * <code>null</code> value.
115 public void _getTitles() {
117 String[] titles = oObj.getTitles();
118 tRes.tested("getTitles()",titles != null);
122 * Firsts inserts a new <code>AutoTextEntry</code> using a range
123 * from relation, entry titles are checked before and after
124 * insertion, second tries to add an entry with the same name. <p>
126 * Has <b>OK</b> status if in the first case titles are changed,
127 * and in the second case <code>ElementExistException</code> is
128 * thrown.
130 public void _insertNewByName() {
132 boolean result = false;
134 try {
135 String[] before = oObj.getTitles();
136 oObj.insertNewByName(str, "For " + str,oRange);
137 String[] after = oObj.getTitles();
138 result = !util.ValueComparer.equalValue(before, after);
140 catch (com.sun.star.container.ElementExistException ex) {
141 log.println("Exception occured while testing insertNewByName");
142 ex.printStackTrace(log);
143 result = false;
146 try {
147 oObj.insertNewByName(str, "For " + str, oRange);
148 log.println(
149 "com::sun::star::container::ElementExistsException wasn't thrown");
150 oObj.removeByName(str);
151 result &= false;
152 } catch (com.sun.star.container.ElementExistException ex) {
153 result &= true;
154 } catch (com.sun.star.container.NoSuchElementException ex) {
155 log.println("Wrong exception was thrown :");
156 ex.printStackTrace(log);
157 result &= false;
160 tRes.tested("insertNewByName()",result);
165 * Removes <code>AutoTextEntry</code> added before and checks
166 * titles of the group before and after removing. <p>
167 * Has <b> OK </b> status if titles are not equal before and after
168 * removing and no exceptions were thrown. <p>
169 * The following method tests are to be completed successfully before :
170 * <ul>
171 * <li> <code> insertNewByName() </code> : the entry is
172 * inserted here. </li>
173 * </ul>
175 public void _removeByName() {
176 requiredMethod("insertNewByName()") ;
178 try {
179 String[] before = oObj.getTitles();
180 oObj.removeByName(str);
181 String[] after = oObj.getTitles();
182 tRes.tested("removeByName()",
183 !ValueComparer.equalValue(before,after));
185 catch (com.sun.star.container.NoSuchElementException ex) {
186 log.println("Exception occured while testing removeByName");
187 ex.printStackTrace(log);
188 tRes.tested("removeByName()",false);
193 * Three cases are tested here :
194 * <ol>
195 * <li> Trying to rename an entry to a name, which already
196 * exists in the group. <code>ElementExistException</code>
197 * must be thrown. </li>
198 * <li> Trying to rename an element with non-existing name.
199 * <code>IllegalArgumentException</code> must be thrown.</li>
200 * <li> The normal situation : no exceptions must be thrown
201 * and element with a new name must arise. </li>
202 * </ol>
204 * Has <b>OK</b> status if all three cases were completed successfully.
206 public void _renameByName() {
207 boolean result = false;
209 try {
210 String[] tit = oObj.getTitles() ;
211 String[] names = oObj.getElementNames() ;
212 oObj.insertNewByName(str,"For " + str,oRange);
213 oObj.insertNewByName(str + "dup","For " + str,oRange);
214 tit = oObj.getTitles() ;
215 names = oObj.getElementNames() ;
216 result = true;
217 } catch (com.sun.star.container.ElementExistException e) {
218 log.println("Unexpected exception occured :") ;
219 e.printStackTrace(log);
220 } finally {
221 if (!result) {
222 try {
223 oObj.removeByName(str);
224 } catch (com.sun.star.container.NoSuchElementException e) {}
225 try {
226 oObj.removeByName(str + "dup");
227 } catch (com.sun.star.container.NoSuchElementException e) {}
228 tRes.tested("renameByName()", false);
229 return;
234 try {
235 oObj.renameByName(str, str + "dup", "For "+str);
236 log.println(
237 "com::sun::star::container::ElementExistsException wasn't thrown");
238 result = false;
239 } catch (com.sun.star.container.ElementExistException e) {
240 result = true;
241 } catch (com.sun.star.io.IOException e) {
242 log.println("Wrong exception was thrown :");
243 e.printStackTrace(log);
244 result = false;
245 } catch (com.sun.star.lang.IllegalArgumentException e) {
246 log.println("Wrong exception was thrown :");
247 e.printStackTrace(log);
248 result = false;
249 } finally {
250 try {
251 oObj.removeByName(str);
252 } catch (com.sun.star.container.NoSuchElementException e) {}
253 try {
254 oObj.removeByName(str + "dup");
255 } catch (com.sun.star.container.NoSuchElementException e) {}
258 try {
259 oObj.renameByName("~"+str,str,str);
260 log.println(
261 "com::sun::star::lang::IllegalArgumentException wasn't thrown");
262 result &= false;
263 } catch (com.sun.star.lang.IllegalArgumentException ex) {
264 result &= true;
265 } catch (com.sun.star.container.ElementExistException e) {
266 log.println("Unexpected exception :") ;
267 e.printStackTrace(log) ;
268 result = false ;
269 } catch (com.sun.star.io.IOException e) {
270 log.println("Unexpected exception :") ;
271 e.printStackTrace(log) ;
272 result = false ;
273 } finally {
274 try {
275 oObj.removeByName(str);
276 } catch (com.sun.star.container.NoSuchElementException e) {}
279 try {
280 oObj.insertNewByName(str, "For " + str, oRange);
282 oObj.renameByName(str,str+"a",str+"b");
283 result &= oObj.hasByName(str + "a");
284 } catch (com.sun.star.container.ElementExistException ex) {
285 log.println("Exception occured while testing renameByName");
286 ex.printStackTrace(log);
287 result &=false;
288 } catch (com.sun.star.lang.IllegalArgumentException ex) {
289 log.println("Exception occured while testing renameByName");
290 ex.printStackTrace(log);
291 result &=false;
292 } catch (com.sun.star.io.IOException ex) {
293 log.println("Exception occured while testing renameByName");
294 ex.printStackTrace(log);
295 result &=false;
296 } finally {
297 try {
298 oObj.removeByName(str);
299 } catch (com.sun.star.container.NoSuchElementException e) {}
300 try {
301 oObj.removeByName(str + "a");
302 } catch (com.sun.star.container.NoSuchElementException e) {}
305 tRes.tested("renameByName()",result);
309 } // finish class _XAutoTextGroup