1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.velocity.tools

File CollectionsToolTest.java

 

Code metrics

16
309
27
1
533
461
40
0.13
11.44
27
1.48

Classes

Class Line # Actions
CollectionsToolTest 44 309 0% 40 0
1.0100%
 

Contributing tests

This file is covered by 27 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package org.xwiki.velocity.tools;
22   
23    import java.util.ArrayList;
24    import java.util.Collection;
25    import java.util.Iterator;
26    import java.util.List;
27    import java.util.Map;
28    import java.util.Queue;
29    import java.util.Random;
30    import java.util.Set;
31    import java.util.SortedMap;
32    import java.util.SortedSet;
33    import java.util.concurrent.BlockingQueue;
34   
35    import org.junit.Assert;
36    import org.junit.Test;
37   
38    /**
39    * Unit tests for {@link CollectionsTool}.
40    *
41    * @version $Id: 5d4fa07164d431d20afa9f17f631a64a2051cd11 $
42    * @since 4.0M1
43    */
 
44    public class CollectionsToolTest
45    {
46    private CollectionsTool tool = new CollectionsTool();
47   
 
48  1 toggle @Test
49    public void testGetMap()
50    {
51  1 Map<String, String> result = this.tool.getMap();
52  1 Assert.assertNotNull("Returned null instead of a map", result);
53  1 Assert.assertTrue("Returned map wasn't initially empty", result.isEmpty());
54   
55    // Check that null values are supported
56  1 Assert.assertFalse("null value already present in the empty map", result.containsKey("nullvalue"));
57  1 Assert.assertFalse("null value already present in the empty map", result.containsValue(null));
58  1 result.put("nullvalue", null);
59  1 Assert.assertNull(result.get("nullvalue"));
60  1 Assert.assertTrue("Failed to insert null value in the map", result.containsKey("nullvalue"));
61  1 Assert.assertTrue("Failed to insert null value in the map", result.containsValue(null));
62   
63    // Check that null keys are supported
64  1 Assert.assertFalse("null key already present in the empty map", result.containsKey(null));
65  1 Assert.assertFalse("null key already present in the empty map", result.containsValue("nullkey"));
66  1 result.put(null, "nullkey");
67  1 Assert.assertEquals("Failed to insert null key in the map", "nullkey", result.get(null));
68  1 Assert.assertTrue("Failed to insert null key in the map", result.containsKey(null));
69  1 Assert.assertTrue("Failed to insert null key in the map", result.containsValue("nullkey"));
70    }
71   
 
72  1 toggle @Test
73    public void testGetSortedMap()
74    {
75  1 SortedMap<Double, Integer> result = this.tool.getSortedMap();
76  1 Assert.assertNotNull("Returned null instead of a map", result);
77  1 Assert.assertTrue("Returned map wasn't initially empty", result.isEmpty());
78   
79    // Check that the map is indeed sorted by the key
80  1 Random r = new Random();
81  101 for (int i = 0; i < 100; ++i) {
82  100 result.put(r.nextDouble(), i);
83    }
84  1 Double d = 0.0;
85  1 for (Map.Entry<Double, Integer> e : result.entrySet()) {
86  100 Assert.assertTrue("Map doesn't preserve the natural order of the elements", e.getKey() >= d);
87  100 d = e.getKey();
88    }
89    }
90   
 
91  1 toggle @Test
92    public void testGetOrderedMap()
93    {
94  1 Map<Double, Integer> result = this.tool.getOrderedMap();
95  1 Assert.assertNotNull("Returned null instead of a map", result);
96  1 Assert.assertTrue("Returned map wasn't initially empty", result.isEmpty());
97   
98    // Check that the map is indeed ordered
99  1 Random r = new Random();
100  101 for (int i = 0; i < 100; ++i) {
101  100 result.put(r.nextDouble(), i);
102    }
103  1 int i = 0;
104  1 for (Map.Entry<Double, Integer> e : result.entrySet()) {
105  100 Assert.assertEquals("Map doesn't preserve the insertion order", i++, e.getValue().intValue());
106    }
107    }
108   
 
109  1 toggle @Test
110    public void testGetSet()
111    {
112  1 Set<String> result = this.tool.getSet();
113  1 Assert.assertNotNull("Returned null instead of a set", result);
114  1 Assert.assertTrue("Returned set wasn't initially empty", result.isEmpty());
115   
116    // Check that null values are supported
117  1 Assert.assertFalse("null value already present in the empty set", result.contains(null));
118  1 Assert.assertTrue("null value already present in the empty set", result.add(null));
119  1 Assert.assertTrue("null value wasn't inserted in the set", result.contains(null));
120  1 Assert.assertFalse("null value was re-inserted in the set", result.add(null));
121  1 Assert.assertTrue("failed to remove null value from the set", result.remove(null));
122  1 Assert.assertFalse("null value present in the set after being removed", result.contains(null));
123    }
124   
 
125  1 toggle @Test
126    public void testGetSortedSet()
127    {
128  1 SortedSet<Double> result = this.tool.getSortedSet();
129  1 Assert.assertNotNull("Returned null instead of a set", result);
130  1 Assert.assertTrue("Returned set wasn't initially empty", result.isEmpty());
131   
132    // Check that the set is indeed sorted
133  1 Random r = new Random();
134  101 for (int i = 0; i < 100; ++i) {
135  100 result.add(r.nextDouble());
136    }
137  1 Double d = 0.0;
138  1 for (Double e : result) {
139  100 Assert.assertTrue("Set doesn't preserve the natural order of the elements", e >= d);
140  100 d = e;
141    }
142    }
143   
 
144  1 toggle @Test
145    public void testGetOrderedSet()
146    {
147  1 Set<Double> result = this.tool.getOrderedSet();
148  1 Assert.assertNotNull("Returned null instead of a set", result);
149  1 Assert.assertTrue("Returned set wasn't initially empty", result.isEmpty());
150   
151    // Check that the set is indeed ordered
152  1 Random r = new Random();
153  1 List<Double> values = new ArrayList<Double>(150);
154  101 for (int i = 0; i < 100; ++i) {
155  100 Double d = r.nextDouble();
156  100 result.add(d);
157  100 values.add(d);
158    }
159  1 Iterator<Double> valueIterator = values.iterator();
160  1 for (Double e : result) {
161  100 Assert.assertEquals("Set doesn't preserve the insertion order", valueIterator.next(), e);
162    }
163    }
164   
 
165  1 toggle @Test
166    public void testGetArrayList()
167    {
168  1 List<String> result = this.tool.getArrayList();
169  1 Assert.assertNotNull("Returned null instead of a list", result);
170  1 Assert.assertTrue("Returned list wasn't initially empty", result.isEmpty());
171    }
172   
 
173  1 toggle @Test
174    public void testGetLinkedList()
175    {
176  1 List<String> result = this.tool.getLinkedList();
177  1 Assert.assertNotNull("Returned null instead of a list", result);
178  1 Assert.assertTrue("Returned list wasn't initially empty", result.isEmpty());
179    }
180   
 
181  1 toggle @Test
182    public void testGetQueue()
183    {
184  1 Queue<String> result = this.tool.getQueue();
185  1 Assert.assertNotNull("Returned null instead of a queue", result);
186  1 Assert.assertTrue("Returned queue wasn't initially empty", result.isEmpty());
187  1 Assert.assertNull(result.poll());
188    }
189   
 
190  1 toggle @Test
191    public void testGetBlockingQueue()
192    {
193  1 BlockingQueue<String> result = this.tool.getBlockingQueue();
194  1 Assert.assertNotNull("Returned null instead of a queue", result);
195  1 Assert.assertTrue("Returned queue wasn't initially empty", result.isEmpty());
196    }
197   
 
198  1 toggle @Test
199    public void testGetPriorityQueue()
200    {
201  1 Queue<Double> result = this.tool.getPriorityQueue();
202  1 Assert.assertNotNull("Returned null instead of a queue", result);
203  1 Assert.assertTrue("Returned queue wasn't initially empty", result.isEmpty());
204   
205    // Check that the queue is indeed prioritized
206  1 Random r = new Random();
207  101 for (int i = 0; i < 100; ++i) {
208  100 result.add(r.nextDouble());
209    }
210  1 Double d = 0.0;
211  101 while (!result.isEmpty()) {
212  100 Double e = result.poll();
213  100 Assert.assertTrue("Queue doesn't preserve the natural order of the elements", e >= d);
214  100 d = e;
215    }
216    }
217   
 
218  1 toggle @Test
219    public void testUnmodifiableList()
220    {
221  1 List<String> original = this.tool.getLinkedList();
222  1 original.add("one");
223  1 original.add("two");
224  1 original.add("three");
225  1 List<String> result = this.tool.unmodifiable(original);
226  1 Assert.assertNotNull("Returned null instead of a proper unmodifiable wrapper", result);
227  1 Assert.assertFalse("Returned wrapper was empty", result.isEmpty());
228  1 Assert.assertEquals("Wrong size of the wrapper list", 3, result.size());
229  1 boolean failed = false;
230  1 try {
231  1 result.add("four");
232    } catch (UnsupportedOperationException ex) {
233  1 failed = true;
234    }
235  1 Assert.assertTrue("Shouldn't be allowed to add new items", failed);
236  1 original.add("four");
237  1 Assert.assertEquals("Wrapper list doesn't show changes in the original list", 4, result.size());
238  1 Iterator<String> it = result.iterator();
239  1 failed = false;
240  1 try {
241  1 it.remove();
242    } catch (UnsupportedOperationException ex) {
243  1 failed = true;
244    }
245  1 Assert.assertTrue("Shouldn't be allowed to remove items using the iterator", failed);
246    }
247   
 
248  1 toggle @Test
249    public void testUnmodifiableMap()
250    {
251  1 Map<String, Integer> original = this.tool.getMap();
252  1 original.put("one", 1);
253  1 original.put("two", 2);
254  1 original.put("three", 3);
255  1 Map<String, Integer> result = this.tool.unmodifiable(original);
256  1 Assert.assertNotNull("Returned null instead of a proper unmodifiable wrapper", result);
257  1 Assert.assertFalse("Returned wrapper was empty", result.isEmpty());
258  1 Assert.assertEquals("Wrong size of the wrapper map", 3, result.size());
259  1 boolean failed = false;
260  1 try {
261  1 result.put("four", 4);
262    } catch (UnsupportedOperationException ex) {
263  1 failed = true;
264    }
265  1 Assert.assertTrue("Shouldn't be allowed to add new items", failed);
266  1 original.put("four", 4);
267  1 Assert.assertEquals("Wrapper map doesn't show changes in the original map", 4, result.size());
268    }
269   
 
270  1 toggle @Test
271    public void testUnmodifiableSet()
272    {
273  1 SortedSet<Integer> original = this.tool.getSortedSet();
274  101 for (int i = 0; i < 100; ++i) {
275  100 original.add(150 - i);
276    }
277  1 Set<Integer> result = this.tool.unmodifiable(original);
278  1 Assert.assertNotNull("Returned null instead of a proper unmodifiable wrapper", result);
279  1 Assert.assertFalse("Returned wrapper was empty", result.isEmpty());
280  1 Assert.assertEquals("Wrong size of the wrapper set", 100, result.size());
281  1 Assert.assertEquals("Wrapper set doesn't preserve the order of the original set", 51,
282    result.iterator().next().intValue());
283  1 boolean failed = false;
284  1 try {
285  1 result.add(307);
286    } catch (UnsupportedOperationException ex) {
287  1 failed = true;
288    }
289  1 Assert.assertTrue("Shouldn't be allowed to add new items", failed);
290  1 original.add(42);
291  1 Assert.assertEquals("Wrapper set doesn't show changes in the original set", 101, result.size());
292  1 Assert.assertEquals("Wrapper set doesn't preserve the order of the original set", 42,
293    result.iterator().next().intValue());
294    }
295   
 
296  1 toggle @Test
297    public void testUnmodifiableCollection()
298    {
299  1 Collection<Integer> original = this.tool.getPriorityQueue();
300  101 for (int i = 0; i < 100; ++i) {
301  100 original.add(150 - i);
302    }
303  1 Collection<Integer> result = this.tool.unmodifiable(original);
304  1 Assert.assertNotNull("Returned null instead of a proper unmodifiable wrapper", result);
305  1 Assert.assertFalse("Returned wrapper was empty", result.isEmpty());
306  1 Assert.assertEquals("Wrong size of the wrapper collection", 100, result.size());
307  1 Assert.assertEquals("Wrapper collection doesn't preserve the order of the original collection", 51,
308    result.iterator().next().intValue());
309  1 boolean failed = false;
310  1 try {
311  1 result.add(307);
312    } catch (UnsupportedOperationException ex) {
313  1 failed = true;
314    }
315  1 Assert.assertTrue("Shouldn't be allowed to add new items", failed);
316  1 original.add(42);
317  1 Assert.assertEquals("Wrapper set doesn't show changes in the original set", 101, result.size());
318  1 Assert.assertEquals("Wrapper set doesn't preserve the order of the original set", 42,
319    result.iterator().next().intValue());
320    }
321   
 
322  1 toggle @Test
323    public void testNullUnmodifiable()
324    {
325  1 Assert.assertNull(this.tool.unmodifiable((List<Object>) null));
326  1 Assert.assertNull(this.tool.unmodifiable((Set<Object>) null));
327  1 Assert.assertNull(this.tool.unmodifiable((Map<Object, Object>) null));
328  1 Assert.assertNull(this.tool.unmodifiable((Collection<Object>) null));
329    }
330   
 
331  1 toggle @Test
332    public void testSetUnion()
333    {
334  1 Set<String> set1 = this.tool.getSet();
335  1 set1.add("one");
336  1 set1.add("two");
337  1 set1.add("three");
338  1 Set<String> set2 = this.tool.getSet();
339  1 set2.add("three");
340  1 set2.add("four");
341  1 set2.add("five");
342  1 Collection<String> result = this.tool.union(set1, set2);
343  1 Assert.assertNotNull("Returned null instead of a proper collection", result);
344  1 Assert.assertFalse("Returned union was empty", result.isEmpty());
345  1 Assert.assertEquals("Wrong size of the union collection", 5, result.size());
346  1 Assert.assertTrue("Not all elements from the first set were included in the union", result.containsAll(set1));
347  1 Assert.assertTrue("Not all elements from the second set were included in the union", result.containsAll(set2));
348    }
349   
 
350  1 toggle @Test
351    public void testListUnion()
352    {
353  1 List<String> list1 = this.tool.getLinkedList();
354  1 list1.add("one");
355  1 list1.add("two");
356  1 list1.add("three");
357  1 list1.add("three");
358  1 list1.add("three");
359  1 List<String> list2 = this.tool.getArrayList();
360  1 list2.add("three");
361  1 list2.add("four");
362  1 list2.add("four");
363  1 list2.add("five");
364  1 Collection<String> result = this.tool.union(list1, list2);
365  1 Assert.assertNotNull("Returned null instead of a proper collection", result);
366  1 Assert.assertFalse("Returned union was empty", result.isEmpty());
367  1 Assert.assertEquals("Wrong size of the union collection", 8, result.size());
368  1 Assert.assertTrue("Not all elements from the first list were included in the union", result.containsAll(list1));
369  1 Assert.assertTrue("Not all elements from the second list were included", result.containsAll(list2));
370    }
371   
 
372  1 toggle @Test
373    public void testNullUnion()
374    {
375  1 List<String> list = this.tool.getLinkedList();
376  1 list.add("one");
377  1 list.add("two");
378  1 list.add("three");
379  1 Assert.assertEquals(list, this.tool.union(list, null));
380  1 Assert.assertEquals(list, this.tool.union(null, list));
381  1 Assert.assertNull(this.tool.union(null, null));
382    }
383   
 
384  1 toggle @Test
385    public void testSetIntersection()
386    {
387  1 Set<String> set1 = this.tool.getSet();
388  1 set1.add("one");
389  1 set1.add("two");
390  1 set1.add("three");
391  1 Set<String> set2 = this.tool.getSet();
392  1 set2.add("three");
393  1 set2.add("four");
394  1 set2.add("five");
395  1 Collection<String> result = this.tool.intersection(set1, set2);
396  1 Assert.assertNotNull("Returned null instead of a proper collection", result);
397  1 Assert.assertFalse("Returned intersection was empty", result.isEmpty());
398  1 Assert.assertEquals("Wrong size of the intersection collection", 1, result.size());
399  1 Assert.assertEquals("Wrong element included in the intersection", "three", result.iterator().next());
400    }
401   
 
402  1 toggle @Test
403    public void testListIntersection()
404    {
405  1 List<String> list1 = this.tool.getLinkedList();
406  1 list1.add("one");
407  1 list1.add("two");
408  1 list1.add("three");
409  1 list1.add("three");
410  1 list1.add("three");
411  1 List<String> list2 = this.tool.getArrayList();
412  1 list2.add("three");
413  1 list2.add("three");
414  1 list2.add("four");
415  1 list2.add("five");
416  1 Collection<String> result = this.tool.intersection(list1, list2);
417  1 Assert.assertNotNull("Returned null instead of a proper collection", result);
418  1 Assert.assertFalse("Returned intersection was empty", result.isEmpty());
419  1 Assert.assertEquals("Wrong size of the intersection collection", 2, result.size());
420  1 Iterator<String> it = result.iterator();
421  1 Assert.assertEquals("Wrong element included in the intersection", "three", it.next());
422  1 Assert.assertEquals("Wrong element included in the intersection", "three", it.next());
423  1 Assert.assertFalse("Wrong size of the intersection collection", it.hasNext());
424    }
425   
 
426  1 toggle @Test
427    public void testNullIntersection()
428    {
429  1 List<String> list = this.tool.getLinkedList();
430  1 list.add("one");
431  1 list.add("two");
432  1 list.add("three");
433  1 Assert.assertEquals(list, this.tool.intersection(list, null));
434  1 Assert.assertEquals(list, this.tool.intersection(null, list));
435  1 Assert.assertNull(this.tool.intersection(null, null));
436    }
437   
 
438  1 toggle @Test
439    public void testSetDisjunction()
440    {
441  1 Set<String> set1 = this.tool.getSet();
442  1 set1.add("one");
443  1 set1.add("two");
444  1 set1.add("three");
445  1 Set<String> set2 = this.tool.getSet();
446  1 set2.add("three");
447  1 set2.add("four");
448  1 set2.add("five");
449  1 Collection<String> result = this.tool.disjunction(set1, set2);
450  1 Assert.assertNotNull("Returned null instead of a proper collection", result);
451  1 Assert.assertFalse("Returned disjunction was empty", result.isEmpty());
452  1 Assert.assertEquals("Wrong size of the intersection collection", 4, result.size());
453  1 Assert.assertTrue("Missing element from the disjunction", result.contains("one"));
454  1 Assert.assertTrue("Missing element from the disjunction", result.contains("two"));
455  1 Assert.assertFalse("Wrong element included in the disjunction", result.contains("three"));
456  1 Assert.assertTrue("Missing element from the disjunction", result.contains("four"));
457  1 Assert.assertTrue("Missing element from the disjunction", result.contains("five"));
458    }
459   
 
460  1 toggle @Test
461    public void testListDisjunction()
462    {
463  1 List<String> list1 = this.tool.getLinkedList();
464  1 list1.add("one");
465  1 list1.add("two");
466  1 list1.add("three");
467  1 list1.add("three");
468  1 list1.add("three");
469  1 list1.add("four");
470  1 List<String> list2 = this.tool.getArrayList();
471  1 list2.add("three");
472  1 list2.add("three");
473  1 list2.add("four");
474  1 list2.add("five");
475  1 Collection<String> result = this.tool.disjunction(list1, list2);
476  1 Assert.assertNotNull("Returned null instead of a proper collection", result);
477  1 Assert.assertFalse("Returned disjunction was empty", result.isEmpty());
478  1 Assert.assertEquals("Wrong size of the disjunction collection", 4, result.size());
479  1 Assert.assertTrue("Missing element from the disjunction", result.contains("one"));
480  1 Assert.assertTrue("Missing element from the disjunction", result.contains("two"));
481  1 Assert.assertTrue("Missing element from the disjunction", result.contains("three"));
482  1 Assert.assertFalse("Wrong element included in the disjunction", result.contains("four"));
483  1 Assert.assertTrue("Missing element from the disjunction", result.contains("five"));
484    }
485   
 
486  1 toggle @Test
487    public void testNullDisjunction()
488    {
489  1 List<String> list = this.tool.getLinkedList();
490  1 list.add("one");
491  1 list.add("two");
492  1 list.add("three");
493  1 Assert.assertEquals(list, this.tool.disjunction(list, null));
494  1 Assert.assertEquals(list, this.tool.disjunction(null, list));
495  1 Assert.assertNull(this.tool.disjunction(null, null));
496    }
497   
 
498  1 toggle @Test
499    public void testReverse()
500    {
501  1 List<String> list = this.tool.getLinkedList();
502  1 list.add("one");
503  1 list.add("two");
504  1 list.add("three");
505  1 Assert.assertTrue("Failed to reverse list", this.tool.reverse(list));
506  1 Assert.assertEquals("List wasn't properly reversed", "three", list.get(0));
507  1 Assert.assertEquals("List wasn't properly reversed", "two", list.get(1));
508  1 Assert.assertEquals("List wasn't properly reversed", "one", list.get(2));
509  1 List<String> readonly = this.tool.unmodifiable(list);
510  1 Assert.assertFalse("Unmodifiable list wrongly reversed", this.tool.reverse(readonly));
511  1 Assert.assertEquals("Unmodifiable list was changed", "three", list.get(0));
512  1 Assert.assertFalse("Reversed a null list?", this.tool.reverse(null));
513    }
514   
 
515  1 toggle @Test
516    public void testSort()
517    {
518  1 List<String> list = this.tool.getLinkedList();
519  1 list.add("one");
520  1 list.add("two");
521  1 list.add("three");
522  1 Assert.assertTrue("Failed to sort the list", this.tool.sort(list));
523  1 Assert.assertEquals("List wasn't properly sorted", "one", list.get(0));
524  1 Assert.assertEquals("List wasn't properly sorted", "three", list.get(1));
525  1 Assert.assertEquals("List wasn't properly sorted", "two", list.get(2));
526  1 this.tool.reverse(list);
527  1 List<String> readonly = this.tool.unmodifiable(list);
528  1 Assert.assertFalse("Unmodifiable list wrongly sorted", this.tool.sort(readonly));
529  1 Assert.assertEquals("Unmodifiable list was changed", "two", list.get(0));
530  1 list = null;
531  1 Assert.assertFalse("Sorted a null list?", this.tool.sort(list));
532    }
533    }