1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.collections.bidimap; 18 19 import java.util.ArrayList; 20 import java.util.Collections; 21 import java.util.Iterator; 22 import java.util.List; 23 import java.util.Map; 24 import java.util.NoSuchElementException; 25 26 import org.apache.commons.collections.BulkTest; 27 import org.apache.commons.collections.MapIterator; 28 import org.apache.commons.collections.OrderedBidiMap; 29 import org.apache.commons.collections.iterators.AbstractTestMapIterator; 30 31 /** 32 * Abstract test class for {@link OrderedBidiMap} methods and contracts. 33 * 34 * @version $Revision: 646780 $ $Date: 2008-04-10 14:48:07 +0200 (Thu, 10 Apr 2008) $ 35 * 36 * @author Matthew Hawthorne 37 * @author Stephen Colebourne 38 */ 39 public abstract class AbstractTestOrderedBidiMap extends AbstractTestBidiMap { 40 AbstractTestOrderedBidiMap(String testName)41 public AbstractTestOrderedBidiMap(String testName) { 42 super(testName); 43 } 44 AbstractTestOrderedBidiMap()45 public AbstractTestOrderedBidiMap() { 46 super(); 47 } 48 49 //----------------------------------------------------------------------- testFirstKey()50 public void testFirstKey() { 51 resetEmpty(); 52 OrderedBidiMap bidi = (OrderedBidiMap) map; 53 try { 54 bidi.firstKey(); 55 fail(); 56 } catch (NoSuchElementException ex) {} 57 58 resetFull(); 59 bidi = (OrderedBidiMap) map; 60 Object confirmedFirst = confirmed.keySet().iterator().next(); 61 assertEquals(confirmedFirst, bidi.firstKey()); 62 } 63 testLastKey()64 public void testLastKey() { 65 resetEmpty(); 66 OrderedBidiMap bidi = (OrderedBidiMap) map; 67 try { 68 bidi.lastKey(); 69 fail(); 70 } catch (NoSuchElementException ex) {} 71 72 resetFull(); 73 bidi = (OrderedBidiMap) map; 74 Object confirmedLast = null; 75 for (Iterator it = confirmed.keySet().iterator(); it.hasNext();) { 76 confirmedLast = it.next(); 77 } 78 assertEquals(confirmedLast, bidi.lastKey()); 79 } 80 81 //----------------------------------------------------------------------- testNextKey()82 public void testNextKey() { 83 resetEmpty(); 84 OrderedBidiMap bidi = (OrderedBidiMap) map; 85 assertEquals(null, bidi.nextKey(getOtherKeys()[0])); 86 if (isAllowNullKey() == false) { 87 try { 88 assertEquals(null, bidi.nextKey(null)); // this is allowed too 89 } catch (NullPointerException ex) {} 90 } else { 91 assertEquals(null, bidi.nextKey(null)); 92 } 93 94 resetFull(); 95 bidi = (OrderedBidiMap) map; 96 Iterator it = confirmed.keySet().iterator(); 97 Object confirmedLast = it.next(); 98 while (it.hasNext()) { 99 Object confirmedObject = it.next(); 100 assertEquals(confirmedObject, bidi.nextKey(confirmedLast)); 101 confirmedLast = confirmedObject; 102 } 103 assertEquals(null, bidi.nextKey(confirmedLast)); 104 105 if (isAllowNullKey() == false) { 106 try { 107 bidi.nextKey(null); 108 fail(); 109 } catch (NullPointerException ex) {} 110 } else { 111 assertEquals(null, bidi.nextKey(null)); 112 } 113 } 114 testPreviousKey()115 public void testPreviousKey() { 116 resetEmpty(); 117 OrderedBidiMap bidi = (OrderedBidiMap) map; 118 assertEquals(null, bidi.previousKey(getOtherKeys()[0])); 119 if (isAllowNullKey() == false) { 120 try { 121 assertEquals(null, bidi.previousKey(null)); // this is allowed too 122 } catch (NullPointerException ex) {} 123 } else { 124 assertEquals(null, bidi.previousKey(null)); 125 } 126 127 resetFull(); 128 bidi = (OrderedBidiMap) map; 129 List list = new ArrayList(confirmed.keySet()); 130 Collections.reverse(list); 131 Iterator it = list.iterator(); 132 Object confirmedLast = it.next(); 133 while (it.hasNext()) { 134 Object confirmedObject = it.next(); 135 assertEquals(confirmedObject, bidi.previousKey(confirmedLast)); 136 confirmedLast = confirmedObject; 137 } 138 assertEquals(null, bidi.previousKey(confirmedLast)); 139 140 if (isAllowNullKey() == false) { 141 try { 142 bidi.previousKey(null); 143 fail(); 144 } catch (NullPointerException ex) {} 145 } else { 146 assertEquals(null, bidi.previousKey(null)); 147 } 148 } 149 150 //----------------------------------------------------------------------- bulkTestOrderedMapIterator()151 public BulkTest bulkTestOrderedMapIterator() { 152 return new TestBidiOrderedMapIterator(); 153 } 154 155 public class TestBidiOrderedMapIterator extends AbstractTestMapIterator { TestBidiOrderedMapIterator()156 public TestBidiOrderedMapIterator() { 157 super("TestBidiOrderedMapIterator"); 158 } 159 addSetValues()160 public Object[] addSetValues() { 161 return AbstractTestOrderedBidiMap.this.getNewSampleValues(); 162 } 163 supportsRemove()164 public boolean supportsRemove() { 165 return AbstractTestOrderedBidiMap.this.isRemoveSupported(); 166 } 167 supportsSetValue()168 public boolean supportsSetValue() { 169 return AbstractTestOrderedBidiMap.this.isSetValueSupported(); 170 } 171 makeEmptyMapIterator()172 public MapIterator makeEmptyMapIterator() { 173 resetEmpty(); 174 return ((OrderedBidiMap) AbstractTestOrderedBidiMap.this.map).orderedMapIterator(); 175 } 176 makeFullMapIterator()177 public MapIterator makeFullMapIterator() { 178 resetFull(); 179 return ((OrderedBidiMap) AbstractTestOrderedBidiMap.this.map).orderedMapIterator(); 180 } 181 getMap()182 public Map getMap() { 183 // assumes makeFullMapIterator() called first 184 return AbstractTestOrderedBidiMap.this.map; 185 } 186 getConfirmedMap()187 public Map getConfirmedMap() { 188 // assumes makeFullMapIterator() called first 189 return AbstractTestOrderedBidiMap.this.confirmed; 190 } 191 verify()192 public void verify() { 193 super.verify(); 194 AbstractTestOrderedBidiMap.this.verify(); 195 } 196 } 197 198 } 199