You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2003/09/09 05:03:57 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/decorators TestPredicatedBag.java TestPredicatedSortedBag.java TestAll.java

psteitz     2003/09/08 20:03:57

  Modified:    collections/src/test/org/apache/commons/collections/decorators
                        TestAll.java
  Added:       collections/src/test/org/apache/commons/collections/decorators
                        TestPredicatedBag.java TestPredicatedSortedBag.java
  Log:
  Added tests for PredicatedBag, PredicatedSortedBag
  
  Revision  Changes    Path
  1.8       +4 -2      jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestAll.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestAll.java	3 Sep 2003 23:54:25 -0000	1.7
  +++ TestAll.java	9 Sep 2003 03:03:57 -0000	1.8
  @@ -98,6 +98,8 @@
           suite.addTest(TestTransformedSortedBag.suite());
           suite.addTest(TestTransformedSortedMap.suite());
           suite.addTest(TestTransformedSortedSet.suite());
  +        suite.addTest(TestPredicatedBag.suite());
  +        suite.addTest(TestPredicatedSortedBag.suite());
           
           return suite;
       }
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestPredicatedBag.java
  
  Index: TestPredicatedBag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestPredicatedBag.java,v 1.1 2003/09/09 03:03:57 psteitz Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements 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 Software Foundation.
   *
   * 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.decorators;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import java.util.Set;
  
  import org.apache.commons.collections.Bag;
  import org.apache.commons.collections.HashBag;
  import org.apache.commons.collections.TestBag;
  import org.apache.commons.collections.Predicate;
  
  /**
   * Extension of {@link TestBag} for exercising the {@link PredicatedBag}
   * implementation.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/09/09 03:03:57 $
   * 
   * @author Phil Steitz
   */
  public class TestPredicatedBag extends TestBag {
      
      public TestPredicatedBag(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestPredicatedBag.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestPredicatedBag.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
  
      protected Predicate getPredicate() {
          return new Predicate() {
              public boolean evaluate(Object o) {
                  return o instanceof String;
              }
          };
      }
      
      protected Bag decorateBag(HashBag bag, Predicate predicate) {
          return PredicatedBag.decorate(bag, predicate);
      }
  
      public Bag makeBag() {
          return decorateBag(new HashBag(), getPredicate());
      }
  
      public void testlegalAddRemove() {
          Bag bag = makeBag();
          assertEquals(0, bag.size());
          Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
          for (int i = 0; i < els.length; i++) {
              bag.add(els[i]);
              assertEquals(i + 1, bag.size());
              assertEquals(true, bag.contains(els[i]));
          }
          Set set = ((PredicatedBag) bag).uniqueSet();
          assertTrue("Unique set contains the first element",set.contains(els[0]));
          assertEquals(true, bag.remove(els[0])); 
          set = ((PredicatedBag) bag).uniqueSet();
          assertTrue("Unique set now does not contain the first element",
              !set.contains(els[0])); 
      }
   
      public void testIllegalAdd() {
          Bag bag = makeBag();
          Integer i = new Integer(3);
          try {
              bag.add(i);
              fail("Integer should fail string predicate.");
          } catch (IllegalArgumentException e) {
              // expected
          }
          assertTrue("Collection shouldn't contain illegal element", 
           !bag.contains(i));   
      }
  
      public void testIllegalDecorate() {
          HashBag elements = new HashBag();
          elements.add("one");
          elements.add("two");
          elements.add(new Integer(3));
          elements.add("four");
          try {
              Bag bag = decorateBag(elements, getPredicate());
              fail("Bag contains an element that should fail the predicate.");
          } catch (IllegalArgumentException e) {
              // expected
          }
          try {
              Bag bag = decorateBag(new HashBag(), null);
              fail("Expectiing IllegalArgumentException for null predicate.");
          } catch (IllegalArgumentException e) {
              // expected
          }              
      }
  }
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestPredicatedSortedBag.java
  
  Index: TestPredicatedSortedBag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestPredicatedSortedBag.java,v 1.1 2003/09/09 03:03:57 psteitz Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 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 acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements 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 Software Foundation.
   *
   * 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.decorators;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import java.util.Comparator;
  
  import org.apache.commons.collections.Bag;
  import org.apache.commons.collections.SortedBag;
  import org.apache.commons.collections.TreeBag;
  import org.apache.commons.collections.Predicate;
  import org.apache.commons.collections.TestBag;
  
  /**
   * Extension of {@link TestBag} for exercising the {@link PredicatedSortedBag}
   * implementation.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/09/09 03:03:57 $
   * 
   * @author Phil Steitz
   */
  public class TestPredicatedSortedBag extends TestBag {
      
      private SortedBag emptyBag = new TreeBag();
      private SortedBag nullBag = null;
      
      public TestPredicatedSortedBag(String testName) {
          super(testName);
      }
      
      protected Predicate getPredicate() {
          return new Predicate() {
              public boolean evaluate(Object o) {
                  return o instanceof String;
              }
          };
      }
      
      protected SortedBag decorateBag(SortedBag bag, Predicate predicate) {
          return PredicatedSortedBag.decorate(bag, predicate);
      }
      
      public Bag makeBag() {
          return decorateBag(emptyBag, getPredicate());
      }
  
      public static Test suite() {
          return new TestSuite(TestPredicatedSortedBag.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestPredicatedSortedBag.class.getName()};
          junit.textui.TestRunner.main(testCaseName);
      }
      
      public void testDecorate() {
          SortedBag bag = decorateBag(emptyBag, getPredicate());
          SortedBag bag2 = ((PredicatedSortedBag) bag).getSortedBag();
          try {
              SortedBag bag3 = decorateBag(emptyBag, null);
              fail("Expecting IllegalArgumentException for null predicate");
          } catch (IllegalArgumentException e) {}
          try {
              SortedBag bag4 = decorateBag(nullBag, getPredicate());
              fail("Expecting IllegalArgumentException for null bag");
          } catch (IllegalArgumentException e) {}
      }
      
      public void testSortOrder() {
          PredicatedSortedBag bag = 
              (PredicatedSortedBag) decorateBag(emptyBag, getPredicate());
          String one = "one";
          String two = "two";
          String three = "three";
          bag.add(one);
          bag.add(two);
          bag.add(three);
          assertEquals("first element", bag.first(), one);
          assertEquals("last element", bag.last(), two); 
          Comparator c = bag.comparator();
          assertTrue("natural order, so comparator should be null", 
              c == null);
      }
  }