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