001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.keyvalue;
017:
018: import java.util.Map;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.collections.KeyValue;
024:
025: /**
026: * Test the DefaultMapEntry class.
027: *
028: * @since Commons Collections 3.0
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Neil O'Toole
032: */
033: public class TestDefaultMapEntry extends AbstractTestMapEntry {
034:
035: public TestDefaultMapEntry(String testName) {
036: super (testName);
037:
038: }
039:
040: public static void main(String[] args) {
041: junit.textui.TestRunner.run(TestDefaultMapEntry.class);
042: }
043:
044: public static Test suite() {
045: return new TestSuite(TestDefaultMapEntry.class);
046: }
047:
048: //-----------------------------------------------------------------------
049: /**
050: * Make an instance of Map.Entry with the default (null) key and value.
051: * Subclasses should override this method to return a Map.Entry
052: * of the type being tested.
053: */
054: public Map.Entry makeMapEntry() {
055: return new DefaultMapEntry(null, null);
056: }
057:
058: /**
059: * Make an instance of Map.Entry with the specified key and value.
060: * Subclasses should override this method to return a Map.Entry
061: * of the type being tested.
062: */
063: public Map.Entry makeMapEntry(Object key, Object value) {
064: return new DefaultMapEntry(key, value);
065: }
066:
067: //-----------------------------------------------------------------------
068: /**
069: * Subclasses should override this method.
070: *
071: */
072: public void testConstructors() {
073: // 1. test key-value constructor
074: Map.Entry entry = new DefaultMapEntry(key, value);
075: assertSame(key, entry.getKey());
076: assertSame(value, entry.getValue());
077:
078: // 2. test pair constructor
079: KeyValue pair = new DefaultKeyValue(key, value);
080: assertSame(key, pair.getKey());
081: assertSame(value, pair.getValue());
082:
083: // 3. test copy constructor
084: Map.Entry entry2 = new DefaultMapEntry(entry);
085: assertSame(key, entry2.getKey());
086: assertSame(value, entry2.getValue());
087:
088: // test that the objects are independent
089: entry.setValue(null);
090: assertSame(value, entry2.getValue());
091: }
092:
093: public void testSelfReferenceHandling() {
094: Map.Entry entry = makeMapEntry();
095:
096: try {
097: entry.setValue(entry);
098: assertSame(entry, entry.getValue());
099:
100: } catch (Exception e) {
101: fail("This Map.Entry implementation supports value self-reference.");
102: }
103: }
104:
105: }
|