From 63a4d98529c64cff9ceb01880b26a0739e32c220 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Wed, 1 Oct 2003 22:36:49 +0000 Subject: [PATCH] Add tests for pairs package git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131213 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/TestAllPackages.java | 5 +- .../pairs/AbstractTestMapEntry.java | 202 +++++++++++++++ .../commons/collections/pairs/TestAll.java | 91 +++++++ .../pairs/TestDefaultKeyValue.java | 238 ++++++++++++++++++ .../pairs/TestDefaultMapEntry.java | 145 +++++++++++ 5 files changed, 679 insertions(+), 2 deletions(-) create mode 100644 src/test/org/apache/commons/collections/pairs/AbstractTestMapEntry.java create mode 100644 src/test/org/apache/commons/collections/pairs/TestAll.java create mode 100644 src/test/org/apache/commons/collections/pairs/TestDefaultKeyValue.java create mode 100644 src/test/org/apache/commons/collections/pairs/TestDefaultMapEntry.java diff --git a/src/test/org/apache/commons/collections/TestAllPackages.java b/src/test/org/apache/commons/collections/TestAllPackages.java index 721852fc0..e9b720055 100644 --- a/src/test/org/apache/commons/collections/TestAllPackages.java +++ b/src/test/org/apache/commons/collections/TestAllPackages.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAllPackages.java,v 1.3 2003/09/07 16:49:41 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAllPackages.java,v 1.4 2003/10/01 22:36:49 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -64,7 +64,7 @@ import junit.framework.TestSuite; /** * Entry point for all Collections project tests. * - * @version $Revision: 1.3 $ $Date: 2003/09/07 16:49:41 $ + * @version $Revision: 1.4 $ $Date: 2003/10/01 22:36:49 $ * * @author Stephen Colebourne */ @@ -80,6 +80,7 @@ public class TestAllPackages extends TestCase { suite.addTest(org.apache.commons.collections.decorators.TestAll.suite()); suite.addTest(org.apache.commons.collections.iterators.TestAll.suite()); suite.addTest(org.apache.commons.collections.observed.TestAll.suite()); + suite.addTest(org.apache.commons.collections.pairs.TestAll.suite()); suite.addTest(org.apache.commons.collections.primitives.TestAll.suite()); return suite; } diff --git a/src/test/org/apache/commons/collections/pairs/AbstractTestMapEntry.java b/src/test/org/apache/commons/collections/pairs/AbstractTestMapEntry.java new file mode 100644 index 000000000..c5e06adc7 --- /dev/null +++ b/src/test/org/apache/commons/collections/pairs/AbstractTestMapEntry.java @@ -0,0 +1,202 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/AbstractTestMapEntry.java,v 1.1 2003/10/01 22:36:49 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.pairs; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +/** + * Abstract tests that can be extended to test any Map.Entry implementation. + * Subclasses must implement {@link #makeMapEntry(Object, Object)} to return + * a new Map.Entry of the type being tested. Subclasses must also implement + * {@link #testConstructors()} to test the constructors of the Map.Entry + * type being tested. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/10/01 22:36:49 $ + * + * @author Neil O'Toole + */ +public abstract class AbstractTestMapEntry extends TestCase { + + protected final String key = "name"; + protected final String value = "duke"; + + /** + * JUnit constructor. + * + * @param testName the test name + */ + public AbstractTestMapEntry(String testName) { + super(testName); + } + + //----------------------------------------------------------------------- + /** + * Make an instance of Map.Entry with the default (null) key and value. + * This implementation simply calls {@link #makeMapEntry(Object, Object)} + * with null for key and value. Subclasses can override this method if desired. + */ + protected Map.Entry makeMapEntry() { + return makeMapEntry(null, null); + } + + /** + * Make an instance of Map.Entry with the specified key and value. + * Subclasses should override this method to return a Map.Entry + * of the type being tested. + */ + protected abstract Map.Entry makeMapEntry(Object key, Object value); + + /** + * Makes a Map.Entry of a type that's known to work correctly. + */ + protected Map.Entry makeKnownMapEntry() { + return makeKnownMapEntry(null, null); + } + + /** + * Makes a Map.Entry of a type that's known to work correctly. + */ + protected Map.Entry makeKnownMapEntry(Object key, Object value) { + Map map = new HashMap(1); + map.put(key, value); + Map.Entry entry = (Map.Entry) map.entrySet().iterator().next(); + return entry; + } + + //----------------------------------------------------------------------- + public void testAccessorsAndMutators() { + Map.Entry entry = makeMapEntry(key, value); + + assertTrue(entry.getKey() == key); + + entry.setValue(value); + assertTrue(entry.getValue() == value); + + // check that null doesn't do anything funny + entry = makeMapEntry(null, null); + assertTrue(entry.getKey() == null); + + entry.setValue(null); + assertTrue(entry.getValue() == null); + } + + /** + * Subclasses should override this method to test the + * desired behaviour of the class with respect to + * handling of self-references. + * + */ + + public void testSelfReferenceHandling() { + // test that #setValue does not permit + // the MapEntry to contain itself (and thus cause infinite recursion + // in #hashCode and #toString) + + Map.Entry entry = makeMapEntry(); + + try { + entry.setValue(entry); + fail("Should throw an IllegalArgumentException"); + } catch (IllegalArgumentException iae) { + // expected to happen... + + // check that the KVP's state has not changed + assertTrue(entry.getKey() == null && entry.getValue() == null); + } + } + + /** + * Subclasses should provide tests for their constructors. + * + */ + public abstract void testConstructors(); + + public void testEqualsAndHashCode() { + // 1. test with object data + Map.Entry e1 = makeMapEntry(key, value); + Map.Entry e2 = makeKnownMapEntry(key, value); + + assertTrue(e1.equals(e1)); + assertTrue(e2.equals(e1)); + assertTrue(e1.equals(e2)); + assertTrue(e1.hashCode() == e2.hashCode()); + + // 2. test with nulls + e1 = makeMapEntry(); + e2 = makeKnownMapEntry(); + + assertTrue(e1.equals(e1)); + assertTrue(e2.equals(e1)); + assertTrue(e1.equals(e2)); + assertTrue(e1.hashCode() == e2.hashCode()); + } + + public void testToString() { + Map.Entry entry = makeMapEntry(key, value); + assertTrue(entry.toString().equals("[" + entry.getKey() + "=" + entry.getValue() + "]")); + + // test with nulls + entry = makeMapEntry(); + assertTrue(entry.toString().equals("[" + entry.getKey() + "=" + entry.getValue() + "]")); + } + +} diff --git a/src/test/org/apache/commons/collections/pairs/TestAll.java b/src/test/org/apache/commons/collections/pairs/TestAll.java new file mode 100644 index 000000000..91676303c --- /dev/null +++ b/src/test/org/apache/commons/collections/pairs/TestAll.java @@ -0,0 +1,91 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/TestAll.java,v 1.1 2003/10/01 22:36:49 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.pairs; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Entry point for key-value test cases. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/10/01 22:36:49 $ + * + * @author Neil O'Toole + */ +public class TestAll extends TestCase { + + public TestAll(String testName) { + super(testName); + } + + public static void main(String args[]) { + String[] testCaseName = { TestAll.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + public static Test suite() { + TestSuite suite = new TestSuite(); + + suite.addTest(TestDefaultKeyValue.suite()); + suite.addTest(TestDefaultMapEntry.suite()); + return suite; + } + +} diff --git a/src/test/org/apache/commons/collections/pairs/TestDefaultKeyValue.java b/src/test/org/apache/commons/collections/pairs/TestDefaultKeyValue.java new file mode 100644 index 000000000..8358fa10e --- /dev/null +++ b/src/test/org/apache/commons/collections/pairs/TestDefaultKeyValue.java @@ -0,0 +1,238 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/TestDefaultKeyValue.java,v 1.1 2003/10/01 22:36:49 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.pairs; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Test the DefaultKeyValue class. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/10/01 22:36:49 $ + * + * @author Neil O'Toole + */ +public class TestDefaultKeyValue extends TestCase { + + private final String key = "name"; + private final String value = "duke"; + + /** + * JUnit constructor. + * + * @param testName the test name + */ + public TestDefaultKeyValue(String testName) { + super(testName); + + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(TestDefaultKeyValue.class); + } + + public static Test suite() { + return new TestSuite(TestDefaultKeyValue.class); + } + + //----------------------------------------------------------------------- + /** + * Make an instance of DefaultKeyValue with the default (null) key and value. + * Subclasses should override this method to return a DefaultKeyValue + * of the type being tested. + */ + protected DefaultKeyValue makeDefaultKeyValue() { + return new DefaultKeyValue(null, null); + } + + /** + * Make an instance of DefaultKeyValue with the specified key and value. + * Subclasses should override this method to return a DefaultKeyValue + * of the type being tested. + */ + protected DefaultKeyValue makeDefaultKeyValue(Object key, Object value) { + return new DefaultKeyValue(key, value); + } + + //----------------------------------------------------------------------- + public void testAccessorsAndMutators() { + DefaultKeyValue kv = makeDefaultKeyValue(); + + kv.setKey(key); + assertTrue(kv.getKey() == key); + + kv.setValue(value); + assertTrue(kv.getValue() == value); + + // check that null doesn't do anything funny + kv.setKey(null); + assertTrue(kv.getKey() == null); + + kv.setValue(null); + assertTrue(kv.getValue() == null); + + } + + public void testSelfReferenceHandling() { + // test that #setKey and #setValue do not permit + // the KVP to contain itself (and thus cause infinite recursion + // in #hashCode and #toString) + + DefaultKeyValue kv = makeDefaultKeyValue(); + + try { + kv.setKey(kv); + fail("Should throw an IllegalArgumentException"); + } catch (IllegalArgumentException iae) { + // expected to happen... + + // check that the KVP's state has not changed + assertTrue(kv.getKey() == null && kv.getValue() == null); + } + + try { + kv.setValue(kv); + fail("Should throw an IllegalArgumentException"); + } catch (IllegalArgumentException iae) { + // expected to happen... + + // check that the KVP's state has not changed + assertTrue(kv.getKey() == null && kv.getValue() == null); + } + } + + /** + * Subclasses should override this method to test their own constructors. + */ + public void testConstructors() { + // 1. test default constructor + DefaultKeyValue kv = new DefaultKeyValue(); + assertTrue(kv.getKey() == null && kv.getValue() == null); + + // 2. test key-value constructor + kv = new DefaultKeyValue(key, value); + assertTrue(kv.getKey() == key && kv.getValue() == value); + + // 3. test copy constructor + DefaultKeyValue kv2 = new DefaultKeyValue(kv); + assertTrue(kv2.getKey() == key && kv2.getValue() == value); + + // test that the KVPs are independent + kv.setKey(null); + kv.setValue(null); + + assertTrue(kv2.getKey() == key && kv2.getValue() == value); + + // 4. test Map.Entry constructor + Map map = new HashMap(); + map.put(key, value); + Map.Entry entry = (Map.Entry) map.entrySet().iterator().next(); + + kv = new DefaultKeyValue(entry); + assertTrue(kv.getKey() == key && kv.getValue() == value); + + // test that the KVP is independent of the Map.Entry + entry.setValue(null); + assertTrue(kv.getValue() == value); + + } + + public void testEqualsAndHashCode() { + // 1. test with object data + DefaultKeyValue kv = makeDefaultKeyValue(key, value); + DefaultKeyValue kv2 = makeDefaultKeyValue(key, value); + + assertTrue(kv.equals(kv)); + assertTrue(kv.equals(kv2)); + assertTrue(kv.hashCode() == kv2.hashCode()); + + // 2. test with nulls + kv = makeDefaultKeyValue(null, null); + kv2 = makeDefaultKeyValue(null, null); + + assertTrue(kv.equals(kv)); + assertTrue(kv.equals(kv2)); + assertTrue(kv.hashCode() == kv2.hashCode()); + } + + public void testToString() { + DefaultKeyValue kv = makeDefaultKeyValue(key, value); + assertTrue(kv.toString().equals("[" + kv.getKey() + "=" + kv.getValue() + "]")); + + // test with nulls + kv = makeDefaultKeyValue(null, null); + assertTrue(kv.toString().equals("[" + kv.getKey() + "=" + kv.getValue() + "]")); + } + + public void testToMapEntry() { + DefaultKeyValue kv = makeDefaultKeyValue(key, value); + + Map map = new HashMap(); + map.put(kv.getKey(), kv.getValue()); + Map.Entry entry = (Map.Entry) map.entrySet().iterator().next(); + + assertTrue(entry.equals(kv.toMapEntry())); + assertTrue(entry.hashCode() == kv.hashCode()); + } + +} diff --git a/src/test/org/apache/commons/collections/pairs/TestDefaultMapEntry.java b/src/test/org/apache/commons/collections/pairs/TestDefaultMapEntry.java new file mode 100644 index 000000000..66c8007e8 --- /dev/null +++ b/src/test/org/apache/commons/collections/pairs/TestDefaultMapEntry.java @@ -0,0 +1,145 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/pairs/Attic/TestDefaultMapEntry.java,v 1.1 2003/10/01 22:36:49 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.pairs; + +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * Test the DefaultMapEntry class. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/10/01 22:36:49 $ + * + * @author Neil O'Toole + */ +public class TestDefaultMapEntry extends AbstractTestMapEntry { + + public TestDefaultMapEntry(String testName) { + super(testName); + + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(TestDefaultMapEntry.class); + } + + public static Test suite() { + return new TestSuite(TestDefaultMapEntry.class); + } + + //----------------------------------------------------------------------- + /** + * Make an instance of Map.Entry with the default (null) key and value. + * Subclasses should override this method to return a Map.Entry + * of the type being tested. + */ + public Map.Entry makeMapEntry() { + return new DefaultMapEntry(null, null); + } + + /** + * Make an instance of Map.Entry with the specified key and value. + * Subclasses should override this method to return a Map.Entry + * of the type being tested. + */ + public Map.Entry makeMapEntry(Object key, Object value) { + return new DefaultMapEntry(key, value); + } + + //----------------------------------------------------------------------- + /** + * Subclasses should override this method. + * + */ + public void testConstructors() { + // 1. test key-value constructor + Map.Entry entry = new DefaultMapEntry(key, value); + assertSame(key, entry.getKey()); + assertSame(value, entry.getValue()); + + // 2. test pair constructor + KeyValue pair = new DefaultKeyValue(key, value); + assertSame(key, pair.getKey()); + assertSame(value, pair.getValue()); + + // 3. test copy constructor + Map.Entry entry2 = new DefaultMapEntry(entry); + assertSame(key, entry2.getKey()); + assertSame(value, entry2.getValue()); + + // test that the objects are independent + entry.setValue(null); + assertSame(value, entry2.getValue()); + } + + public void testSelfReferenceHandling() { + Map.Entry entry = makeMapEntry(); + + try { + entry.setValue(entry); + assertSame(entry, entry.getValue()); + + } catch (Exception e) { + fail("This Map.Entry implementation supports value self-reference."); + } + } + +}