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: import org.apache.commons.collections.Unmodifiable;
025:
026: /**
027: * Test the UnmodifiableMapEntry class.
028: *
029: * @since Commons Collections 3.0
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Neil O'Toole
033: */
034: public class TestUnmodifiableMapEntry extends AbstractTestMapEntry {
035:
036: public TestUnmodifiableMapEntry(String testName) {
037: super (testName);
038:
039: }
040:
041: public static void main(String[] args) {
042: junit.textui.TestRunner.run(TestUnmodifiableMapEntry.class);
043: }
044:
045: public static Test suite() {
046: return new TestSuite(TestUnmodifiableMapEntry.class);
047: }
048:
049: //-----------------------------------------------------------------------
050: /**
051: * Make an instance of Map.Entry with the default (null) key and value.
052: * Subclasses should override this method to return a Map.Entry
053: * of the type being tested.
054: */
055: public Map.Entry makeMapEntry() {
056: return new UnmodifiableMapEntry(null, null);
057: }
058:
059: /**
060: * Make an instance of Map.Entry with the specified key and value.
061: * Subclasses should override this method to return a Map.Entry
062: * of the type being tested.
063: */
064: public Map.Entry makeMapEntry(Object key, Object value) {
065: return new UnmodifiableMapEntry(key, value);
066: }
067:
068: //-----------------------------------------------------------------------
069: /**
070: * Subclasses should override this method.
071: *
072: */
073: public void testConstructors() {
074: // 1. test key-value constructor
075: Map.Entry entry = new UnmodifiableMapEntry(key, value);
076: assertSame(key, entry.getKey());
077: assertSame(value, entry.getValue());
078:
079: // 2. test pair constructor
080: KeyValue pair = new DefaultKeyValue(key, value);
081: entry = new UnmodifiableMapEntry(pair);
082: assertSame(key, entry.getKey());
083: assertSame(value, entry.getValue());
084:
085: // 3. test copy constructor
086: Map.Entry entry2 = new UnmodifiableMapEntry(entry);
087: assertSame(key, entry2.getKey());
088: assertSame(value, entry2.getValue());
089:
090: assertTrue(entry instanceof Unmodifiable);
091: }
092:
093: public void testAccessorsAndMutators() {
094: Map.Entry entry = makeMapEntry(key, value);
095:
096: assertSame(key, entry.getKey());
097: assertSame(value, entry.getValue());
098:
099: // check that null doesn't do anything funny
100: entry = makeMapEntry(null, null);
101: assertSame(null, entry.getKey());
102: assertSame(null, entry.getValue());
103: }
104:
105: public void testSelfReferenceHandling() {
106: // block
107: }
108:
109: public void testUnmodifiable() {
110: Map.Entry entry = makeMapEntry();
111: try {
112: entry.setValue(null);
113: fail();
114:
115: } catch (UnsupportedOperationException ex) {
116: }
117: }
118:
119: }
|