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 com.sun.star.table.XCell;
27 import lib.MultiMethodTest;
28 
29 import com.sun.star.util.XReplaceDescriptor;
30 import com.sun.star.util.XReplaceable;
31 import com.sun.star.util.XSearchDescriptor;
32 
33 /**
34  * Testing <code>com.sun.star.util.XReplaceable</code>
35  * interface methods :
36  * <ul>
37  *  <li><code> createReplaceDescriptor()</code></li>
38  *  <li><code> replaceAll()</code></li>
39  * </ul> <p>
40  *
41  * The requipment for the tested object is that it
42  * <b>must containt</b> string 'xTextDoc'. Only
43  * in that case this interface is tested correctly. <p>
44  *
45  * Test is <b> NOT </b> multithread compilant. <p>
46  * @see com.sun.star.util.XReplaceable
47  */
48 public class _XReplaceable extends MultiMethodTest {
49 
50     public XReplaceable oObj = null;
51     public XReplaceDescriptor Rdesc = null;
52     private String mSearchString = "xTextDoc";
53     private String mReplaceString = "** xTextDoc";
54     private boolean mDispose = false;
55 
56     /**
57      * Creates an entry to search for, if the current object does not provide
58      * one. In this case, the environment is disposed after the test, since
59      * the inserted object may influence following tests.
60      *
61      */
before()62     protected void before() {
63         Object o = tEnv.getObjRelation("SEARCHSTRING");
64         if (o != null) {
65             mSearchString = (String)o;
66         }
67         // use object relation for XSearchable
68         o = tEnv.getObjRelation("XSearchable.MAKEENTRYINCELL");
69         if (o != null) {
70             XCell[] cells = new XCell[0];
71             if (o instanceof XCell) {
72                 cells = new XCell[]{(XCell)o};
73             }
74             else if (o instanceof XCell[]) {
75                 cells = (XCell[])o;
76             }
77             else {
78                 log.println("Needed object relation 'XSearchable.MAKEENTRYINCELL' is there, but is of type '"
79                             + o.getClass().getName() + "'. Should be 'XCell' or 'XCell[]' instead.");
80             }
81             for (int i=0; i<cells.length; i++) {
82                 cells[i].setFormula(mSearchString);
83             }
84             mDispose = true;
85         }
86     }
87 
88     /**
89      * Creates the descriptor for replacing string 'xTextDoc'
90      * with string '** xTextDoc'. <p>
91      * Has <b> OK </b> status if the returned descriptor is not
92      * <code>null</code>. <p>
93      */
_createReplaceDescriptor()94     public void _createReplaceDescriptor() {
95 
96         log.println("testing createReplaceDescriptor() ... ");
97 
98         Rdesc = oObj.createReplaceDescriptor();
99         Rdesc.setSearchString(mSearchString);
100         Rdesc.setReplaceString(mReplaceString);
101         tRes.tested("createReplaceDescriptor()", Rdesc != null);
102 
103     }
104 
105     /**
106      * Replaces the text using descriptor created before. Then
107      * search is performed in the target text. <p>
108      *
109      * Has <b> OK </b> status if the string '**' is found in
110      * the text. <p>
111      *
112      * The following method tests are to be completed successfully before :
113      * <ul>
114      *  <li> <code> createReplaceDescriptor() </code> : replace
115      *    descriptor is created. </li>
116      * </ul>
117      */
_replaceAll()118     public void _replaceAll() {
119         requiredMethod("createReplaceDescriptor()");
120         oObj.replaceAll(Rdesc);
121         XSearchDescriptor SDesc = oObj.createSearchDescriptor();
122         SDesc.setSearchString("**");
123         boolean res = (oObj.findFirst(SDesc) != null);
124         // redo replacement
125         Rdesc.setSearchString(mReplaceString);
126         Rdesc.setReplaceString(mSearchString);
127         oObj.replaceAll(Rdesc);
128         res &= (oObj.findFirst(SDesc) == null);
129 
130         tRes.tested("replaceAll()",res);
131     }
132 
133     /**
134      * In case the interface itself made the entry to search for, the environment
135      * must be disposed
136      */
after()137     protected void after() {
138         if(mDispose) {
139             disposeEnvironment();
140         }
141     }
142 }
143 
144