//////////////////////////////////////////////////////////////////////////////
// 
//               Copyright 2010, Starting Block Technologies
//                        www.startingblocktech.com
//                           All Rights Reserved
//
//////////////////////////////////////////////////////////////////////////////

package com.startingblocktech.tcases.util;

import static org.junit.Assert.*;

import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.IteratorUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Defines test assertion utilities.
 *
 * @version $Revision$, $Date$
 */
public abstract class Asserts
  {
  /**
   * Reports a failure if the actual Collection contains a different set of values than the expected Collection.
   */
  public static <T> void assertSetEquals( String label, Collection<T> expected, Collection<T> actual)
    {
    if( (expected == null) != (actual == null))
      {
      assertEquals( label, expected, actual);
      }

    else if( expected != null)
      {
      Collection<T> unexpected = CollectionUtils.subtract( actual, expected);
      Collection<T> missing = CollectionUtils.subtract( expected, actual);

      if( !(unexpected.isEmpty() && missing.isEmpty()))
        {
        StringBuilder msg = new StringBuilder();

        msg.append( label);
        if( !missing.isEmpty())
          {
          msg.append( ", missing=").append( '[').append( StringUtils.join( missing, ',')).append( ']');
          }
        if( !unexpected.isEmpty())
          {
          msg.append( ", unexpected=").append( '[').append( StringUtils.join( unexpected, ',')).append( ']');
          }

        fail( msg.toString());
        }
      }
    }

  /**
   * Reports a failure if the actual Collection contains a different set of values than the expected array.
   */
  public static <T> void assertSetEquals( String label, T[] expected, Collection<T> actual)
    {
    List<T> expectedList = expected == null ? null : Arrays.asList( expected);
    assertSetEquals( label, expectedList, actual);
    }

  /**
   * Reports a failure if the actual collection contains a different set of values than the expected array.
   */
  public static <T> void assertSetEquals( String label, T[] expected, Iterator<T> actual)
    {
    List<T> expectedList = expected == null ? null : Arrays.asList( expected);
    List<T> actualList = actual == null ? null : IteratorUtils.toList( actual);
    assertSetEquals( label, expectedList, actualList);
    }

  /**
   * Reports a failure if the actual Iterator contains a different sequence of values than the expected List.
   */
  public static <T> void assertSeqEquals( String label, List<T> expected, Iterator<T> actual)
    {
    if( (expected == null) !=(actual == null))
      {
      fail( label
            + " expected: "
            + (expected == null ? null : "[" + StringUtils.join( expected, ",") + "]")
            + ", but was: "
            + (actual == null ? null : "[" + StringUtils.join( IteratorUtils.toArray( actual), ",")
               + "]"));
      }

    else if( expected != null)
      {
      StringBuilder labelBuilder = new StringBuilder();
      int i;
      for (i = 0; i < expected.size() && actual.hasNext(); i++)
        {
        assertEquals
          ( labelBuilder.delete( 0, labelBuilder.length())
            .append( label).append( " [").append( i).append( "]").toString(),
            expected.get( i),
            actual.next());
        }

      if( i < expected.size())
        {
        fail
          ( labelBuilder.delete( 0, labelBuilder.length())
            .append( " too small, starting at index=").append( i)
            .append( ", missing=[").append( StringUtils.join( expected.toArray(), ",", i, expected.size())).append( "]")
            .toString());
        }
      else if( actual.hasNext())
        {
        fail
          ( labelBuilder.delete( 0, labelBuilder.length())
            .append( " too big, starting at index=").append( i)
            .append( ", unexpected=[").append( StringUtils.join( IteratorUtils.toArray( actual), ",")).append( "]")
            .toString());
        }
      }
    }

  /**
   * Reports a failure if the actual List contains a different sequence of values than the expected List.
   */
  public static <T> void assertSeqEquals( String label, List<T> expected, List<T> actual)
    {
    Iterator<T> actualIterator = actual == null ? null : actual.iterator();
    assertSeqEquals( label, expected, actualIterator);
    }

  /**
   * Reports a failure if the actual Iterator contains a different sequence of values than the expected array.
   */
  public static <T> void assertSeqEquals( String label, T[] expected, Iterator<T> actual)
    {
    List<T> expectedList = expected == null ? null : Arrays.asList( expected);
    assertSeqEquals( label, expectedList, actual);
    }

  /**
   * Reports a failure if the actual List contains a different sequence of values than the expected array.
   */
  public static <T> void assertSeqEquals( String label, T[] expected, List<T> actual)
    {
    List<T> expectedList = expected == null ? null : Arrays.asList( expected);
    assertSeqEquals( label, expectedList, actual);
    }
  }
@@ -1832,32 +1832,400 @@
  %7D%0A      %7D%0A    %7D
+%0A  /**%0A   * Reports a failure if the actual Collection contains a different set of values than the expected Collection.%0A   */%0A  public static %3CT%3E void assertSetEquals( String label, Collection%3CT%3E expected, Iterator%3CT%3E actual)%0A    %7B%0A    List%3CT%3E actualList = actual == null ? null : IteratorUtils.toList( actual);%0A    assertSetEquals( label, expected, actualList);%0A    %7D
 %0A%0A  /**%0A   * Rep
@@ -2864,88 +2864,8 @@
 d);%0A
-    List%3CT%3E actualList = actual == null ? null : IteratorUtils.toList( actual);%0A
     
@@ -2900,36 +2900,32 @@
 ctedList, actual
-List
 );%0A    %7D%0A%0A  /**%0A

Add assertSetEquals( String, Collection, Iterator)