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.util;
25 
26 import lib.MultiMethodTest;
27 
28 import com.sun.star.util.SearchAlgorithms;
29 import com.sun.star.util.SearchFlags;
30 import com.sun.star.util.SearchOptions;
31 import com.sun.star.util.SearchResult;
32 import com.sun.star.util.XTextSearch;
33 
34 /**
35 * Testing <code>com.sun.star.util.XTextSearch</code>
36 * interface methods :
37 * <ul>
38 *  <li><code> setOptions()</code></li>
39 *  <li><code> searchForward()</code></li>
40 *  <li><code> searchBackward()</code></li>
41 * </ul> <p>
42 * Test is <b> NOT </b> multithread compilant. <p>
43 * @see com.sun.star.util.XTextSearch
44 */
45 public class _XTextSearch extends MultiMethodTest {
46 
47     // oObj filled by MultiMethodTest
48     public XTextSearch oObj = null ;
49 
50     protected final String str = "acababaabcababadcdaa" ;
51     protected final int startPos = 2 , endPos = 20 ;
52     protected final String searchStr = "(ab)*a(c|d)+" ;
53     protected final int fStartRes = 10, fEndRes = 18 ;
54     protected final int bStartRes = 18, bEndRes = 14 ;
55 
56     /**
57     * Sets options for searching regular expression in a string,
58     * ignoring case. <p>
59     * Has <b>OK</b> status if no runtime exceptions occured.
60     */
61     public void _setOptions() {
62 
63         SearchOptions opt = new SearchOptions() ;
64         opt.algorithmType = SearchAlgorithms.REGEXP ;
65         opt.searchFlag = SearchFlags.ALL_IGNORE_CASE ;
66         opt.searchString = searchStr ;
67 
68         oObj.setOptions(opt) ;
69 
70         tRes.tested("setOptions()", true) ;
71     }
72 
73 
74     /**
75     * Tries to find a substring matching regular expression. <p>
76     * Has <b>OK</b> if the correct substring position returned.
77     */
78     public void _searchForward() {
79         requiredMethod("setOptions()") ;
80 
81         SearchResult res = oObj.searchForward(str, startPos, endPos) ;
82 
83         log.println("Result of searching '" + searchStr + "' substring in \n'" +
84             str + "' string (" + res.subRegExpressions + " matches):") ;
85 
86         for (int i = 0; i < res.subRegExpressions; i++)
87             log.println("  (" + res.startOffset[i] + ", " + res.endOffset[i] + ")") ;
88 
89         tRes.tested("searchForward()", res.subRegExpressions > 0 &&
90             res.startOffset[0] == fStartRes && res.endOffset[0] == fEndRes) ;
91     }
92 
93     /**
94     * Tries to find a substring matching regular expression walking
95     * backward. <p>
96     * Has <b>OK</b> if the correct substring position returned.
97     */
98     public void _searchBackward() {
99         requiredMethod("setOptions()") ;
100 
101         SearchResult res = oObj.searchBackward(str, endPos, startPos) ;
102 
103         log.println("Result of searching '" + searchStr + "' substring in \n'" +
104             str + "' string (" + res.subRegExpressions + " matches):") ;
105 
106         for (int i = 0; i < res.subRegExpressions; i++)
107             log.println("  (" + res.startOffset[i] + ", " + res.endOffset[i] + ")") ;
108 
109         tRes.tested("searchBackward()", res.subRegExpressions > 0 &&
110             res.startOffset[0] == bStartRes && res.endOffset[0] == bEndRes) ;
111     }
112 
113 }
114 
115 
116