Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.

Also see the following revisions:

    ------------------------------------------------------------------------
    r593347 | skestle | 2007-11-08 14:49:02 -0800 (Thu, 08 Nov 2007) | 3 lines
    
    Added initial IndexedCollection - still needs quite a bit of work - see TODO comments
    
    JIRA: COLLECTIONS-275
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815149 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:58:05 +00:00
parent 1cece986b5
commit 5497aca53a

View File

@ -0,0 +1,73 @@
package org.apache.commons.collections;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
public class TestIndexedCollection extends AbstractDecoratedCollectionTest<String> {
private IndexedCollection<Integer, String> indexed;
@Before
public void setUp() throws Exception {
indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() {
public Integer transform(String input) {
return Integer.parseInt(input);
}
});
decorated = indexed;
}
@Test
public void addedObjectsCanBeRetrievedByKey() throws Exception {
decorated.add("12");
decorated.add("16");
decorated.add("1");
decorated.addAll(asList("2","3","4"));
assertEquals("12", indexed.get(12));
assertEquals("16", indexed.get(16));
assertEquals("1", indexed.get(1));
assertEquals("2", indexed.get(2));
assertEquals("3", indexed.get(3));
assertEquals("4", indexed.get(4));
}
@Test(expected=IllegalArgumentException.class)
public void ensureDuplicateObjectsCauseException() throws Exception {
decorated.add("1");
decorated.add("1");
}
@Test
public void decoratedCollectionIsIndexedOnCreation() throws Exception {
original.add("1");
original.add("2");
original.add("3");
indexed = IndexedCollection.uniqueIndexedCollection(original, new Transformer<String, Integer>() {
public Integer transform(String input) {
return Integer.parseInt(input);
}
});
assertEquals("1", indexed.get(1));
assertEquals("2", indexed.get(2));
assertEquals("3", indexed.get(3));
}
@Test
public void reindexUpdatesIndexWhenTheDecoratedCollectionIsModifiedSeparately() throws Exception {
original.add("1");
original.add("2");
original.add("3");
assertNull(indexed.get(1));
assertNull(indexed.get(2));
assertNull(indexed.get(3));
indexed.reindex();
assertEquals("1", indexed.get(1));
assertEquals("2", indexed.get(2));
assertEquals("3", indexed.get(3));
}
}