commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From morg...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/comparators TestComparatorChain.java
Date Fri, 01 Mar 2002 23:31:35 GMT
morgand     02/03/01 15:31:35

  Modified:    collections/src/test/org/apache/commons/collections
                        TestAll.java
  Added:       collections/src/test/org/apache/commons/collections/comparators
                        TestComparatorChain.java
  Log:
  added tests for ComparatorChain class
  
  Revision  Changes    Path
  1.23      +5 -4      jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- TestAll.java	1 Mar 2002 19:06:44 -0000	1.22
  +++ TestAll.java	1 Mar 2002 23:31:35 -0000	1.23
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
1.22 2002/03/01 19:06:44 morgand Exp $
  - * $Revision: 1.22 $
  - * $Date: 2002/03/01 19:06:44 $
  + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v
1.23 2002/03/01 23:31:35 morgand Exp $
  + * $Revision: 1.23 $
  + * $Date: 2002/03/01 23:31:35 $
    *
    * ====================================================================
    *
  @@ -67,7 +67,7 @@
   /**
    * Entry point for all Collections tests.
    * @author Rodney Waldhoff
  - * @version $Id: TestAll.java,v 1.22 2002/03/01 19:06:44 morgand Exp $
  + * @version $Id: TestAll.java,v 1.23 2002/03/01 23:31:35 morgand Exp $
    */
   public class TestAll extends TestCase {
       public TestAll(String testName) {
  @@ -82,6 +82,7 @@
           suite.addTest(TestBeanMap.suite());
           suite.addTest(TestCollectionUtils.suite());
           suite.addTest(TestComparableComparator.suite());
  +        suite.addTest(TestComparatorChain.suite());
           suite.addTest(TestCursorableLinkedList.suite());
           suite.addTest(TestDoubleOrderedMap.suite());
           suite.addTest(TestExtendedProperties.suite());
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/comparators/TestComparatorChain.java
  
  Index: TestComparatorChain.java
  ===================================================================
  package org.apache.commons.collections.comparators;
  
  import java.io.Serializable;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.LinkedList;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  public class TestComparatorChain extends TestComparator {
  
      public TestComparatorChain(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestComparatorChain.class);
      }
  
      /**
       * 
       * @return 
       */
      public Comparator makeComparator() {
          ComparatorChain chain = new ComparatorChain(new ColumnComparator(0));
          chain.addComparator(new ColumnComparator(1),true); // reverse the second column
          chain.addComparator(new ColumnComparator(2),false);
          return chain;
      }
  
      public List getComparableObjectsOrdered() {
          List list = new LinkedList();
          // this is the correct order assuming a
          // "0th forward, 1st reverse, 2nd forward" sort
          list.add(new PseudoRow(1,2,3));
          list.add(new PseudoRow(2,3,5));
          list.add(new PseudoRow(2,2,4));
          list.add(new PseudoRow(2,2,8));
          list.add(new PseudoRow(3,1,0));
          list.add(new PseudoRow(4,4,4));
          list.add(new PseudoRow(4,4,7));
          return list;
      }
  
      public static class PseudoRow implements Serializable {
  
          public int cols[] = new int[3];
  
          public PseudoRow(int col1, int col2, int col3) {
              cols[0] = col1;
              cols[1] = col2;
              cols[2] = col3;
          }
  
          public int getColumn(int colIndex) {
              return cols[colIndex];
          }
  
          public String toString() {
              StringBuffer buf = new StringBuffer();
              buf.append("[");
              buf.append(cols[0]);
              buf.append(",");
              buf.append(cols[1]);
              buf.append(",");
              buf.append(cols[2]);
              buf.append("]");
              return buf.toString();
          }
  
          public boolean equals(Object o) {
              if (!(o instanceof PseudoRow)) {
                  return false;
              }
  
              PseudoRow row = (PseudoRow) o;
              if (getColumn(0) != row.getColumn(0)) {
                  return false;
              }
  
              if (getColumn(1) != row.getColumn(1)) {
                  return false;
              }            
              
              if (getColumn(2) != row.getColumn(2)) {
                  return false;
              }
  
              return true;
          }
  
      }
  
      public static class ColumnComparator implements Comparator,Serializable {
  
          protected int colIndex = 0;
  
          public ColumnComparator(int colIndex) {
              this.colIndex = colIndex;
          }
  
          public int compare(Object o1, Object o2) {
  
              int col1 = ( (PseudoRow) o1).getColumn(colIndex);
              int col2 = ( (PseudoRow) o2).getColumn(colIndex);
  
              if (col1 > col2) {
                  return 1;
              } else if (col1 < col2) {
                  return -1;
              }
  
              return 0;
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message