1 16 package org.apache.commons.collections.bidimap; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.NoSuchElementException ; 24 25 import org.apache.commons.collections.BulkTest; 26 import org.apache.commons.collections.MapIterator; 27 import org.apache.commons.collections.OrderedBidiMap; 28 import org.apache.commons.collections.iterators.AbstractTestMapIterator; 29 30 38 public abstract class AbstractTestOrderedBidiMap extends AbstractTestBidiMap { 39 40 public AbstractTestOrderedBidiMap(String testName) { 41 super(testName); 42 } 43 44 public AbstractTestOrderedBidiMap() { 45 super(); 46 } 47 48 public void testFirstKey() { 50 resetEmpty(); 51 OrderedBidiMap bidi = (OrderedBidiMap) map; 52 try { 53 bidi.firstKey(); 54 fail(); 55 } catch (NoSuchElementException ex) {} 56 57 resetFull(); 58 bidi = (OrderedBidiMap) map; 59 Object confirmedFirst = confirmed.keySet().iterator().next(); 60 assertEquals(confirmedFirst, bidi.firstKey()); 61 } 62 63 public void testLastKey() { 64 resetEmpty(); 65 OrderedBidiMap bidi = (OrderedBidiMap) map; 66 try { 67 bidi.lastKey(); 68 fail(); 69 } catch (NoSuchElementException ex) {} 70 71 resetFull(); 72 bidi = (OrderedBidiMap) map; 73 Object confirmedLast = null; 74 for (Iterator it = confirmed.keySet().iterator(); it.hasNext();) { 75 confirmedLast = it.next(); 76 } 77 assertEquals(confirmedLast, bidi.lastKey()); 78 } 79 80 public void testNextKey() { 82 resetEmpty(); 83 OrderedBidiMap bidi = (OrderedBidiMap) map; 84 assertEquals(null, bidi.nextKey(getOtherKeys()[0])); 85 if (isAllowNullKey() == false) { 86 try { 87 assertEquals(null, bidi.nextKey(null)); } catch (NullPointerException ex) {} 89 } else { 90 assertEquals(null, bidi.nextKey(null)); 91 } 92 93 resetFull(); 94 bidi = (OrderedBidiMap) map; 95 Iterator it = confirmed.keySet().iterator(); 96 Object confirmedLast = it.next(); 97 while (it.hasNext()) { 98 Object confirmedObject = it.next(); 99 assertEquals(confirmedObject, bidi.nextKey(confirmedLast)); 100 confirmedLast = confirmedObject; 101 } 102 assertEquals(null, bidi.nextKey(confirmedLast)); 103 104 if (isAllowNullKey() == false) { 105 try { 106 bidi.nextKey(null); 107 fail(); 108 } catch (NullPointerException ex) {} 109 } else { 110 assertEquals(null, bidi.nextKey(null)); 111 } 112 } 113 114 public void testPreviousKey() { 115 resetEmpty(); 116 OrderedBidiMap bidi = (OrderedBidiMap) map; 117 assertEquals(null, bidi.previousKey(getOtherKeys()[0])); 118 if (isAllowNullKey() == false) { 119 try { 120 assertEquals(null, bidi.previousKey(null)); } catch (NullPointerException ex) {} 122 } else { 123 assertEquals(null, bidi.previousKey(null)); 124 } 125 126 resetFull(); 127 bidi = (OrderedBidiMap) map; 128 List list = new ArrayList (confirmed.keySet()); 129 Collections.reverse(list); 130 Iterator it = list.iterator(); 131 Object confirmedLast = it.next(); 132 while (it.hasNext()) { 133 Object confirmedObject = it.next(); 134 assertEquals(confirmedObject, bidi.previousKey(confirmedLast)); 135 confirmedLast = confirmedObject; 136 } 137 assertEquals(null, bidi.previousKey(confirmedLast)); 138 139 if (isAllowNullKey() == false) { 140 try { 141 bidi.previousKey(null); 142 fail(); 143 } catch (NullPointerException ex) {} 144 } else { 145 assertEquals(null, bidi.previousKey(null)); 146 } 147 } 148 149 public BulkTest bulkTestOrderedMapIterator() { 151 return new TestBidiOrderedMapIterator(); 152 } 153 154 public class TestBidiOrderedMapIterator extends AbstractTestMapIterator { 155 public TestBidiOrderedMapIterator() { 156 super("TestBidiOrderedMapIterator"); 157 } 158 159 public Object [] addSetValues() { 160 return AbstractTestOrderedBidiMap.this.getNewSampleValues(); 161 } 162 163 public boolean supportsRemove() { 164 return AbstractTestOrderedBidiMap.this.isRemoveSupported(); 165 } 166 167 public boolean supportsSetValue() { 168 return AbstractTestOrderedBidiMap.this.isSetValueSupported(); 169 } 170 171 public MapIterator makeEmptyMapIterator() { 172 resetEmpty(); 173 return ((OrderedBidiMap) AbstractTestOrderedBidiMap.this.map).orderedMapIterator(); 174 } 175 176 public MapIterator makeFullMapIterator() { 177 resetFull(); 178 return ((OrderedBidiMap) AbstractTestOrderedBidiMap.this.map).orderedMapIterator(); 179 } 180 181 public Map getMap() { 182 return AbstractTestOrderedBidiMap.this.map; 184 } 185 186 public Map getConfirmedMap() { 187 return AbstractTestOrderedBidiMap.this.confirmed; 189 } 190 191 public void verify() { 192 super.verify(); 193 AbstractTestOrderedBidiMap.this.verify(); 194 } 195 } 196 197 } 198 | Popular Tags |