Add tests to BidiMap and make part of main test suite

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131241 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-10-05 20:52:29 +00:00
parent b1328e6ce5
commit 0672fd080f
2 changed files with 348 additions and 304 deletions

View File

@ -1,296 +1,341 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestBidiMap.java,v 1.3 2003/09/29 23:24:18 matth Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestBidiMap.java,v 1.4 2003/10/05 20:52:29 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* *
* 3. The end-user documentation included with the redistribution, if * 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement: * any, must include the following acknowledgement:
* "This product includes software developed by the * "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)." * Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself, * Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear. * if and wherever such third-party acknowledgements normally appear.
* *
* 4. The names "The Jakarta Project", "Commons", and "Apache Software * 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived * Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written * from this software without prior written permission. For written
* permission, please contact apache@apache.org. * permission, please contact apache@apache.org.
* *
* 5. Products derived from this software may not be called "Apache" * 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written * nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation. * permission of the Apache Software Foundation.
* *
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* ==================================================================== * ====================================================================
* *
* This software consists of voluntary contributions made by many * This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more * individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see * information on the Apache Software Foundation, please see
* <http://www.apache.org/>. * <http://www.apache.org/>.
* *
*/ */
package org.apache.commons.collections; package org.apache.commons.collections;
import java.util.Map; import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
/** * JUnit tests.
* JUnit tests. *
* * @version $Revision: 1.4 $ $Date: 2003/10/05 20:52:29 $
* @author Matthew Hawthorne *
* @version $Id: TestBidiMap.java,v 1.3 2003/09/29 23:24:18 matth Exp $ * @author Matthew Hawthorne
* @see org.apache.commons.collections.BidiMap */
*/ public abstract class TestBidiMap extends AbstractTestMap {
public abstract class TestBidiMap extends TestCase {
// Test data.
// Test data. private static final Object KEY = "key1";
private static final Object KEY = "key1"; private static final Object VALUE = "value1";
private static final Object VALUE = "value1";
private static final Object[][] entriesKV =
private static final Object[][] entries = new Object[][] {
new Object[][] { new Object[] { KEY, VALUE },
new Object[] { KEY, VALUE }, new Object[] { "key2", "value2" },
new Object[] { "key2", "value2" }, new Object[] { "key3", "value3" }
new Object[] { "key3", "value3" } };
}; private static final Object[][] entriesVK =
new Object[][] {
public TestBidiMap(String testName) { new Object[] { VALUE, KEY },
super(testName); new Object[] { "value2", "key2" },
} new Object[] { "value3", "key3" }
};
/** private final Object[][] entries;
* Creates an empty <code>BidiMap</code> implementation.
* public TestBidiMap(String testName) {
* @return an empty <code>BidiMap</code> implementation. super(testName);
*/ entries = entriesKV;
protected abstract BidiMap createBidiMap(); }
// testGetKey public TestBidiMap() {
super("Inverse");
public void testGetKey() { entries = entriesVK;
testGetKey(createBidiMapWithData(), entries[0][0], entries[0][1]); }
}
//-----------------------------------------------------------------------
public void testGetKeyInverse() { /**
testGetKey( * Implement to create an empty <code>BidiMap</code>.
createBidiMapWithData().inverseBidiMap(), *
entries[0][1], * @return an empty <code>BidiMap</code> implementation.
entries[0][0]); */
} protected abstract BidiMap makeEmptyBidiMap();
private final void testGetKey(BidiMap map, Object key, Object value) { /**
assertEquals("Value not found for key.", value, map.get(key)); * Override to create a full <code>BidiMap</code> other than the default.
assertEquals("Key not found for value.", key, map.getKey(value)); *
} * @return a full <code>BidiMap</code> implementation.
*/
// testInverse protected BidiMap makeFullBidiMap() {
final BidiMap map = makeEmptyBidiMap();
public void testInverse() { for (int i = 0; i < entries.length; i++) {
final BidiMap map = createBidiMapWithData(); map.put(entries[i][0], entries[i][1]);
final BidiMap inverseMap = map.inverseBidiMap(); }
return map;
assertSame( }
"Inverse of inverse is not equal to original.",
map, /**
inverseMap.inverseBidiMap()); * Override to return the empty BidiMap.
*/
assertEquals( protected final Map makeEmptyMap() {
"Value not found for key.", return makeEmptyBidiMap();
entries[0][0], }
inverseMap.get(entries[0][1]));
/**
assertEquals( * Override to indicate to AbstractTestMap this is a BidiMap.
"Key not found for value.", */
entries[0][1], protected boolean useDuplicateValues() {
inverseMap.getKey(entries[0][0])); return false;
} }
/** /**
* Ensures that calling: * Override to prevent infinite recursion of tests.
* */
* <pre> protected String[] ignoredTests() {
* map.add(a, c) return new String[] {"TestHashBidiMap.bulkTestInverseMap.bulkTestInverseMap"};
* map.add(b, c) }
* </pre>
* // BidiPut
* Removes the entry (a, c) //-----------------------------------------------------------------------
*/ public void testBidiPut() {
public void testAddDuplicateValue() { BidiMap map = makeEmptyBidiMap();
final BidiMap map = createBidiMap(); BidiMap inverse = map.inverseBidiMap();
assertEquals(0, map.size());
final Object key1 = "key1"; assertEquals(map.size(), inverse.size());
final Object key2 = "key2";
final Object value = "value"; map.put("A", "B");
assertEquals(1, map.size());
map.put(key1, value); assertEquals(map.size(), inverse.size());
map.put(key2, value); assertEquals("B", map.get("A"));
assertEquals("A", inverse.get("B"));
assertTrue(
"Key/value pair was not removed on duplicate value.", map.put("A", "C");
!map.containsKey(key1)); assertEquals(1, map.size());
assertEquals(map.size(), inverse.size());
assertEquals("Key/value mismatch", key2, map.getKey(value)); assertEquals("C", map.get("A"));
} assertEquals("A", inverse.get("C"));
public void testModifyEntrySet() { map.put("B", "C");
modifyEntrySet(createBidiMapWithData()); assertEquals(1, map.size());
modifyEntrySet(createBidiMapWithData().inverseBidiMap()); assertEquals(map.size(), inverse.size());
} assertEquals("C", map.get("B"));
assertEquals("B", inverse.get("C"));
private final void modifyEntrySet(BidiMap map) {
// Gets first entry map.put("E", "F");
final Map.Entry entry = (Map.Entry)map.entrySet().iterator().next(); assertEquals(2, map.size());
assertEquals(map.size(), inverse.size());
// Gets key and value assertEquals("F", map.get("E"));
final Object key = entry.getKey(); assertEquals("E", inverse.get("F"));
final Object oldValue = entry.getValue(); }
// Sets new value // testGetKey
final Object newValue = "newValue"; //-----------------------------------------------------------------------
entry.setValue(newValue); public void testBidiGetKey() {
doTestGetKey(makeFullBidiMap(), entries[0][0], entries[0][1]);
assertEquals( }
"Modifying entrySet did not affect underlying Map.",
newValue, public void testBidiGetKeyInverse() {
map.get(key)); doTestGetKey(
makeFullBidiMap().inverseBidiMap(),
assertNull( entries[0][1],
"Modifying entrySet did not affect inverse Map.", entries[0][0]);
map.getKey(oldValue)); }
}
private final void doTestGetKey(BidiMap map, Object key, Object value) {
// ---------------------------------------------------------------- assertEquals("Value not found for key.", value, map.get(key));
// Removal tests assertEquals("Key not found for value.", key, map.getKey(value));
// ---------------------------------------------------------------- }
public void testClear() { // testInverse
BidiMap map = createBidiMapWithData(); //-----------------------------------------------------------------------
map.clear(); public void testBidiInverse() {
assertTrue("Map was not cleared.", map.isEmpty()); final BidiMap map = makeFullBidiMap();
assertTrue( final BidiMap inverseMap = map.inverseBidiMap();
"Inverse map was not cleared.",
map.inverseBidiMap().isEmpty()); assertSame(
"Inverse of inverse is not equal to original.",
// Tests clear on inverse map,
map = createBidiMapWithData().inverseBidiMap(); inverseMap.inverseBidiMap());
map.clear();
assertTrue("Map was not cleared.", map.isEmpty()); assertEquals(
assertTrue( "Value not found for key.",
"Inverse map was not cleared.", entries[0][0],
map.inverseBidiMap().isEmpty()); inverseMap.get(entries[0][1]));
} assertEquals(
"Key not found for value.",
public void testRemove() { entries[0][1],
remove(createBidiMapWithData(), KEY); inverseMap.getKey(entries[0][0]));
remove(createBidiMapWithData().inverseBidiMap(), VALUE); }
removeKey(createBidiMapWithData(), VALUE); //-----------------------------------------------------------------------
removeKey(createBidiMapWithData().inverseBidiMap(), KEY); public void testBidiModifyEntrySet() {
} modifyEntrySet(makeFullBidiMap());
modifyEntrySet(makeFullBidiMap().inverseBidiMap());
private final void remove(BidiMap map, Object key) { }
final Object value = map.remove(key);
assertTrue("Key was not removed.", !map.containsKey(key)); private final void modifyEntrySet(BidiMap map) {
assertNull("Value was not removed.", map.getKey(value)); // Gets first entry
} final Map.Entry entry = (Map.Entry)map.entrySet().iterator().next();
private final void removeKey(BidiMap map, Object value) { // Gets key and value
final Object key = map.removeKey(value); final Object key = entry.getKey();
assertTrue("Key was not removed.", !map.containsKey(key)); final Object oldValue = entry.getValue();
assertNull("Value was not removed.", map.getKey(value));
} // Sets new value
final Object newValue = "newValue";
public void testRemoveByKeySet() { entry.setValue(newValue);
removeByKeySet(createBidiMapWithData(), KEY, VALUE);
removeByKeySet(createBidiMapWithData().inverseBidiMap(), VALUE, KEY); assertEquals(
} "Modifying entrySet did not affect underlying Map.",
newValue,
private final void removeByKeySet(BidiMap map, Object key, Object value) { map.get(key));
map.keySet().remove(key);
assertNull(
assertTrue("Key was not removed.", !map.containsKey(key)); "Modifying entrySet did not affect inverse Map.",
assertTrue("Value was not removed.", !map.containsValue(value)); map.getKey(oldValue));
}
assertTrue(
"Key was not removed from inverse map.", //-----------------------------------------------------------------------
!map.inverseBidiMap().containsValue(key)); public void testBidiClear() {
assertTrue( BidiMap map = makeFullBidiMap();
"Value was not removed from inverse map.", map.clear();
!map.inverseBidiMap().containsKey(value)); assertTrue("Map was not cleared.", map.isEmpty());
} assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty());
public void testRemoveByEntrySet() { // Tests clear on inverse
removeByEntrySet(createBidiMapWithData(), KEY, VALUE); map = makeFullBidiMap().inverseBidiMap();
removeByEntrySet(createBidiMapWithData().inverseBidiMap(), VALUE, KEY); map.clear();
} assertTrue("Map was not cleared.", map.isEmpty());
assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty());
private final void removeByEntrySet(
BidiMap map, }
Object key,
Object value) { //-----------------------------------------------------------------------
map.entrySet().remove(new DefaultMapEntry(key, value)); public void testBidiRemove() {
remove(makeFullBidiMap(), KEY);
assertTrue("Key was not removed.", !map.containsKey(key)); remove(makeFullBidiMap().inverseBidiMap(), VALUE);
assertTrue("Value was not removed.", !map.containsValue(value));
removeKey(makeFullBidiMap(), VALUE);
assertTrue( removeKey(makeFullBidiMap().inverseBidiMap(), KEY);
"Key was not removed from inverse map.", }
!map.inverseBidiMap().containsValue(key));
assertTrue( private final void remove(BidiMap map, Object key) {
"Value was not removed from inverse map.", final Object value = map.remove(key);
!map.inverseBidiMap().containsKey(value)); assertTrue("Key was not removed.", !map.containsKey(key));
} assertNull("Value was not removed.", map.getKey(value));
}
// ----------------------------------------------------------------
// Data generation methods private final void removeKey(BidiMap map, Object value) {
// ---------------------------------------------------------------- final Object key = map.removeKey(value);
assertTrue("Key was not removed.", !map.containsKey(key));
/** assertNull("Value was not removed.", map.getKey(value));
* This classes used to extend collections.TestMap, but can't anymore since }
* put() breaks a contract.
*/ //-----------------------------------------------------------------------
protected Map makeEmptyMap() { public void testBidiRemoveByKeySet() {
return createBidiMap(); removeByKeySet(makeFullBidiMap(), KEY, VALUE);
} removeByKeySet(makeFullBidiMap().inverseBidiMap(), VALUE, KEY);
}
protected final BidiMap createBidiMapWithData() {
final BidiMap map = createBidiMap(); private final void removeByKeySet(BidiMap map, Object key, Object value) {
fillMap(map); map.keySet().remove(key);
return map;
} assertTrue("Key was not removed.", !map.containsKey(key));
assertTrue("Value was not removed.", !map.containsValue(value));
private static final void fillMap(BidiMap map) {
for (int i = 0; i < entries.length; i++) { assertTrue(
map.put(entries[i][0], entries[i][1]); "Key was not removed from inverse map.",
} !map.inverseBidiMap().containsValue(key));
} assertTrue(
"Value was not removed from inverse map.",
} // TestBidiMap !map.inverseBidiMap().containsKey(value));
}
//-----------------------------------------------------------------------
public void testBidiRemoveByEntrySet() {
removeByEntrySet(makeFullBidiMap(), KEY, VALUE);
removeByEntrySet(makeFullBidiMap().inverseBidiMap(), VALUE, KEY);
}
private final void removeByEntrySet(BidiMap map, Object key, Object value) {
Map temp = new HashMap();
temp.put(key, value);
map.entrySet().remove(temp.entrySet().iterator().next());
assertTrue("Key was not removed.", !map.containsKey(key));
assertTrue("Value was not removed.", !map.containsValue(value));
assertTrue(
"Key was not removed from inverse map.",
!map.inverseBidiMap().containsValue(key));
assertTrue(
"Value was not removed from inverse map.",
!map.inverseBidiMap().containsKey(value));
}
public BulkTest bulkTestInverseMap() {
return new TestInverseBidiMap(this);
}
class TestInverseBidiMap extends TestBidiMap {
final TestBidiMap main;
public TestInverseBidiMap(TestBidiMap main) {
super();
this.main = main;
}
protected BidiMap makeEmptyBidiMap() {
return main.makeEmptyBidiMap().inverseBidiMap();
}
protected BidiMap makeFullBidiMap() {
return main.makeFullBidiMap().inverseBidiMap();
}
}
}

