KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
19
20 import junit.framework.Test;
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.collections.KeyValue;
24
25 /**
26  * Test the DefaultMapEntry class.
27  *
28  * @since Commons Collections 3.0
29  * @version $Revision: 1.3 $ $Date: 2004/02/18 01:20:40 $
30  *
31  * @author Neil O'Toole
32  */

33 public class TestDefaultMapEntry extends AbstractTestMapEntry {
34
35     public TestDefaultMapEntry(String JavaDoc testName) {
36         super(testName);
37
38     }
39
40     public static void main(String JavaDoc[] args) {
41         junit.textui.TestRunner.run(TestDefaultMapEntry.class);
42     }
43
44     public static Test suite() {
45         return new TestSuite(TestDefaultMapEntry.class);
46     }
47
48     //-----------------------------------------------------------------------
49
/**
50      * Make an instance of Map.Entry with the default (null) key and value.
51      * Subclasses should override this method to return a Map.Entry
52      * of the type being tested.
53      */

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

63     public Map.Entry JavaDoc makeMapEntry(Object JavaDoc key, Object JavaDoc value) {
64         return new DefaultMapEntry(key, value);
65     }
66
67     //-----------------------------------------------------------------------
68
/**
69      * Subclasses should override this method.
70      *
71      */

72     public void testConstructors() {
73         // 1. test key-value constructor
74
Map.Entry JavaDoc entry = new DefaultMapEntry(key, value);
75         assertSame(key, entry.getKey());
76         assertSame(value, entry.getValue());
77
78         // 2. test pair constructor
79
KeyValue pair = new DefaultKeyValue(key, value);
80         assertSame(key, pair.getKey());
81         assertSame(value, pair.getValue());
82
83         // 3. test copy constructor
84
Map.Entry JavaDoc entry2 = new DefaultMapEntry(entry);
85         assertSame(key, entry2.getKey());
86         assertSame(value, entry2.getValue());
87
88         // test that the objects are independent
89
entry.setValue(null);
90         assertSame(value, entry2.getValue());
91     }
92
93     public void testSelfReferenceHandling() {
94         Map.Entry JavaDoc entry = makeMapEntry();
95
96         try {
97             entry.setValue(entry);
98             assertSame(entry, entry.getValue());
99
100         } catch (Exception JavaDoc e) {
101             fail("This Map.Entry implementation supports value self-reference.");
102         }
103     }
104
105 }
106
Popular Tags