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 com
.sun
.star
.lib
.uno
.helper
;
21 import com
.sun
.star
.uno
.XWeak
;
22 import com
.sun
.star
.lang
.XTypeProvider
;
23 import com
.sun
.star
.lib
.uno
.environments
.java
.java_environment
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Iterator
;
27 import java
.util
.List
;
28 import java
.util
.ListIterator
;
29 import java
.util
.NoSuchElementException
;
31 import java
.util
.logging
.Level
;
32 import java
.util
.logging
.Logger
;
34 import static org
.junit
.Assert
.assertArrayEquals
;
35 import static org
.junit
.Assert
.assertEquals
;
36 import static org
.junit
.Assert
.assertFalse
;
37 import static org
.junit
.Assert
.assertNotNull
;
38 import static org
.junit
.Assert
.assertSame
;
39 import static org
.junit
.Assert
.assertTrue
;
40 import static org
.junit
.Assert
.fail
;
41 import org
.junit
.Before
;
42 import org
.junit
.Test
;
44 public class InterfaceContainer_Test
46 private static final Logger logger
= Logger
.getLogger(InterfaceContainer_Test
.class.getName());
47 java_environment env
= new java_environment(null);
48 /** Creates a new instance of InterfaceContainerTest */
49 AWeakBase obj1
,obj2
,obj3
,obj4
;
50 Object proxyObj1Weak1
;
51 Object proxyObj3Weak1
;
52 Object proxyObj3Weak2
;
53 Object proxyObj3TypeProv
;
54 Object proxyObj2TypeProv
;
55 //contains original objects
57 //contains original objects + proxies
59 //contains original object + proxies + null value
62 /** Class variables are initialized before each Test method */
63 @Before public void setUp() throws Exception
65 obj1
= new AWeakBase();
66 obj2
= new AWeakBase();
67 obj3
= new AWeakBase();
68 obj4
= new AWeakBase();
70 proxyObj1Weak1
= ProxyProvider
.createProxy(obj1
, XWeak
.class);
71 proxyObj3Weak1
= ProxyProvider
.createProxy(obj3
, XWeak
.class);
72 proxyObj3Weak2
= ProxyProvider
.createProxy(obj3
, XWeak
.class);
73 assertNotNull(proxyObj1Weak1
);
74 assertNotNull(proxyObj3Weak1
);
75 assertNotNull(proxyObj3Weak2
);
77 proxyObj2TypeProv
= ProxyProvider
.createProxy(obj2
, XTypeProvider
.class);
78 proxyObj3TypeProv
= ProxyProvider
.createProxy(obj3
, XTypeProvider
.class);
79 assertNotNull(proxyObj2TypeProv
);
80 assertNotNull(proxyObj3TypeProv
);
82 list1
= new ArrayList
<Object
>();
87 list2
= new ArrayList
<Object
>();
89 list2
.add(proxyObj2TypeProv
);
90 list2
.add(proxyObj3TypeProv
);
92 list3
= new ArrayList
<Object
>();
95 list3
.add(proxyObj2TypeProv
);
96 list3
.add(proxyObj3Weak1
);
99 /** Tests add(object), size(), clear();
101 @Test public void add() throws Exception
103 logger
.log(Level
.INFO
, "Testing List.add(Object), List.size(), List.clear(), List.isEmpty()");
104 InterfaceContainer cont
= new InterfaceContainer();
106 assertEquals(cont
.size(), 0);
108 assertTrue(cont
.add(obj1
));
109 assertEquals(cont
.size(), 1);
111 assertTrue(cont
.add(obj1
)); // add the same object again
112 assertEquals(cont
.size(), 2);
114 assertTrue(cont
.add(proxyObj3TypeProv
));
115 assertEquals(cont
.size(), 3);
117 assertFalse(cont
.isEmpty());
119 assertEquals(cont
.size(), 0);
120 assertTrue(cont
.isEmpty());
123 /**Tests list.addAll(Collection c), list.addAll(int index, Collection c)
125 @Test public void listConstructors() throws Exception
127 logger
.log(Level
.INFO
, "Testing Constructors of InterfaceContainer");
128 InterfaceContainer cont
= new InterfaceContainer(100);
130 assertEquals(cont
.elementData
.length
, 100);
133 @Test public void trimToSize() throws Exception
135 logger
.log(Level
.INFO
, "Testing InterfaceContainer.trimToSize");
136 InterfaceContainer cont
= new InterfaceContainer(100);
139 assertTrue(cont
.isEmpty());
140 cont
= new InterfaceContainer(10);
143 assertEquals(cont
.elementData
.length
, list1
.size());
146 @Test public void ensureCapacity() throws Exception
148 logger
.log(Level
.INFO
, "Testing InterfaceContainer.ensureCapacity");
149 InterfaceContainer cont
= new InterfaceContainer(10);
151 cont
.ensureCapacity(9);
152 assertTrue(cont
.elementData
.length
>= 9);
154 cont
.ensureCapacity(11);
155 assertTrue(cont
.elementData
.length
>= 11);
158 @Test public void addAll() throws Exception
160 logger
.log(Level
.INFO
, "Testing List.addAll(Collection c), List.addAll(int index, Collection c)");
161 InterfaceContainer cont
= new InterfaceContainer();
163 assertTrue(cont
.addAll(list1
));
164 assertEquals(cont
.size(), list1
.size());
165 for (int c
= 0; c
< cont
.size(); c
++)
167 assertSame(list1
.get(c
), cont
.get(c
));
169 assertTrue(cont
.add(obj1
));
170 assertTrue(cont
.addAll(1, list2
));
171 assertSame(cont
.get(0), list1
.get(0));
172 assertSame(cont
.get(1), list2
.get(0));
173 assertSame(cont
.get(2), list2
.get(1));
174 assertSame(cont
.get(3), list2
.get(2));
175 assertSame(cont
.get(4), list1
.get(1));
176 assertSame(cont
.get(5), list1
.get(2));
177 assertSame(cont
.get(6), obj1
);
181 // the null element in list3 at index 1 is not added to cont
182 assertSame(cont
.get(0), list3
.get(0));
183 assertSame(cont
.get(1), list3
.get(2));
184 assertSame(cont
.get(2), list3
.get(3));
187 /** Tests List.add(int index, Object element), List.get(int index)
189 @Test public void get() throws Exception
191 logger
.log(Level
.INFO
, "Testing List.add(int index, Object element), List.get(int index)");
192 InterfaceContainer cont
= new InterfaceContainer();
196 cont
.add(1, proxyObj3Weak1
);
199 assertSame(cont
.get(0), obj1
);
200 assertSame(cont
.get(1), proxyObj3Weak1
);
201 assertSame(cont
.get(2), obj2
);
202 assertSame(cont
.get(3), obj3
);
206 fail("IndexOutOfBoundsException expected");
207 } catch (IndexOutOfBoundsException indexOutOfBoundsException
) {
208 logger
.log(Level
.FINE
, "IndexOutOfBoundsException caught");
212 /** Tests List.contains
214 @Test public void contains() throws Exception
216 logger
.log(Level
.INFO
, "Testing List.contains()");
217 InterfaceContainer cont
= new InterfaceContainer();
219 assertFalse(cont
.contains(obj1
)); // nothing in the list
222 cont
.add(proxyObj2TypeProv
);
223 cont
.add(proxyObj3TypeProv
);
225 assertTrue(cont
.contains(obj1
));
226 assertTrue(cont
.contains(obj2
));
227 assertTrue(cont
.contains(proxyObj3Weak1
));
228 assertTrue(cont
.contains(proxyObj3Weak2
));
229 assertTrue(cont
.contains(proxyObj1Weak1
));
230 assertTrue(cont
.contains(obj3
));
231 assertFalse(cont
.contains(null));
234 /** Tests List.containsAll
236 @Test public void containsAll() throws Exception
238 logger
.log(Level
.INFO
, "Testing List.containsAll");
239 InterfaceContainer cont
= new InterfaceContainer();
242 assertTrue(cont
.containsAll(list1
));
246 assertTrue(cont
.containsAll(list2
));
249 cont
.addAll(list3
); // the null element in list3 is not added to cont
250 assertFalse(cont
.containsAll(list3
));
253 for( int x
= 0; x
< 6; x
++)
255 assertFalse(cont
.contains(list1
));
257 cont
.add(1, list1
.get(0));
258 cont
.add(3, list1
.get(1));
259 cont
.add(5, list1
.get(2));
260 assertFalse(cont
.contains(list1
));
262 /** Tests List.indexOf, List.lastIndexOf
264 @Test public void indexOf() throws Exception
266 logger
.log(Level
.INFO
, "Testing List.indexOf(Object element), List.lastIndexOf(Object element)");
267 InterfaceContainer cont
= new InterfaceContainer();
271 assertEquals(cont
.indexOf(obj3
), 2);
272 assertEquals(cont
.lastIndexOf(obj3
), 5);
277 assertEquals(cont
.indexOf(proxyObj3Weak1
), 2);
278 assertEquals(cont
.lastIndexOf(proxyObj3Weak2
), 5);
281 /** Tests List.remove(int index), List.remove(Object element), List.removeAll(Collection c)
283 @Test public void remove() throws Exception
285 logger
.log(Level
.INFO
, "Testing List.remove(int index), List.remove(Object element), List.removeAll(Collection c)");
286 InterfaceContainer cont
= new InterfaceContainer();
289 assertTrue(proxyObj2TypeProv
.equals(cont
.remove(1)));
290 assertEquals(cont
.size(), 2);
294 assertTrue(cont
.remove(obj1
));
295 assertTrue(cont
.remove(proxyObj2TypeProv
));
296 assertTrue(cont
.remove(proxyObj3Weak2
));
297 assertTrue(cont
.isEmpty());
301 assertTrue(cont
.removeAll(list3
));
302 assertTrue(cont
.isEmpty());
305 List
<Object
> list
= new ArrayList
<Object
>();
306 list
.add(list2
.get(0));
307 list
.add(list2
.get(1));
308 list
.add(proxyObj3Weak2
);
309 assertTrue(cont
.removeAll(list
));
310 assertTrue(cont
.isEmpty());
313 /** Tests List.retainAll
315 @Test public void retainAll() throws Exception
317 logger
.log(Level
.INFO
, "Testing List.retainAll(Collection c)");
318 InterfaceContainer cont
= new InterfaceContainer();
320 cont
.addAll(list1
); //obj1, obj2, obj3
321 cont
.addAll(list2
); //obj1, proxyObj2TypeProv, proxyObj3TypeProv
322 List
<Object
> list
= new ArrayList
<Object
>();
324 list
.add(proxyObj3Weak1
);
326 assertTrue(cont
.retainAll(list
));
327 assertSame(cont
.get(0), obj1
);
328 assertSame(cont
.get(1), obj3
);
329 assertSame(cont
.get(2), obj1
);
330 assertSame(cont
.get(3), proxyObj3TypeProv
);
331 assertEquals(cont
.size(), 4);
334 /** Tests List.set(int index, Object element)
336 @Test public void set() throws Exception
338 logger
.log(Level
.INFO
, "Testing List.set(int index, Object element)");
339 InterfaceContainer cont
= new InterfaceContainer();
342 Object o1
= cont
.set(0, obj3
);
343 Object o2
= cont
.set(2, proxyObj3Weak1
);
345 assertSame(list2
.get(0), o1
);
346 assertSame(list2
.get(2), o2
);
347 assertSame(cont
.get(0), obj3
);
348 assertSame(cont
.get(2), proxyObj3Weak1
);
351 /** Tests List.toArray(), List.toArray(Object[] a)
353 @Test public void toArray() throws Exception
355 logger
.log(Level
.INFO
, "Testing List.toArray(), List.toArray(Object[] a)");
356 InterfaceContainer cont
= new InterfaceContainer();
359 Object
[] ar
= cont
.toArray();
360 Object
[] arOrig
= list1
.toArray();
361 assertEquals(ar
.length
, arOrig
.length
);
362 assertArrayEquals(ar
, arOrig
);
364 XWeak
[] arWeak
= new XWeak
[3];
365 XWeak
[] arWeak2
= (XWeak
[])cont
.toArray(arWeak
);
366 assertEquals(ar
.length
, arWeak2
.length
);
367 assertArrayEquals(ar
, arWeak2
);
370 @Test public void Iterator_next() throws Exception
372 logger
.log(Level
.INFO
, "Testing InterfaceContainer.iterator, Iterator.next()");
373 InterfaceContainer cont
= new InterfaceContainer();
376 Iterator it
= cont
.iterator();
377 assertSame(it
.next(), list1
.get(0));
378 assertSame(it
.next(), list1
.get(1));
379 assertSame(it
.next(), list1
.get(2));
383 fail("NoSuchElementException expected");
384 } catch (NoSuchElementException noSuchElementException
) {
385 logger
.log(Level
.FINE
, "NoSuchElementException caught");
389 @Test public void Iterator_hasNext() throws Exception
391 logger
.log(Level
.INFO
, "Testing, Iterator.next()");
392 InterfaceContainer cont
= new InterfaceContainer();
394 Iterator it
= cont
.iterator();
395 assertFalse(it
.hasNext());
399 assertTrue(it
.hasNext());
402 assertTrue(it
.hasNext());
405 assertTrue(it
.hasNext());
408 assertFalse(it
.hasNext());
411 @Test public void Iterator_remove() throws Exception
413 logger
.log(Level
.INFO
, "Testing, Iterator.remove()");
414 InterfaceContainer cont
= new InterfaceContainer();
416 Iterator it
= cont
.iterator();
419 fail("IllegalStateException expected");
420 } catch (IllegalStateException illegalStateException
) {
421 logger
.log(Level
.FINE
, "IllegalStateException caught");
428 assertTrue(cont
.isEmpty());
431 fail("IllegalStateException expected");
432 } catch (IllegalStateException illegalStateException
) {
433 logger
.log(Level
.FINE
, "IllegalStateException caught");
444 assertTrue(cont
.isEmpty());
446 // 2 iterators, remove must not impair the other iterator
449 int size
= cont
.size();
451 Iterator it2
= cont
.iterator();
457 for( int c
= 0; c
< size
; c
++)
459 assertEquals(cont
.size(), 0);
462 @Test public void ListIterator_next() throws Exception
464 logger
.log(Level
.INFO
, "Testing InterfaceContainer.listIterator, ListIterator.next()");
465 InterfaceContainer cont
= new InterfaceContainer();
468 Iterator it
= cont
.listIterator();
469 assertSame(it
.next(), list1
.get(0));
470 assertSame(it
.next(), list1
.get(1));
471 assertSame(it
.next(), list1
.get(2));
474 fail("NoSuchElementException expected");
475 } catch (NoSuchElementException noSuchElementException
) {
476 logger
.log(Level
.FINE
, "NoSuchElementException caught");
480 @Test public void ListIterator_hasNext() throws Exception
482 logger
.log(Level
.INFO
, "Testing ListIterator.hasNext()");
483 InterfaceContainer cont
= new InterfaceContainer();
485 Iterator it
= cont
.listIterator();
486 assertFalse(it
.hasNext());
489 it
= cont
.listIterator();
490 assertTrue(it
.hasNext());
492 assertTrue(it
.hasNext());
494 assertTrue(it
.hasNext());
496 assertFalse(it
.hasNext());
499 @Test public void ListIterator_remove() throws Exception
501 logger
.log(Level
.INFO
, "Testing ListIterator.remove()");
502 InterfaceContainer cont
= new InterfaceContainer();
504 ListIterator it
= cont
.listIterator();
507 fail("IllegalStateException expected");
508 } catch (IllegalStateException illegalStateException
) {
509 logger
.log(Level
.FINE
, "IllegalStateException caught");
513 it
= cont
.listIterator();
516 assertTrue(cont
.isEmpty());
519 fail("IllegalStateException expected");
520 } catch (IllegalStateException illegalStateException
) {
521 logger
.log(Level
.FINE
, "IllegalStateException caught");
526 it
= cont
.listIterator();
532 assertTrue(cont
.isEmpty());
534 // 2 iterators, remove must not impair the other iterator
537 int size
= cont
.size();
538 it
= cont
.listIterator();
539 Iterator it2
= cont
.listIterator();
545 for( int c
= 0; c
< size
; c
++)
547 assertEquals(cont
.size(), 0);
550 @Test public void ListIterator_hasPrevious() throws Exception
552 logger
.log(Level
.INFO
, "Testing ListIterator.hasPrevious()");
553 InterfaceContainer cont
= new InterfaceContainer();
555 ListIterator it
= cont
.listIterator();
556 assertFalse(it
.hasPrevious());
558 it
= cont
.listIterator();
562 assertTrue(it
.hasPrevious());
566 @Test public void ListIterator_previous() throws Exception
568 logger
.log(Level
.INFO
, "Testing ListIterator.previous()");
569 InterfaceContainer cont
= new InterfaceContainer();
572 // go to the end of our list and list1
573 ListIterator it
= cont
.listIterator();
574 while (it
.hasNext()) {
577 ListIterator it_list1
= list1
.listIterator();
578 while (it_list1
.hasNext()) {
582 while (it
.hasPrevious())
584 assertSame(it
.previous(), it_list1
.previous());
588 fail("NoSuchElementException expected");
589 } catch (NoSuchElementException noSuchElementException
) {
590 logger
.log(Level
.FINE
, "NoSuchElementException caught");
594 @Test public void ListIterator_nextIndex() throws Exception
596 logger
.log(Level
.INFO
, "Testing ListIterator.nextIndex()");
597 InterfaceContainer cont
= new InterfaceContainer();
601 it
= cont
.listIterator();
602 assertEquals(it
.nextIndex(), 0);
604 assertEquals(it
.nextIndex(), 1);
606 assertEquals(it
.nextIndex(), 2);
609 @Test public void ListIterator_previousIndex() throws Exception
611 logger
.log(Level
.INFO
, "Testing ListIterator.previousIndex()");
612 InterfaceContainer cont
= new InterfaceContainer();
616 it
= cont
.listIterator();
617 while (it
.hasNext()) {
621 assertEquals(it
.previousIndex(), 2);
623 assertEquals(it
.previousIndex(), 1);
625 assertEquals(it
.previousIndex(), 0);
629 @SuppressWarnings("unchecked")
630 @Test public void ListIterator_add() throws Exception
632 logger
.log(Level
.INFO
, "Testing ListIterator.add()");
633 InterfaceContainer cont
= new InterfaceContainer();
635 ListIterator it
= cont
.listIterator();
637 assertEquals(cont
.size(), 1);
639 assertEquals(cont
.size(), 2);
641 assertSame(it
.previous(), obj3
);
642 assertSame(it
.previous(), obj2
);
643 assertSame(it
.previous(), obj1
);
646 @Test public void disposeAndClear() throws Exception
648 logger
.log(Level
.INFO
, "Testing InterfaceContainer.disposeAndClear");
649 InterfaceContainer cont
= new InterfaceContainer(10);
654 cont
.add(proxyObj1Weak1
);
655 cont
.add(proxyObj3TypeProv
);
656 logger
.log(Level
.FINE
, "Two proxies are called. Check the output:");
657 cont
.disposeAndClear(new com
.sun
.star
.lang
.EventObject());
658 assertEquals(obj1
.nDisposingCalled
, 1);
659 assertEquals(obj2
.nDisposingCalled
, 1);
660 assertEquals(obj3
.nDisposingCalled
, 1);