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