/*
 * $Header$
 * $Revision$
 * $Date$
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2002 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 acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements 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 Group.
 *
 * 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
 * <http://www.apache.org/>.
 *
 */
package org.apache.commons.collections;

import java.io.*;
import java.util.*;

import junit.framework.*;
/**
 * TestPredicateSet tests the PredicateSet class.
 * 
 * @author Stephen Colebourne
 */
public class TestPredicateMap 
		extends TestCase {
		    
	Map iMap1 = null;
	Map iMap2 = null;
	Map iBadMap = null;
	Predicate iValuePredicate = null;
	Predicate iKeyPredicate = null;
    
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}
	public static TestSuite suite() {
		return new TestSuite(TestPredicateMap.class);
	}
	public TestPredicateMap(String name) {
		super(name);
	}
	public void setUp() {
	    // the contents of these two must be the same
	    iMap1 = new HashMap();
	    iMap1.put("Two", new Integer(2));
	    iMap1.put("Five", new Long(5));
	    iMap1.put("Seven", new Integer(7));
	    iMap2 = new HashMap();
	    iMap2.put("Two", new Integer(2));
	    iMap2.put("Five", new Long(5));
	    iMap2.put("Seven", new Integer(7));
	    // bad collection
	    iBadMap = new HashMap();
	    iBadMap.put("Two", new Integer(2));
	    iBadMap.put("Object", new Object());
	    iBadMap.put("Seven", new Integer(7));
	    
	    iKeyPredicate = PredicateUtils.instanceofPredicate(String.class);
	    iValuePredicate = PredicateUtils.instanceofPredicate(Number.class);
	}
	public void tearDown() {
	}

	public void testConstructorNormal() {
	    Map map = PredicateUtils.predicateMap(iMap1, iKeyPredicate, iValuePredicate);
	    assertNotNull(map);
	    assertEquals(3, map.size());
	    assertTrue(map.equals(iMap2));
	}	
	public void testConstructorNullNormalNormal() {
	    try {
		    Map map = PredicateUtils.predicateMap(null, iKeyPredicate, iValuePredicate);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	public void testConstructorNormalNullNormal() {
	    try {
		    Map map = PredicateUtils.predicateMap(iMap1, null, iValuePredicate);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	public void testConstructorNormalNormalNull() {
	    try {
		    Map map = PredicateUtils.predicateMap(iMap1, iKeyPredicate, null);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	
	public void testPutNormal() {
	    Map map = PredicateUtils.predicateMap(new HashMap(), iKeyPredicate, iValuePredicate);
	    map.put("Six", new Integer(6));
	    assertTrue(map.containsKey("Six"));
	    assertTrue(map.containsValue(new Integer(6)));
	    assertEquals(1, map.size());
	}	
	public void testPutValueValidation() {
	    Map map = PredicateUtils.predicateMap(new HashMap(), iKeyPredicate, iValuePredicate);
	    try {
		    map.put("Object", new Object());
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	

	public void testAddCollNormal() {
	    Map map = PredicateUtils.predicateMap(new HashMap(), iKeyPredicate, iValuePredicate);
	    map.putAll(iMap2);
	    assertTrue(map.equals(iMap2));
	    assertEquals(3, map.size());
	}	
	public void testAddCollValidation() {
	    Map map = PredicateUtils.predicateMap(new HashMap(), iKeyPredicate, iValuePredicate);
	    try {
		    map.putAll(iBadMap);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	public void testEntrySetValidation() {
	    Map map = PredicateUtils.predicateMap(iMap1, iKeyPredicate, iValuePredicate);
	    Map.Entry entry = (Map.Entry) map.entrySet().iterator().next();
	    try {
	        entry.setValue(new Object());
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
}
