/*
 * $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 TestPredicateSet 
		extends TestCase {
		    
	Collection iColl = null;
	Set iSet = null;
	Collection iBadColl = null;
	Predicate iPredicate = null;
    
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}
	public static TestSuite suite() {
		return new TestSuite(TestPredicateSet.class);
	}
	public TestPredicateSet(String name) {
		super(name);
	}
	public void setUp() {
	    // the contents of these two must be the same
	    iColl = new ArrayList();
	    iColl.add(new Integer(2));
	    iColl.add(new Long(5));
	    iColl.add(new Integer(7));
	    iSet = new HashSet();
	    iSet.add(new Integer(2));
	    iSet.add(new Long(5));
	    iSet.add(new Integer(7));
	    // bad collection
	    iBadColl = new ArrayList();
	    iBadColl.add(new Integer(2));
	    iBadColl.add(new Object());
	    iBadColl.add(new Integer(7));
	    
	    iPredicate = PredicateUtils.instanceofPredicate(Number.class);
	}
	public void tearDown() {
	}

	public void testConstructorNormal() {
	    Set set = PredicateUtils.predicateSet(iSet, iPredicate);
	    assertNotNull(set);
	    assertEquals(3, set.size());
	    assertTrue(set.containsAll(iColl));
	}	
	public void testConstructorNullNormal() {
	    try {
		    Set set = PredicateUtils.predicateSet(null, iPredicate);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	public void testConstructorNormalNull() {
	    try {
		    Set set = PredicateUtils.predicateSet(iSet, null);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	public void testConstructorNullNull() {
	    try {
		    Set set = PredicateUtils.predicateSet(null, null);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
	
	public void testAdd1Normal() {
	    Set set = PredicateUtils.predicateSet(new HashSet(), iPredicate);
	    set.add(new Integer(6));
	    assertTrue(set.contains(new Integer(6)));
	    assertEquals(1, set.size());
	}	
	public void testAdd1Validation() {
	    Set set = PredicateUtils.predicateSet(new HashSet(), iPredicate);
	    try {
		    set.add(new Object());
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	

	public void testAddColl1Normal() {
	    Set set = PredicateUtils.predicateSet(new HashSet(), iPredicate);
	    set.addAll(iColl);
	    assertTrue(set.containsAll(iSet));
	    assertEquals(3, set.size());
	}	
	public void testAddColl1Validation() {
	    Set set = PredicateUtils.predicateSet(new HashSet(), iPredicate);
	    try {
		    set.addAll(iBadColl);
	    } catch (IllegalArgumentException ex) {
	        return;
	    }
	    fail();
	}	
}