View File

@ -1,10 +1,10 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestHashBidiMap.java,v 1.1 2003/09/23 20:29:34 matth Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestHashBidiMap.java,v 1.2 2003/10/05 20:52:29 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -58,15 +58,14 @@
package org.apache.commons.collections; package org.apache.commons.collections;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner; import junit.textui.TestRunner;
/** /**
* JUnit tests. * JUnit tests.
* *
* @version $Revision: 1.2 $ $Date: 2003/10/05 20:52:29 $
*
* @author Matthew Hawthorne * @author Matthew Hawthorne
* @version $Id: TestHashBidiMap.java,v 1.1 2003/09/23 20:29:34 matth Exp $
* @see org.apache.commons.collections.HashBidiMap
*/ */
public class TestHashBidiMap extends TestBidiMap { public class TestHashBidiMap extends TestBidiMap {
@ -75,15 +74,15 @@ public class TestHashBidiMap extends TestBidiMap {
} }
public static Test suite() { public static Test suite() {
return new TestSuite(TestHashBidiMap.class); return BulkTest.makeSuite(TestHashBidiMap.class);
} }
public TestHashBidiMap(String testName) { public TestHashBidiMap(String testName) {
super(testName); super(testName);
} }
protected BidiMap createBidiMap() { protected BidiMap makeEmptyBidiMap() {
return new HashBidiMap(); return new HashBidiMap();
} }
} // TestHashBidiMap }