KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > foundation > SortedCollection4TestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.common.foundation;
22
23 import com.db4o.foundation.*;
24
25 import db4ounit.*;
26
27 /**
28  * @exclude
29  */

30 public class SortedCollection4TestCase implements TestCase {
31     
32     static final Comparison4 INTEGER_COMPARISON = new Comparison4() {
33         public int compare(Object JavaDoc x, Object JavaDoc y) {
34             return ((Integer JavaDoc)x).intValue()-((Integer JavaDoc)y).intValue();
35         }
36     };
37     
38     public void testAddAllAndToArray() {
39         final Object JavaDoc[] array = IntArrays4.toObjectArray(new int[] { 6, 4, 1, 2, 7, 3 });
40         
41         SortedCollection4 collection = newSortedCollection();
42         Assert.areEqual(0, collection.size());
43         collection.addAll(new ArrayIterator4(array));
44         
45         assertCollection(new int[] { 1, 2, 3, 4, 6, 7 }, collection);
46     }
47     
48     public void testToArrayOnEmptyCollection() {
49         final Object JavaDoc[] array = new Object JavaDoc[0];
50         Assert.areSame(array, newSortedCollection().toArray(array));
51     }
52     
53     public void testAddRemove() {
54         final SortedCollection4 collection = newSortedCollection();
55         collection.add(new Integer JavaDoc(3));
56         collection.add(new Integer JavaDoc(1));
57         collection.add(new Integer JavaDoc(5));
58         
59         assertCollection(new int[] { 1, 3, 5 }, collection);
60         
61         collection.remove(new Integer JavaDoc(3));
62         assertCollection(new int[] { 1, 5 }, collection);
63         
64         collection.remove(new Integer JavaDoc(1));
65         assertCollection(new int[] { 5 }, collection);
66     }
67     
68     private void assertCollection(final int[] expected, SortedCollection4 collection) {
69         Assert.areEqual(expected.length, collection.size());
70         ArrayAssert.areEqual(
71                 IntArrays4.toObjectArray(expected),
72                 collection.toArray(new Object JavaDoc[collection.size()]));
73     }
74     
75     private SortedCollection4 newSortedCollection() {
76         return new SortedCollection4(INTEGER_COMPARISON);
77     }
78
79 }
80
Popular Tags