Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / text / _XAutoTextGroup.java
blobb99fab13b1032533a12870963d43d3a2254190d5
1 /*
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 .
19 package ifc.text;
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
24 import util.ValueComparer;
26 import com.sun.star.text.XAutoTextGroup;
27 import com.sun.star.text.XTextRange;
29 /**
30 * Testing <code>com.sun.star.text.XAutoTextGroup</code>
31 * interface methods :
32 * <ul>
33 * <li><code> getTitles()</code></li>
34 * <li><code> renameByName()</code></li>
35 * <li><code> insertNewByName()</code></li>
36 * <li><code> removeByName()</code></li>
37 * </ul> <p>
38 * This test needs the following object relations :
39 * <ul>
40 * <li> <code>'TextRange'</code> (of type <code>XTextRange</code>):
41 * the range for which an entry is added. </li>
42 * <ul> <p>
43 * Test is multithread compliant. <p>
44 * @see com.sun.star.text.XAutoTextGroup
46 public class _XAutoTextGroup extends MultiMethodTest {
48 public XAutoTextGroup oObj = null;
50 /**
51 * Unique number among different interface threads.
53 protected static int uniq = 0 ;
55 /**
56 * Unique string for AutoTextEntry names among different
57 * threads.
59 protected String str = null ;
61 /**
62 * Prefix for unique string.
63 * @see #str
65 protected static final String pref = "XAutoTextGroup" ;
66 protected XTextRange oRange = null;
68 /**
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 unsuccessful
73 * test runs). The relation is obtained.
75 * @throws StatusException if the relation is not found.
77 @Override
78 public void before() {
79 str = pref + uniq++ ;
80 String[] names = oObj.getElementNames() ;
81 for (int i = 0; i < names.length; i++) {
82 log.println(" " + names[i]);
83 if (names[i].toUpperCase().indexOf(pref.toUpperCase()) > 0) {
84 try {
85 log.println(" ... removing ...");
86 oObj.removeByName(names[i]) ;
87 } catch (com.sun.star.container.NoSuchElementException e) {
88 log.println("Element '" + names[i] + "' not found.");
93 oRange = (XTextRange) tEnv.getObjRelation("TextRange");
94 if (oRange == null) {
95 throw new StatusException(Status.failed("No relation found")) ;
99 /**
100 * Test calls the method. <p>
101 * Has <b> OK </b> status if the method returns not
102 * <code>null</code> value.
104 public void _getTitles() {
106 String[] titles = oObj.getTitles();
107 tRes.tested("getTitles()",titles != null);
111 * Firsts inserts a new <code>AutoTextEntry</code> using a range
112 * from relation, entry titles are checked before and after
113 * insertion, second tries to add an entry with the same name. <p>
115 * Has <b>OK</b> status if in the first case titles are changed,
116 * and in the second case <code>ElementExistException</code> is
117 * thrown.
119 public void _insertNewByName() {
121 boolean result = false;
123 try {
124 String[] before = oObj.getTitles();
125 oObj.insertNewByName(str, "For " + str,oRange);
126 String[] after = oObj.getTitles();
127 result = !util.ValueComparer.equalValue(before, after);
129 catch (com.sun.star.container.ElementExistException ex) {
130 log.println("Exception occurred while testing insertNewByName");
131 ex.printStackTrace(log);
132 result = false;
135 try {
136 oObj.insertNewByName(str, "For " + str, oRange);
137 log.println(
138 "com::sun::star::container::ElementExistsException wasn't thrown");
139 oObj.removeByName(str);
140 result &= false;
141 } catch (com.sun.star.container.ElementExistException ex) {
142 result &= true;
143 } catch (com.sun.star.container.NoSuchElementException ex) {
144 log.println("Wrong exception was thrown :");
145 ex.printStackTrace(log);
146 result &= false;
149 tRes.tested("insertNewByName()",result);
154 * Removes <code>AutoTextEntry</code> added before and checks
155 * titles of the group before and after removing. <p>
156 * Has <b> OK </b> status if titles are not equal before and after
157 * removing and no exceptions were thrown. <p>
158 * The following method tests are to be completed successfully before :
159 * <ul>
160 * <li> <code> insertNewByName() </code> : the entry is
161 * inserted here. </li>
162 * </ul>
164 public void _removeByName() {
165 requiredMethod("insertNewByName()") ;
167 try {
168 String[] before = oObj.getTitles();
169 oObj.removeByName(str);
170 String[] after = oObj.getTitles();
171 tRes.tested("removeByName()",
172 !ValueComparer.equalValue(before,after));
174 catch (com.sun.star.container.NoSuchElementException ex) {
175 log.println("Exception occurred while testing removeByName");
176 ex.printStackTrace(log);
177 tRes.tested("removeByName()",false);
182 * Three cases are tested here :
183 * <ol>
184 * <li> Trying to rename an entry to a name, which already
185 * exists in the group. <code>ElementExistException</code>
186 * must be thrown. </li>
187 * <li> Trying to rename an element with non-existing name.
188 * <code>IllegalArgumentException</code> must be thrown.</li>
189 * <li> The normal situation : no exceptions must be thrown
190 * and element with a new name must arise. </li>
191 * </ol>
193 * Has <b>OK</b> status if all three cases were completed successfully.
195 public void _renameByName() {
196 boolean result = false;
198 try {
199 oObj.getTitles();
200 oObj.getElementNames();
201 oObj.insertNewByName(str,"For " + str,oRange);
202 oObj.insertNewByName(str + "dup","For " + str,oRange);
203 oObj.getTitles();
204 oObj.getElementNames();
205 result = true;
206 } catch (com.sun.star.container.ElementExistException e) {
207 log.println("Unexpected exception occurred :") ;
208 e.printStackTrace(log);
209 } finally {
210 if (!result) {
211 try {
212 oObj.removeByName(str);
213 } catch (com.sun.star.container.NoSuchElementException e) {}
214 try {
215 oObj.removeByName(str + "dup");
216 } catch (com.sun.star.container.NoSuchElementException e) {}
217 tRes.tested("renameByName()", false);
218 return;
223 try {
224 oObj.renameByName(str, str + "dup", "For "+str);
225 log.println(
226 "com::sun::star::container::ElementExistsException wasn't thrown");
227 result = false;
228 } catch (com.sun.star.container.ElementExistException e) {
229 result = true;
230 } catch (com.sun.star.io.IOException e) {
231 log.println("Wrong exception was thrown :");
232 e.printStackTrace(log);
233 result = false;
234 } catch (com.sun.star.lang.IllegalArgumentException e) {
235 log.println("Wrong exception was thrown :");
236 e.printStackTrace(log);
237 result = false;
238 } finally {
239 try {
240 oObj.removeByName(str);
241 } catch (com.sun.star.container.NoSuchElementException e) {}
242 try {
243 oObj.removeByName(str + "dup");
244 } catch (com.sun.star.container.NoSuchElementException e) {}
247 try {
248 oObj.renameByName("~"+str,str,str);
249 log.println(
250 "com::sun::star::lang::IllegalArgumentException wasn't thrown");
251 result &= false;
252 } catch (com.sun.star.lang.IllegalArgumentException ex) {
253 result &= true;
254 } catch (com.sun.star.container.ElementExistException e) {
255 log.println("Unexpected exception :") ;
256 e.printStackTrace(log) ;
257 result = false ;
258 } catch (com.sun.star.io.IOException e) {
259 log.println("Unexpected exception :") ;
260 e.printStackTrace(log) ;
261 result = false ;
262 } finally {
263 try {
264 oObj.removeByName(str);
265 } catch (com.sun.star.container.NoSuchElementException e) {}
268 try {
269 oObj.insertNewByName(str, "For " + str, oRange);
271 oObj.renameByName(str,str+"a",str+"b");
272 result &= oObj.hasByName(str + "a");
273 } catch (com.sun.star.container.ElementExistException ex) {
274 log.println("Exception occurred while testing renameByName");
275 ex.printStackTrace(log);
276 result &=false;
277 } catch (com.sun.star.lang.IllegalArgumentException ex) {
278 log.println("Exception occurred while testing renameByName");
279 ex.printStackTrace(log);
280 result &=false;
281 } catch (com.sun.star.io.IOException ex) {
282 log.println("Exception occurred while testing renameByName");
283 ex.printStackTrace(log);
284 result &=false;
285 } finally {
286 try {
287 oObj.removeByName(str);
288 } catch (com.sun.star.container.NoSuchElementException e) {}
289 try {
290 oObj.removeByName(str + "a");
291 } catch (com.sun.star.container.NoSuchElementException e) {}
294 tRes.tested("renameByName()",result);
298 } // finish class _XAutoTextGroup