Index: lutinutil/src/test/org/codelutin/util/HashMapMultiKeyTest.java diff -u /dev/null lutinutil/src/test/org/codelutin/util/HashMapMultiKeyTest.java:1.1 --- /dev/null Wed Oct 5 20:24:17 2005 +++ lutinutil/src/test/org/codelutin/util/HashMapMultiKeyTest.java Wed Oct 5 20:24:12 2005 @@ -0,0 +1,167 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * HashMapMultiKeyTest.java + * + * Created: 27 septembre 2005 12:28:03 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/10/05 20:24:12 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.util; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import javax.swing.JLabel; +import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class HashMapMultiKeyTest extends TestCase { // HashMapMultiKeyTest + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(HashMapMultiKeyTest.class); + + public void testPutGet() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"), "valeur"); + Object valeur = map.get(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu")); + assertEquals("valeur", valeur); + } + + public void testRemove() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(); + + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"), "valeur1"); + map.put(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu"), "valeur2"); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tyty"), "valeur3"); + + Object valeur = map.remove("toto"); + assertEquals(ArrayList.class, valeur.getClass()); + assertEquals(2, ((ArrayList)valeur).size()); + assertEquals(1, map.size()); + + valeur = map.remove(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu")); + assertEquals("valeur2", valeur); + assertEquals(0, map.size()); + } + + public void testGetKeys() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(); + + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"), "valeur1"); + map.put(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu"), "valeur2"); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tyty"), "valeur3"); + + Set valeur = map.getKeys("titi"); + assertEquals(3, valeur.size()); + } + + public void testGetValues() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(); + + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"), "valeur1"); + map.put(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu"), "valeur2"); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tyty"), "valeur3"); + + ArrayList valeur = map.getValues("titi"); + assertEquals(3, valeur.size()); + } + + public void testWeak() throws Exception { + Object value = new ArrayList(); + WeakReference ref = new WeakReference(value); + System.out.println("ob:" + ref.get()); + System.gc(); + System.out.println("ob:" + ref.get()); + value = null; + System.gc(); + System.out.println("ob:" + ref.get() + " isEnqueued:" + ref.isEnqueued()); + } + + public void testWeakPutGet() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(null, HashMapMultiKey.WEAK); + Object valeur = new ArrayList(); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"), valeur); + valeur = null; + System.gc(); + valeur = map.get(new HashMapMultiKey.Key().add("toto").add("titi").add("tutu")); + assertEquals(null, valeur); + assertEquals(0, map.size()); + } + + public void testWeakRemove() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(null, HashMapMultiKey.WEAK); + Object valeur = new ArrayList(); + + Object key = new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"); + map.put(key, valeur); + map.put(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu"), new JLabel("v2")); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tyty"), new JLabel("v3")); + + System.gc(); + + valeur = map.remove("toto"); + assertEquals(ArrayList.class, valeur.getClass()); + assertEquals(1, ((ArrayList)valeur).size()); + assertEquals(0, map.size()); + + valeur = map.remove(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu")); + assertEquals(null, valeur); + assertEquals(0, map.size()); + } + + public void testWeakGetKeys() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(null, HashMapMultiKey.WEAK); + + Object valeur = new ArrayList(); + Object key = new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"); + map.put(key, valeur); + map.put(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu"), new JLabel("v2")); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tyty"), new JLabel("v3")); + + System.gc(); + + Set keys = map.getKeys("titi"); + assertEquals(1, keys.size()); + } + + public void testWeakGetValues() throws Exception { + HashMapMultiKey map = new HashMapMultiKey(null, HashMapMultiKey.WEAK); + Object valeur = new ArrayList(); + Object key = new HashMapMultiKey.Key().add("toto").add("titi").add("tutu"); + map.put(key, valeur); + map.put(new HashMapMultiKey.Key().add("tata").add("titi").add("tutu"), new JLabel("v3")); + map.put(new HashMapMultiKey.Key().add("toto").add("titi").add("tyty"), new JLabel("v4")); + + System.gc(); + + ArrayList values = map.getValues("titi"); + assertEquals(1, values.size()); + } + +} // HashMapMultiKeyTest +