2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc
.container
;
21 import lib
.MultiMethodTest
;
22 import util
.ValueComparer
;
24 import com
.sun
.star
.container
.XIndexReplace
;
25 import com
.sun
.star
.container
.XNameContainer
;
26 import com
.sun
.star
.lang
.IllegalArgumentException
;
27 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
28 import com
.sun
.star
.uno
.UnoRuntime
;
32 * Testing <code>com.sun.star.container.XIndexReplace</code>
35 * <li><code> replaceByIndex()</code></li>
37 * This test needs the following object relations :
39 * <li> <code>'INSTANCE1', ..., 'INSTANCEN'</code> : N relations
40 * which represents objects to be replaced with. See below
41 * for more information.</li>
42 * <li> <code>'XIndexReplaceINDEX'</code> : For internal test
43 * usage. Contains current thread number. </li>
44 * <li> Test environment variable <code>'THRCNT'</code> : number
45 * of interface threads running concurently. </li>
47 * XIndexReplace needs n ObjectRelations "INSTANCEn" , where n = 1, ..., THRCNT.
49 * When this interface tested by different threads, it must use different
50 * instances to replace - one for each thread.<p>
51 * That's why we use objRelation "XIndexReplaceINDEX" to store the number of
52 * last taken instance. If there is no such relation, it initialize with 1.
54 * This ObjectRelations should be necessary to create an Object,
55 * which is can be replaced by index
56 * INSTANCEn are n Objectrelations so that every thread can isert it's own
57 * object. n depends on the variable THRCNT which and comes from API.INI
60 * If you insert the same Object by replaceByIndex() several times you
61 * don't insert the Object several times. The first replaceByIndex() inserts
62 * the Object to the Container but all other replaceByIndex() changes
63 * the Index in the Continer because it's the same Object. <p>
64 * Test is multithread compliant. <p>
65 * @see com.sun.star.container.XIndexReplace
68 public class _XIndexReplace
extends MultiMethodTest
{
69 public XIndexReplace oObj
= null;
72 * Primarily tries to replace elements in a proper way :
73 * replaces the first, middle and the last element then
74 * checks if elements were properly replaced.
75 * Then wrong parameters are passed : invalid index and
76 * null value for replacing, and test checks for proper
77 * exceptions to be thrown. <p>
78 * In different threads it replaces elements with different
80 * Has <b>OK</b> status if in the first (correct) case
81 * elements were successfully replaced (i.e. values got
82 * after replacing must be equal to those replaced with),
83 * and in the second case proper exceptions were thrown.
85 public void _replaceByIndex() {
86 boolean result
= true;
88 Object oInstance
= null;
91 //get for every thread its own Object to insert it
92 log
.println("get ObjRelation(\"XIndexReplaceINDEX\")");
93 String sIndex
= (String
)tEnv
.getObjRelation("XIndexReplaceINDEX");
95 log
.println("No XIndexReplaceINDEX - so set it to 1.");
96 tEnv
.addObjRelation("XIndexReplaceINDEX", Integer
.toString(1));
99 Index
= Integer
.parseInt(sIndex
);
101 tEnv
.addObjRelation("XIndexReplaceINDEX", Integer
.toString(Index
));
105 log
.println("get ObjRelation(\"INSTANCE" + Index
+"\")");
106 oInstance
= tEnv
.getObjRelation("INSTANCE"+ Index
);
107 if (oInstance
== null) {
108 log
.println("ObjRelation(\"INSTANCE" + Index
+"\") Object n.a.");
111 log
.println("testing replaceByIndex(0)...");
115 log
.println("Getting old object");
116 old
= oObj
.getByIndex(0);
117 oObj
.replaceByIndex(0, oInstance
);
118 result
= !(oObj
.getByIndex(0)).equals(old
);
119 result
= ! ValueComparer
.equalValue(oObj
,old
);
121 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
122 e
.printStackTrace(log
) ;
124 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
125 e
.printStackTrace(log
) ;
127 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
128 e
.printStackTrace(log
) ;
133 log
.println("replace with a wrong Object occurs Exceptions ...");
135 oObj
.replaceByIndex(999, oInstance
);
137 log
.println("1. replaceByIndex(): Exception expected! - FAILED");
140 XNameContainer xNC
= UnoRuntime
.queryInterface(XNameContainer
.class, oObj
) ;
141 String
[] names
= xNC
.getElementNames() ;
142 log
.println("Element names :") ;
143 for (int i
= 0; i
<names
.length
; i
++) {
144 log
.println(" '" + names
[i
] + "'") ;
146 } catch (IndexOutOfBoundsException e
) {
147 log
.println("1. replaceByIndex(): Expected exception - OK");
149 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
151 log
.println("1. replaceByIndex(): Unexpected exception! - " +
153 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
155 log
.println("1. replaceByIndex(): Unexpected exception! - " +
159 log
.println("replace with a wrong Object occurs Exceptions ...");
161 oObj
.replaceByIndex(0, null);
163 log
.println("2. replaceByIndex(): Exception expected! - FAILED");
166 XNameContainer xNC
= UnoRuntime
.queryInterface(XNameContainer
.class, oObj
) ;
167 String
[] names
= xNC
.getElementNames() ;
168 log
.println("Element names :") ;
169 for (int i
= 0; i
<names
.length
; i
++) {
170 log
.println(" '" + names
[i
] + "'") ;
172 } catch (IllegalArgumentException e
) {
173 log
.println("2. replaceByIndex(): Expected exception - OK");
175 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
177 log
.println("2. replaceByIndex(): Unexpected exception! - " +
179 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
181 log
.println("2. replaceByIndex(): Unexpected exception! - " +
185 log
.println("replace with the old object");
187 oObj
.replaceByIndex(0, old
);
188 } catch (IllegalArgumentException e
) {
189 e
.printStackTrace(log
) ;
190 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
191 e
.printStackTrace(log
) ;
192 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
193 e
.printStackTrace(log
) ;
196 tRes
.tested("replaceByIndex()", result
);