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.container;
25 
26 import com.sun.star.sheet.XCellRangeAddressable;
27 import lib.MultiMethodTest;
28 import util.ValueComparer;
29 
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.container.XNameReplace;
32 import com.sun.star.uno.UnoRuntime;
33 /**
34 * Testing <code>com.sun.star.container.XNameReplace</code>
35 * interface methods :
36 * <ul>
37 *  <li><code> replaceByName()</code></li>
38 * </ul>
39 * This test needs the following object relations :
40 * <ul>
41 *  <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
42 *   which represents objects to be replaced with. See below
43 *   for more information.</li>
44 *  <li> <code>'NAMEREPLACE'</code> <b>optional</b>: <code>String</code>
45 *    relation which represents element name to be replaced.
46 *    Some Objects can't replace the firsr that comes along, i.e.
47 *    SwXStyleFamily. It have some pool styles which can't be replaced.
48 *    So the test need a special object to replace it by name. </li>
49 *  <li> <code>'XNameReplaceINDEX'</code> : For internal test
50 *   usage. Contains current thread number. </li>
51 *  <li> Test environment variable <code>'THRCNT'</code> : number
52 *   of interface threads running concurently. </li>
53 * <ul> <p>
54 * XNameReplace needs n ObjectRelations "INSTANCEn" , where n = 1, ..., THRCNT.
55 * <p>
56 * When this interface tested by different threads, it must use different instances
57 * to replace - one for each thread.
58 * <p>
59 * That's why we use objRelation "XNameReplaceINDEX" to store the number of last
60 * taken instance. If there is no such relation, it initialize with 1.
61 * <p>
62 * In one of the last steps the replaced object will be compared with the old
63 * object. For that it is necessary that every thread replace it's own object.
64 * INSTANCEn are n Objectrelations so that every thread can isert it's own
65 * object. n depends on the variable THRCNT which and comes from API.INI
66 * Some Object-Container can't replace the first that comes belong. So in
67 * NAMEREPLACE you can determine a containerobject, which is replaceable. <p>
68 *
69 * Test is <b> NOT </b> multithread compilant. <p>
70 * After test completion object environment has to be recreated.
71 * @see com.sun.star.container.XNameReplace
72 */
73 public class _XNameReplace extends MultiMethodTest {
74 
75     public XNameReplace oObj = null;
76 
77     /**
78     * First test retrieves instance to be replaced with for each interface thread.
79     * Then list of element names is retrieved, the first of them will
80     * be replaced. In special case when <code>'NAMEREPLACE'</code> relation
81     * exists, element with the specified name is replaced.
82     * Test replaces element and checks values of element with the
83     * specified name before and after replacement. <p>
84     * Has <b>OK</b> status if values before and after replacement are
85     * different.
86     */
_replaceByName()87     public void _replaceByName(){
88         boolean result = true;
89         String[] oNames = null;
90 
91         int Index = 0;
92 
93         //get for every thread its own Object to insert it
94         log.println("get ObjRelation(\"XNameReplaceINDEX\")");
95         String sIndex = (String)tEnv.getObjRelation("XNameReplaceINDEX");
96         System.out.println("Index: "+sIndex);
97         if (sIndex == null) {
98             log.println("No XNameReplaceINDEX - so set it to 1.");
99             tEnv.addObjRelation("XNameReplaceINDEX", Integer.toString(1));
100             Index = 1;
101         } else {
102             Index = Integer.parseInt(sIndex);
103             Index++;
104             tEnv.addObjRelation("XNameReplaceINDEX", Integer.toString(Index));
105         }
106 
107         log.println("get ObjRelation(\"INSTANCE" + Index +"\")");
108         Object oInstance = tEnv.getObjRelation("INSTANCE"+ Index);
109         if (oInstance == null) {
110             log.println("ObjRelation(\"INSTANCE" + Index +"\") Object n.a.");
111         }
112 
113         log.println("getting the existant object's name");
114         XNameAccess oNameAccess = (XNameAccess)UnoRuntime.queryInterface(
115                                                        XNameAccess.class, oObj);
116         oNames = oNameAccess.getElementNames();
117         /* Some Objects can't replace the firsr that comes along, i.e.
118            SwXStyleFamily. It have some pool styles which can't be replaced.
119            So the test need a special object to replace it by name.
120         */
121         log.println("get ObjRelation(\"NAMEREPLACE\")");
122         Object oNameReplace = tEnv.getObjRelation("NAMEREPLACE");
123         if (oNameReplace != null) {
124             oNames[0] = oNameReplace.toString();
125         }
126 
127        log.println("replaceByName()");
128         try {
129             boolean ok;
130             log.println("get current object '" + oNames[0] + "'");
131             Object old = oObj.getByName(oNames[0]) ;
132             log.println("replace object '" + oNames[0] + "' with another instance");
133             oObj.replaceByName(oNames[0],oInstance);
134             Object newEl = oObj.getByName(oNames[0]) ;
135 
136             if (tEnv.getTestCase().getObjectName().equals("ScCellRangesObj")) {
137                 ok = compareRanges(old, newEl);
138             } else {
139                 ok = ! ValueComparer.equalValue(old, newEl);
140             }
141             result &= ok;
142             log.println("result of replace: " + ok);
143             log.println("replace back the old object");
144             oObj.replaceByName(oNames[0],old);
145             Object origEl = oObj.getByName(oNames[0]) ;
146 
147             if (tEnv.getTestCase().getObjectName().equals("ScCellRangesObj")) {
148                 ok = ! compareRanges(old, origEl);
149             } else {
150                 ok = ValueComparer.equalValue(old, origEl);
151             }
152 
153             result &= ok;
154             log.println("result of replace back: " + ok);
155         } catch (com.sun.star.lang.IllegalArgumentException e ) {
156             result = false;
157             e.printStackTrace(log) ;
158         } catch (com.sun.star.container.NoSuchElementException e ) {
159             result = false;
160             e.printStackTrace(log) ;
161         } catch (com.sun.star.lang.WrappedTargetException e ) {
162             result = false;
163             e.printStackTrace(log) ;
164         }
165 
166         tRes.tested("replaceByName()", result);
167 
168     } // end replaceByName()
169 
170     /**
171     * Forces object environment recreation.
172     */
after()173     public void after() {
174         disposeEnvironment() ;
175     }
176 
177     // method returns false if the ranges are equal and true otherwise
178 
compareRanges(Object old, Object newEl)179     private boolean compareRanges(Object old, Object newEl) {
180         XCellRangeAddressable xCRA = (XCellRangeAddressable)
181                 UnoRuntime.queryInterface(XCellRangeAddressable.class,old);
182 
183         XCellRangeAddressable xCRA2 = (XCellRangeAddressable)
184                 UnoRuntime.queryInterface(XCellRangeAddressable.class,newEl);
185 
186         int orgStartCol = xCRA.getRangeAddress().StartColumn;
187         int orgEndCol = xCRA.getRangeAddress().EndColumn;
188         int orgStartRow = xCRA.getRangeAddress().StartRow;
189         int orgEndRow = xCRA.getRangeAddress().EndRow;
190 
191         int newStartCol = xCRA2.getRangeAddress().StartColumn;
192         int newEndCol = xCRA2.getRangeAddress().EndColumn;
193         int newStartRow = xCRA2.getRangeAddress().StartRow;
194         int newEndRow = xCRA2.getRangeAddress().EndRow;
195 
196         boolean ret = true;
197 
198         if (orgStartCol == newStartCol) {
199             log.println("\t StartColumn is the same");
200             ret = false;
201         }
202 
203         if (orgEndCol == newEndCol) {
204             log.println("\t EndColumn is the same");
205             ret = false;
206         }
207         if (orgStartRow == newStartRow) {
208             log.println("\t StartRow is the same");
209             ret = false;
210         }
211 
212         if (orgEndRow == newEndRow) {
213             log.println("\t EndRow is the same");
214             ret = false;
215         }
216 
217         return ret;
218     }
219 
220 }
221 
222 
223