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