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