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