KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > keyvalue > AbstractTestMapEntry


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.keyvalue;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Abstract tests that can be extended to test any Map.Entry implementation.
25  * Subclasses must implement {@link #makeMapEntry(Object, Object)} to return
26  * a new Map.Entry of the type being tested. Subclasses must also implement
27  * {@link #testConstructors()} to test the constructors of the Map.Entry
28  * type being tested.
29  *
30  * @since Commons Collections 3.0
31  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:40 $
32  *
33  * @author Neil O'Toole
34  */

35 public abstract class AbstractTestMapEntry extends TestCase {
36     
37     protected final String JavaDoc key = "name";
38     protected final String JavaDoc value = "duke";
39
40     /**
41      * JUnit constructor.
42      *
43      * @param testName the test name
44      */

45     public AbstractTestMapEntry(String JavaDoc testName) {
46         super(testName);
47     }
48
49     //-----------------------------------------------------------------------
50
/**
51      * Make an instance of Map.Entry with the default (null) key and value.
52      * This implementation simply calls {@link #makeMapEntry(Object, Object)}
53      * with null for key and value. Subclasses can override this method if desired.
54      */

55     public Map.Entry JavaDoc makeMapEntry() {
56         return makeMapEntry(null, null);
57     }
58
59     /**
60      * Make an instance of Map.Entry with the specified key and value.
61      * Subclasses should override this method to return a Map.Entry
62      * of the type being tested.
63      */

64     public abstract Map.Entry JavaDoc makeMapEntry(Object JavaDoc key, Object JavaDoc value);
65
66     /**
67      * Makes a Map.Entry of a type that's known to work correctly.
68      */

69     public Map.Entry JavaDoc makeKnownMapEntry() {
70         return makeKnownMapEntry(null, null);
71     }
72
73     /**
74      * Makes a Map.Entry of a type that's known to work correctly.
75      */

76     public Map.Entry JavaDoc makeKnownMapEntry(Object JavaDoc key, Object JavaDoc value) {
77         Map JavaDoc map = new HashMap JavaDoc(1);
78         map.put(key, value);
79         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) map.entrySet().iterator().next();
80         return entry;
81     }
82
83     //-----------------------------------------------------------------------
84
public void testAccessorsAndMutators() {
85         Map.Entry JavaDoc entry = makeMapEntry(key, value);
86
87         assertTrue(entry.getKey() == key);
88
89         entry.setValue(value);
90         assertTrue(entry.getValue() == value);
91
92         // check that null doesn't do anything funny
93
entry = makeMapEntry(null, null);
94         assertTrue(entry.getKey() == null);
95
96         entry.setValue(null);
97         assertTrue(entry.getValue() == null);
98     }
99
100     /**
101      * Subclasses should override this method to test the
102      * desired behaviour of the class with respect to
103      * handling of self-references.
104      *
105      */

106
107     public void testSelfReferenceHandling() {
108         // test that #setValue does not permit
109
// the MapEntry to contain itself (and thus cause infinite recursion
110
// in #hashCode and #toString)
111

112         Map.Entry JavaDoc entry = makeMapEntry();
113
114         try {
115             entry.setValue(entry);
116             fail("Should throw an IllegalArgumentException");
117         } catch (IllegalArgumentException JavaDoc iae) {
118             // expected to happen...
119

120             // check that the KVP's state has not changed
121
assertTrue(entry.getKey() == null && entry.getValue() == null);
122         }
123     }
124
125     /**
126      * Subclasses should provide tests for their constructors.
127      *
128      */

129     public abstract void testConstructors();
130
131     public void testEqualsAndHashCode() {
132         // 1. test with object data
133
Map.Entry JavaDoc e1 = makeMapEntry(key, value);
134         Map.Entry JavaDoc e2 = makeKnownMapEntry(key, value);
135
136         assertTrue(e1.equals(e1));
137         assertTrue(e2.equals(e1));
138         assertTrue(e1.equals(e2));
139         assertTrue(e1.hashCode() == e2.hashCode());
140
141         // 2. test with nulls
142
e1 = makeMapEntry();
143         e2 = makeKnownMapEntry();
144
145         assertTrue(e1.equals(e1));
146         assertTrue(e2.equals(e1));
147         assertTrue(e1.equals(e2));
148         assertTrue(e1.hashCode() == e2.hashCode());
149     }
150
151     public void testToString() {
152         Map.Entry JavaDoc entry = makeMapEntry(key, value);
153         assertTrue(entry.toString().equals(entry.getKey() + "=" + entry.getValue()));
154
155         // test with nulls
156
entry = makeMapEntry();
157         assertTrue(entry.toString().equals(entry.getKey() + "=" + entry.getValue()));
158     }
159
160 }
161
Popular Tags