import java.util.*;
import org.apache.commons.collections.CollectionUtils;

public class TestCollectionUtils {
    public TestCollectionUtils(String testName) {
        System.out.println("And now we test feature " + testName + ".");
        setUp();
    }

    public static void main(String args[]) {
      TestCollectionUtils aTest = new TestCollectionUtils("testIntersection");
      aTest.testIntersection();
      System.out.println("OK - End of test");      
    }

    private Collection _a = null;
    private Collection _b = null;
    private Collection _c = null;
    private Collection _d = null;

    public void setUp() {
        _a = new ArrayList();
        _a.add("a");
        _a.add("b");
        _a.add("b");
        _a.add("c");
        _a.add("c");
        _a.add("c");
        _a.add("d");
        _a.add("d");
        _a.add("d");
        _a.add("d");
        _b = new LinkedList();
        _b.add("e");
        _b.add("d");
        _b.add("d");
        _b.add("c");
        _b.add("c");
        _b.add("c");
        _b.add("b");
        _b.add("b");
        _b.add("b");
        _b.add("b");
        _c = new ArrayList();
        _c.add(("a").toUpperCase());
        _c.add("X");
        _d = new LinkedList();
        _d.add(("a").toUpperCase());
        _d.add("Y");
    }
    
    public void assertNull(Object o) {
      if (o != null) throw new RuntimeException("assertNull: " + o.toString() + " should be null, but it isn't.");
    }
    public void assertEquals(Object o1, Object o2) {
      if (o1 == null && o2 == null) return;
      if (o1 == null) throw new RuntimeException("assertEquals: The first object is null, and " + o2.toString() + " isn't.");
      if (!(o1.equals(o2))) throw new RuntimeException("assertEquals: " + o1.toString() + " isn't equal to " + o2);
    }    
    public void assertIdentity(Object o1, Object o2) {
       if (o1 != o2) throw new RuntimeException("assertIdentity: " + o1 + " is not identical with " + o2);
    }     
    public void assertDifference(Object o1, Object o2) {
       if (o1 == o2) throw new RuntimeException("assertDifference: " + o1 + " is identical with " + o2);
    }      

    public void testIntersection() {
        Collection col = CollectionUtils.intersection(_a,_b);
        Map freq = CollectionUtils.getCardinalityMap(col);
        assertNull(freq.get("a"));
        assertEquals(new Integer(2),freq.get("b"));
        assertEquals(new Integer(3),freq.get("c"));
        assertEquals(new Integer(2),freq.get("d"));
        assertNull(freq.get("e"));
        Collection col2 = CollectionUtils.intersection(_b,_a);
        Map freq2 = CollectionUtils.getCardinalityMap(col2);
        assertNull(freq2.get("a"));
        assertEquals(new Integer(2),freq2.get("b"));
        assertEquals(new Integer(3),freq2.get("c"));
        assertEquals(new Integer(2),freq2.get("d"));
        assertNull(freq2.get("e"));
        
        assertEquals("A", "A");
        assertEquals("A" + "B", "A" + "B");  
        assertIdentity("A" + "B", "A" + "B");           
        assertEquals(("A" + "B").toLowerCase(), ("A" + "B").toLowerCase());
        assertDifference(("a").toUpperCase(), ("a").toUpperCase());   
        
        System.out.println("We create the string A via method call to get different objects.");
        
        Collection col3 = CollectionUtils.intersection(_c,_d);  
        
        Map freq_c = CollectionUtils.getCardinalityMap(_c);        
        Map freq_d = CollectionUtils.getCardinalityMap(_d);  
        System.out.println("frequency of A in _c: " + freq_c.get("A"));
        System.out.println("frequency of A in _d: " + freq_d.get("A"));        
        Map freq3 = CollectionUtils.getCardinalityMap(col3);       
        System.out.println("frequency of A in intersection(_c, _d): " + freq3.get("A"));         
        assertEquals(new Integer(1),freq3.get("A"));        
        assertEquals(new Integer(1), new Integer(col3.size()));
        System.out.println("The intersection between _c and _d is " + col3.iterator().next());
    }
}
