2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import lib
.MultiMethodTest
;
23 import lib
.StatusException
;
24 import util
.ValueComparer
;
26 import com
.sun
.star
.text
.XAutoTextGroup
;
27 import com
.sun
.star
.text
.XTextRange
;
30 * Testing <code>com.sun.star.text.XAutoTextGroup</code>
33 * <li><code> getTitles()</code></li>
34 * <li><code> renameByName()</code></li>
35 * <li><code> insertNewByName()</code></li>
36 * <li><code> removeByName()</code></li>
38 * This test needs the following object relations :
40 * <li> <code>'TextRange'</code> (of type <code>XTextRange</code>):
41 * the range for which an entry is added. </li>
43 * Test is multithread compilant. <p>
44 * @see com.sun.star.text.XAutoTextGroup
46 public class _XAutoTextGroup
extends MultiMethodTest
{
48 public XAutoTextGroup oObj
= null;
51 * Unique number among different interface threads.
53 protected static int uniq
= 0 ;
56 * Unique string for AutoTextEntry names among different
59 protected String str
= null ;
62 * Prefix for unique string.
65 protected static final String pref
= "XAutoTextGroup" ;
66 protected XTextRange oRange
= null;
69 * Constructs a unique string for current interface thread
70 * for naming purposes. All old entries which names are
71 * started with prefix used for entry names, are deleted
72 * from the group (they can remain after previous unsuccessfull
73 * test runs). The relation is obtained.
75 * @throws StatusException if the relation is not found.
77 public void before() {
79 String
[] names
= oObj
.getElementNames() ;
80 for (int i
= 0; i
< names
.length
; i
++) {
81 log
.println(" " + names
[i
]);
82 if (names
[i
].toUpperCase().indexOf(pref
.toUpperCase()) > 0) {
84 log
.println(" ... removing ...");
85 oObj
.removeByName(names
[i
]) ;
86 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
87 log
.println("Element '" + names
[i
] + "' not found.");
92 oRange
= (XTextRange
) tEnv
.getObjRelation("TextRange");
94 throw new StatusException(Status
.failed("No relation found")) ;
99 * Test calls the method. <p>
100 * Has <b> OK </b> status if the method returns not
101 * <code>null</code> value.
103 public void _getTitles() {
105 String
[] titles
= oObj
.getTitles();
106 tRes
.tested("getTitles()",titles
!= null);
110 * Firsts inserts a new <code>AutoTextEntry</code> using a range
111 * from relation, entry titles are checked before and after
112 * insertion, second tries to add an entry with the same name. <p>
114 * Has <b>OK</b> status if in the first case titles are changed,
115 * and in the second case <code>ElementExistException</code> is
118 public void _insertNewByName() {
120 boolean result
= false;
123 String
[] before
= oObj
.getTitles();
124 oObj
.insertNewByName(str
, "For " + str
,oRange
);
125 String
[] after
= oObj
.getTitles();
126 result
= !util
.ValueComparer
.equalValue(before
, after
);
128 catch (com
.sun
.star
.container
.ElementExistException ex
) {
129 log
.println("Exception occurred while testing insertNewByName");
130 ex
.printStackTrace(log
);
135 oObj
.insertNewByName(str
, "For " + str
, oRange
);
137 "com::sun::star::container::ElementExistsException wasn't thrown");
138 oObj
.removeByName(str
);
140 } catch (com
.sun
.star
.container
.ElementExistException ex
) {
142 } catch (com
.sun
.star
.container
.NoSuchElementException ex
) {
143 log
.println("Wrong exception was thrown :");
144 ex
.printStackTrace(log
);
148 tRes
.tested("insertNewByName()",result
);
153 * Removes <code>AutoTextEntry</code> added before and checks
154 * titles of the group before and after removing. <p>
155 * Has <b> OK </b> status if titles are not equal before and after
156 * removing and no exceptions were thrown. <p>
157 * The following method tests are to be completed successfully before :
159 * <li> <code> insertNewByName() </code> : the entry is
160 * inserted here. </li>
163 public void _removeByName() {
164 requiredMethod("insertNewByName()") ;
167 String
[] before
= oObj
.getTitles();
168 oObj
.removeByName(str
);
169 String
[] after
= oObj
.getTitles();
170 tRes
.tested("removeByName()",
171 !ValueComparer
.equalValue(before
,after
));
173 catch (com
.sun
.star
.container
.NoSuchElementException ex
) {
174 log
.println("Exception occurred while testing removeByName");
175 ex
.printStackTrace(log
);
176 tRes
.tested("removeByName()",false);
181 * Three cases are tested here :
183 * <li> Trying to rename an entry to a name, which already
184 * exists in the group. <code>ElementExistException</code>
185 * must be thrown. </li>
186 * <li> Trying to rename an element with non-existing name.
187 * <code>IllegalArgumentException</code> must be thrown.</li>
188 * <li> The normal situation : no exceptions must be thrown
189 * and element with a new name must arise. </li>
192 * Has <b>OK</b> status if all three cases were completed successfully.
194 public void _renameByName() {
195 boolean result
= false;
199 oObj
.getElementNames();
200 oObj
.insertNewByName(str
,"For " + str
,oRange
);
201 oObj
.insertNewByName(str
+ "dup","For " + str
,oRange
);
203 oObj
.getElementNames();
205 } catch (com
.sun
.star
.container
.ElementExistException e
) {
206 log
.println("Unexpected exception occurred :") ;
207 e
.printStackTrace(log
);
211 oObj
.removeByName(str
);
212 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
214 oObj
.removeByName(str
+ "dup");
215 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
216 tRes
.tested("renameByName()", false);
223 oObj
.renameByName(str
, str
+ "dup", "For "+str
);
225 "com::sun::star::container::ElementExistsException wasn't thrown");
227 } catch (com
.sun
.star
.container
.ElementExistException e
) {
229 } catch (com
.sun
.star
.io
.IOException e
) {
230 log
.println("Wrong exception was thrown :");
231 e
.printStackTrace(log
);
233 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
234 log
.println("Wrong exception was thrown :");
235 e
.printStackTrace(log
);
239 oObj
.removeByName(str
);
240 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
242 oObj
.removeByName(str
+ "dup");
243 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
247 oObj
.renameByName("~"+str
,str
,str
);
249 "com::sun::star::lang::IllegalArgumentException wasn't thrown");
251 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
253 } catch (com
.sun
.star
.container
.ElementExistException e
) {
254 log
.println("Unexpected exception :") ;
255 e
.printStackTrace(log
) ;
257 } catch (com
.sun
.star
.io
.IOException e
) {
258 log
.println("Unexpected exception :") ;
259 e
.printStackTrace(log
) ;
263 oObj
.removeByName(str
);
264 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
268 oObj
.insertNewByName(str
, "For " + str
, oRange
);
270 oObj
.renameByName(str
,str
+"a",str
+"b");
271 result
&= oObj
.hasByName(str
+ "a");
272 } catch (com
.sun
.star
.container
.ElementExistException ex
) {
273 log
.println("Exception occurred while testing renameByName");
274 ex
.printStackTrace(log
);
276 } catch (com
.sun
.star
.lang
.IllegalArgumentException ex
) {
277 log
.println("Exception occurred while testing renameByName");
278 ex
.printStackTrace(log
);
280 } catch (com
.sun
.star
.io
.IOException ex
) {
281 log
.println("Exception occurred while testing renameByName");
282 ex
.printStackTrace(log
);
286 oObj
.removeByName(str
);
287 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
289 oObj
.removeByName(str
+ "a");
290 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {}
293 tRes
.tested("renameByName()",result
);
297 } // finish class _XAutoTextGroup