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