KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bidimap > AbstractTestOrderedBidiMap


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.bidimap;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
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 /**
31  * Abstract test class for {@link OrderedBidiMap} methods and contracts.
32  *
33  * @version $Revision: 1.6 $ $Date: 2004/02/18 01:20:40 $
34  *
35  * @author Matthew Hawthorne
36  * @author Stephen Colebourne
37  */

38 public abstract class AbstractTestOrderedBidiMap extends AbstractTestBidiMap {
39
40     public AbstractTestOrderedBidiMap(String JavaDoc testName) {
41         super(testName);
42     }
43
44     public AbstractTestOrderedBidiMap() {
45         super();
46     }
47
48     //-----------------------------------------------------------------------
49
public void testFirstKey() {
50         resetEmpty();
51         OrderedBidiMap bidi = (OrderedBidiMap) map;
52         try {
53             bidi.firstKey();
54             fail();
55         } catch (NoSuchElementException JavaDoc ex) {}
56         
57         resetFull();
58         bidi = (OrderedBidiMap) map;
59         Object JavaDoc 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 JavaDoc ex) {}
70         
71         resetFull();
72         bidi = (OrderedBidiMap) map;
73         Object JavaDoc confirmedLast = null;
74         for (Iterator JavaDoc it = confirmed.keySet().iterator(); it.hasNext();) {
75             confirmedLast = it.next();
76         }
77         assertEquals(confirmedLast, bidi.lastKey());
78     }
79
80     //-----------------------------------------------------------------------
81
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)); // this is allowed too
88
} catch (NullPointerException JavaDoc ex) {}
89         } else {
90             assertEquals(null, bidi.nextKey(null));
91         }
92         
93         resetFull();
94         bidi = (OrderedBidiMap) map;
95         Iterator JavaDoc it = confirmed.keySet().iterator();
96         Object JavaDoc confirmedLast = it.next();
97         while (it.hasNext()) {
98             Object JavaDoc 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 JavaDoc 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)); // this is allowed too
121
} catch (NullPointerException JavaDoc ex) {}
122         } else {
123             assertEquals(null, bidi.previousKey(null));
124         }
125         
126         resetFull();
127         bidi = (OrderedBidiMap) map;
128         List JavaDoc list = new ArrayList JavaDoc(confirmed.keySet());
129         Collections.reverse(list);
130         Iterator JavaDoc it = list.iterator();
131         Object JavaDoc confirmedLast = it.next();
132         while (it.hasNext()) {
133             Object JavaDoc 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 JavaDoc ex) {}
144         } else {
145             assertEquals(null, bidi.previousKey(null));
146         }
147     }
148     
149     //-----------------------------------------------------------------------
150
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 JavaDoc[] 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 JavaDoc getMap() {
182             // assumes makeFullMapIterator() called first
183
return AbstractTestOrderedBidiMap.this.map;
184         }
185         
186         public Map JavaDoc getConfirmedMap() {
187             // assumes makeFullMapIterator() called first
188
return AbstractTestOrderedBidiMap.this.confirmed;
189         }
190         
191         public void verify() {
192             super.verify();
193             AbstractTestOrderedBidiMap.this.verify();
194         }
195     }
196     
197 }
198
Popular Tags