1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
30 import lib
.MultiMethodTest
;
32 import com
.sun
.star
.util
.SearchAlgorithms
;
33 import com
.sun
.star
.util
.SearchFlags
;
34 import com
.sun
.star
.util
.SearchOptions
;
35 import com
.sun
.star
.util
.SearchResult
;
36 import com
.sun
.star
.util
.XTextSearch
;
39 * Testing <code>com.sun.star.util.XTextSearch</code>
42 * <li><code> setOptions()</code></li>
43 * <li><code> searchForward()</code></li>
44 * <li><code> searchBackward()</code></li>
46 * Test is <b> NOT </b> multithread compilant. <p>
47 * @see com.sun.star.util.XTextSearch
49 public class _XTextSearch
extends MultiMethodTest
{
51 // oObj filled by MultiMethodTest
52 public XTextSearch oObj
= null ;
54 protected final String str
= "acababaabcababadcdaa" ;
55 protected final int startPos
= 2 , endPos
= 20 ;
56 protected final String searchStr
= "(ab)*a(c|d)+" ;
57 protected final int fStartRes
= 10, fEndRes
= 18 ;
58 protected final int bStartRes
= 18, bEndRes
= 14 ;
61 * Sets options for searching regular expression in a string,
63 * Has <b>OK</b> status if no runtime exceptions occurred.
65 public void _setOptions() {
67 SearchOptions opt
= new SearchOptions() ;
68 opt
.algorithmType
= SearchAlgorithms
.REGEXP
;
69 opt
.searchFlag
= SearchFlags
.ALL_IGNORE_CASE
;
70 opt
.searchString
= searchStr
;
72 oObj
.setOptions(opt
) ;
74 tRes
.tested("setOptions()", true) ;
79 * Tries to find a substring matching regular expression. <p>
80 * Has <b>OK</b> if the correct substring position returned.
82 public void _searchForward() {
83 requiredMethod("setOptions()") ;
85 SearchResult res
= oObj
.searchForward(str
, startPos
, endPos
) ;
87 log
.println("Result of searching '" + searchStr
+ "' substring in \n'" +
88 str
+ "' string (" + res
.subRegExpressions
+ " matches):") ;
90 for (int i
= 0; i
< res
.subRegExpressions
; i
++)
91 log
.println(" (" + res
.startOffset
[i
] + ", " + res
.endOffset
[i
] + ")") ;
93 tRes
.tested("searchForward()", res
.subRegExpressions
> 0 &&
94 res
.startOffset
[0] == fStartRes
&& res
.endOffset
[0] == fEndRes
) ;
98 * Tries to find a substring matching regular expression walking
100 * Has <b>OK</b> if the correct substring position returned.
102 public void _searchBackward() {
103 requiredMethod("setOptions()") ;
105 SearchResult res
= oObj
.searchBackward(str
, endPos
, startPos
) ;
107 log
.println("Result of searching '" + searchStr
+ "' substring in \n'" +
108 str
+ "' string (" + res
.subRegExpressions
+ " matches):") ;
110 for (int i
= 0; i
< res
.subRegExpressions
; i
++)
111 log
.println(" (" + res
.startOffset
[i
] + ", " + res
.endOffset
[i
] + ")") ;
113 tRes
.tested("searchBackward()", res
.subRegExpressions
> 0 &&
114 res
.startOffset
[0] == bStartRes
&& res
.endOffset
[0] == bEndRes
) ;