1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XAutoTextGroup.java,v $
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 ************************************************************************/
33 import lib
.MultiMethodTest
;
35 import lib
.StatusException
;
36 import util
.ValueComparer
;
38 import com
.sun
.star
.text
.XAutoTextGroup
;
39 import com
.sun
.star
.text
.XTextRange
;
42 * Testing <code>com.sun.star.text.XAutoTextGroup</code>
45 * <li><code> getTitles()</code></li>
46 * <li><code> renameByName()</code></li>
47 * <li><code> insertNewByName()</code></li>
48 * <li><code> removeByName()</code></li>
50 * This test needs the following object relations :
52 * <li> <code>'TextRange'</code> (of type <code>XTextRange</code>):
53 * the range for which an entry is added. </li>
55 * Test is multithread compilant. <p>
56 * @see com.sun.star.text.XAutoTextGroup
58 public class _XAutoTextGroup
extends MultiMethodTest
{
60 public XAutoTextGroup oObj
= null;
63 * Unique number among different interface threads.
65 protected static int uniq
= 0 ;
68 * Unique string for AutoTextEntry names among different
71 protected String str
= null ;
74 * Prefix for unique string.
77 protected static final String pref
= "XAutoTextGroup" ;
78 protected XTextRange oRange
= null;
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() {
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) {
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
130 public void _insertNewByName() {
132 boolean result
= false;
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
);
147 oObj
.insertNewByName(str
, "For " + str
, oRange
);
149 "com::sun::star::container::ElementExistsException wasn't thrown");
150 oObj
.removeByName(str
);
152 } catch (com
.sun
.star
.container
.ElementExistException ex
) {
154 } catch (com
.sun
.star
.container
.NoSuchElementException ex
) {
155 log
.println("Wrong exception was thrown :");
156 ex
.printStackTrace(log
);
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 :
171 * <li> <code> insertNewByName() </code> : the entry is
172 * inserted here. </li>
175 public void _removeByName() {
176 requiredMethod("insertNewByName()") ;
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 :
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>
204 * Has <b>OK</b> status if all three cases were completed successfully.
206 public void _renameByName() {
207 boolean result
= false;
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() ;
217 } catch (com
.sun
.star
.container
.ElementExistException e
) {
218 log
.println("Unexpected exception occured :") ;
219 e
.printStackTrace(log
);
223 oObj
.removeByName(str
);
224 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
226 oObj
.removeByName(str
+ "dup");
227 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
228 tRes
.tested("renameByName()", false);
235 oObj
.renameByName(str
, str
+ "dup", "For "+str
);
237 "com::sun::star::container::ElementExistsException wasn't thrown");
239 } catch (com
.sun
.star
.container
.ElementExistException e
) {
241 } catch (com
.sun
.star
.io
.IOException e
) {
242 log
.println("Wrong exception was thrown :");
243 e
.printStackTrace(log
);
245 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
246 log
.println("Wrong exception was thrown :");
247 e
.printStackTrace(log
);
251 oObj
.removeByName(str
);
252 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
254 oObj
.removeByName(str
+ "dup");
255 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
259 oObj
.renameByName("~"+str
,str
,str
);
261 "com::sun::star::lang::IllegalArgumentException wasn't thrown");
263 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
265 } catch (com
.sun
.star
.container
.ElementExistException e
) {
266 log
.println("Unexpected exception :") ;
267 e
.printStackTrace(log
) ;
269 } catch (com
.sun
.star
.io
.IOException e
) {
270 log
.println("Unexpected exception :") ;
271 e
.printStackTrace(log
) ;
275 oObj
.removeByName(str
);
276 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
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
);
288 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
289 log
.println("Exception occured while testing renameByName");
290 ex
.printStackTrace(log
);
292 } catch (com
.sun
.star
.io
.IOException ex
) {
293 log
.println("Exception occured while testing renameByName");
294 ex
.printStackTrace(log
);
298 oObj
.removeByName(str
);
299 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
301 oObj
.removeByName(str
+ "a");
302 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
305 tRes
.tested("renameByName()",result
);
309 } // finish class _XAutoTextGroup