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