1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.text;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 import util.ValueComparer;
34 
35 import com.sun.star.text.XAutoTextGroup;
36 import com.sun.star.text.XTextRange;
37 
38 /**
39  * Testing <code>com.sun.star.text.XAutoTextGroup</code>
40  * interface methods :
41  * <ul>
42  *  <li><code> getTitles()</code></li>
43  *  <li><code> renameByName()</code></li>
44  *  <li><code> insertNewByName()</code></li>
45  *  <li><code> removeByName()</code></li>
46  * </ul> <p>
47  * This test needs the following object relations :
48  * <ul>
49  *  <li> <code>'TextRange'</code> (of type <code>XTextRange</code>):
50  *   the range for which an entry is added. </li>
51  * <ul> <p>
52  * Test is multithread compilant. <p>
53  * @see com.sun.star.text.XAutoTextGroup
54  */
55 public class _XAutoTextGroup extends MultiMethodTest {
56 
57     public XAutoTextGroup oObj = null;
58 
59     /**
60      * Unique number among different interface threads.
61      */
62     protected static int uniq = 0 ;
63 
64     /**
65      * Unique string for AutoTextEntry names among different
66      * threads.
67      */
68     protected String str = null ;
69 
70     /**
71      * Prefix for unique string.
72      * @see #str
73      */
74     protected static final String pref = "XAutoTextGroup" ;
75     protected XTextRange oRange = null;
76 
77     /**
78      * Constructs a unique string for current interface thread
79      * for naming purposes. All old entries which names are
80      * started with prefix used for entry names, are deleted
81      * from the group (they can remain after previous unsuccessfull
82      * test runs). The relation is obtained.
83      *
84      * @throws StatusException if the relation is not found.
85      */
86     public void before() {
87         str = pref + uniq++ ;
88         String[] names = oObj.getElementNames() ;
89         for (int i = 0; i < names.length; i++) {
90             log.println("  " + names[i]);
91             if (names[i].toUpperCase().indexOf(pref.toUpperCase()) > 0) {
92                 try {
93                     log.println("  ... removing ...");
94                     oObj.removeByName(names[i]) ;
95                 } catch (com.sun.star.container.NoSuchElementException e) {
96                     log.println("Element '" + names[i] + "' not found.");
97                 }
98             }
99         }
100 
101         oRange = (XTextRange) tEnv.getObjRelation("TextRange");
102         if (oRange == null) {
103             throw new StatusException(Status.failed("No relation found")) ;
104         }
105     }
106 
107     /**
108      * Test calls the method. <p>
109      * Has <b> OK </b> status if the method returns not
110      * <code>null</code> value.
111      */
112     public void _getTitles() {
113 
114         String[] titles = oObj.getTitles();
115         tRes.tested("getTitles()",titles != null);
116     }
117 
118     /**
119      * Firsts inserts a new <code>AutoTextEntry</code> using a range
120      * from relation, entry titles are checked before and after
121      * insertion, second tries to add an entry with the same name. <p>
122      *
123      * Has <b>OK</b> status if in the first case titles are changed,
124      * and in the second case <code>ElementExistException</code> is
125      * thrown.
126      */
127     public void _insertNewByName() {
128 
129         boolean result = false;
130 
131         try {
132             String[] before = oObj.getTitles();
133             oObj.insertNewByName(str, "For " + str,oRange);
134             String[] after = oObj.getTitles();
135             result = !util.ValueComparer.equalValue(before, after);
136         }
137         catch (com.sun.star.container.ElementExistException ex) {
138             log.println("Exception occured while testing insertNewByName");
139             ex.printStackTrace(log);
140             result = false;
141         }
142 
143         try {
144             oObj.insertNewByName(str, "For " + str, oRange);
145             log.println(
146                 "com::sun::star::container::ElementExistsException wasn't thrown");
147             oObj.removeByName(str);
148             result &= false;
149         } catch (com.sun.star.container.ElementExistException ex) {
150             result &= true;
151         } catch (com.sun.star.container.NoSuchElementException ex) {
152             log.println("Wrong exception was thrown :");
153             ex.printStackTrace(log);
154             result &= false;
155         }
156 
157         tRes.tested("insertNewByName()",result);
158 
159     }
160 
161     /**
162      * Removes <code>AutoTextEntry</code> added before and checks
163      * titles of the group before and after removing. <p>
164      * Has <b> OK </b> status if titles are not equal before and after
165      * removing and no exceptions were thrown. <p>
166      * The following method tests are to be completed successfully before :
167      * <ul>
168      *  <li> <code> insertNewByName() </code> : the entry is
169      *   inserted here. </li>
170      * </ul>
171      */
172     public void _removeByName() {
173         requiredMethod("insertNewByName()") ;
174 
175         try {
176             String[] before = oObj.getTitles();
177             oObj.removeByName(str);
178             String[] after = oObj.getTitles();
179             tRes.tested("removeByName()",
180                 !ValueComparer.equalValue(before,after));
181         }
182         catch (com.sun.star.container.NoSuchElementException ex) {
183             log.println("Exception occured while testing removeByName");
184             ex.printStackTrace(log);
185             tRes.tested("removeByName()",false);
186         }
187     }
188 
189     /**
190      * Three cases are tested here :
191      * <ol>
192      *   <li> Trying to rename an entry to a name, which already
193      *     exists in the group. <code>ElementExistException</code>
194      *     must be thrown. </li>
195      *   <li> Trying to rename an element with non-existing name.
196      *     <code>IllegalArgumentException</code> must be thrown.</li>
197      *   <li> The normal situation : no exceptions must be thrown
198      *     and element with a new name must arise. </li>
199      * </ol>
200      *
201      * Has <b>OK</b> status if all three cases were completed successfully.
202      */
203     public void _renameByName() {
204         boolean result = false;
205 
206         try {
207             String[] tit = oObj.getTitles() ;
208             String[] names = oObj.getElementNames() ;
209             oObj.insertNewByName(str,"For " + str,oRange);
210             oObj.insertNewByName(str + "dup","For " + str,oRange);
211             tit = oObj.getTitles() ;
212             names = oObj.getElementNames() ;
213             result = true;
214         } catch (com.sun.star.container.ElementExistException e) {
215             log.println("Unexpected exception occured :") ;
216             e.printStackTrace(log);
217         } finally {
218             if (!result) {
219                 try {
220                     oObj.removeByName(str);
221                 } catch (com.sun.star.container.NoSuchElementException e) {}
222                 try {
223                     oObj.removeByName(str + "dup");
224                 } catch (com.sun.star.container.NoSuchElementException e) {}
225                 tRes.tested("renameByName()", false);
226                 return;
227             }
228         }
229 
230 
231         try {
232             oObj.renameByName(str, str + "dup", "For "+str);
233             log.println(
234                 "com::sun::star::container::ElementExistsException wasn't thrown");
235             result = false;
236         } catch (com.sun.star.container.ElementExistException e) {
237             result = true;
238         } catch (com.sun.star.io.IOException e) {
239             log.println("Wrong exception was thrown :");
240             e.printStackTrace(log);
241             result = false;
242         } catch (com.sun.star.lang.IllegalArgumentException e) {
243             log.println("Wrong exception was thrown :");
244             e.printStackTrace(log);
245             result = false;
246         } finally {
247             try {
248                 oObj.removeByName(str);
249             } catch (com.sun.star.container.NoSuchElementException e) {}
250             try {
251                 oObj.removeByName(str + "dup");
252             } catch (com.sun.star.container.NoSuchElementException e) {}
253         }
254 
255         try {
256             oObj.renameByName("~"+str,str,str);
257             log.println(
258                 "com::sun::star::lang::IllegalArgumentException wasn't thrown");
259             result &= false;
260         } catch (com.sun.star.lang.IllegalArgumentException ex) {
261             result &= true;
262         } catch (com.sun.star.container.ElementExistException e) {
263             log.println("Unexpected exception :") ;
264             e.printStackTrace(log) ;
265             result = false ;
266         } catch (com.sun.star.io.IOException e) {
267             log.println("Unexpected exception :") ;
268             e.printStackTrace(log) ;
269             result = false ;
270         } finally {
271             try {
272                 oObj.removeByName(str);
273             } catch (com.sun.star.container.NoSuchElementException e) {}
274         }
275 
276         try {
277             oObj.insertNewByName(str, "For " + str, oRange);
278 
279             oObj.renameByName(str,str+"a",str+"b");
280             result &= oObj.hasByName(str + "a");
281         } catch (com.sun.star.container.ElementExistException ex) {
282             log.println("Exception occured while testing renameByName");
283             ex.printStackTrace(log);
284             result &=false;
285         } catch (com.sun.star.lang.IllegalArgumentException ex) {
286             log.println("Exception occured while testing renameByName");
287             ex.printStackTrace(log);
288             result &=false;
289         } catch (com.sun.star.io.IOException ex) {
290             log.println("Exception occured while testing renameByName");
291             ex.printStackTrace(log);
292             result &=false;
293         } finally {
294             try {
295                 oObj.removeByName(str);
296             } catch (com.sun.star.container.NoSuchElementException e) {}
297             try {
298                 oObj.removeByName(str + "a");
299             } catch (com.sun.star.container.NoSuchElementException e) {}
300         }
301 
302         tRes.tested("renameByName()",result);
303 
304     }
305 
306 }  // finish class _XAutoTextGroup
307 
308 
309