1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package ifc
.container
;
30 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
31 import lib
.MultiMethodTest
;
32 import util
.ValueComparer
;
34 import com
.sun
.star
.container
.XNameAccess
;
35 import com
.sun
.star
.container
.XNameReplace
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
38 * Testing <code>com.sun.star.container.XNameReplace</code>
41 * <li><code> replaceByName()</code></li>
43 * This test needs the following object relations :
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>
58 * XNameReplace needs n ObjectRelations "INSTANCEn" , where n = 1, ..., THRCNT.
60 * When this interface tested by different threads, it must use different instances
61 * to replace - one for each thread.
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.
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>
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
77 public class _XNameReplace
extends MultiMethodTest
{
79 public XNameReplace oObj
= null;
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
91 public void _replaceByName(){
92 boolean result
= true;
93 String
[] oNames
= null;
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));
106 Index
= Integer
.parseInt(sIndex
);
108 tEnv
.addObjRelation("XNameReplaceINDEX", Integer
.toString(Index
));
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.");
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.
125 log
.println("get ObjRelation(\"NAMEREPLACE\")");
126 Object oNameReplace
= tEnv
.getObjRelation("NAMEREPLACE");
127 if (oNameReplace
!= null) {
128 oNames
[0] = oNameReplace
.toString();
131 log
.println("replaceByName()");
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]) ;
140 if (tEnv
.getTestCase().getObjectName().equals("ScCellRangesObj")) {
141 ok
= compareRanges(old
, newEl
);
143 ok
= ! ValueComparer
.equalValue(old
, newEl
);
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]) ;
151 if (tEnv
.getTestCase().getObjectName().equals("ScCellRangesObj")) {
152 ok
= ! compareRanges(old
, origEl
);
154 ok
= ValueComparer
.equalValue(old
, origEl
);
158 log
.println("result of replace back: " + ok
);
159 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
161 e
.printStackTrace(log
) ;
162 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
164 e
.printStackTrace(log
) ;
165 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
167 e
.printStackTrace(log
) ;
170 tRes
.tested("replaceByName()", result
);
172 } // end replaceByName()
175 * Forces object environment recreation.
177 public void after() {
178 disposeEnvironment() ;
181 // method returns false if the ranges are equal and true otherwise
183 private boolean compareRanges(Object old
, Object newEl
) {
184 XCellRangeAddressable xCRA
= (XCellRangeAddressable
)
185 UnoRuntime
.queryInterface(XCellRangeAddressable
.class,old
);
187 XCellRangeAddressable xCRA2
= (XCellRangeAddressable
)
188 UnoRuntime
.queryInterface(XCellRangeAddressable
.class,newEl
);
190 int orgStartCol
= xCRA
.getRangeAddress().StartColumn
;
191 int orgEndCol
= xCRA
.getRangeAddress().EndColumn
;
192 int orgStartRow
= xCRA
.getRangeAddress().StartRow
;
193 int orgEndRow
= xCRA
.getRangeAddress().EndRow
;
195 int newStartCol
= xCRA2
.getRangeAddress().StartColumn
;
196 int newEndCol
= xCRA2
.getRangeAddress().EndColumn
;
197 int newStartRow
= xCRA2
.getRangeAddress().StartRow
;
198 int newEndRow
= xCRA2
.getRangeAddress().EndRow
;
202 if (orgStartCol
== newStartCol
) {
203 log
.println("\t StartColumn is the same");
207 if (orgEndCol
== newEndCol
) {
208 log
.println("\t EndColumn is the same");
211 if (orgStartRow
== newStartRow
) {
212 log
.println("\t StartRow is the same");
216 if (orgEndRow
== newEndRow
) {
217 log
.println("\t EndRow is the same");