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