package org.assertj.db.api;

import static org.assertj.core.error.ShouldBeEqual.shouldBeEqual;
import static org.assertj.db.error.ShouldBeTypeOfAny.shouldBeTypeOfAny;
import static org.assertj.db.error.ShouldHaveColumnsSize.shouldHaveColumnsSize;
import static org.assertj.db.util.Values.areEqual;

import java.util.Arrays;
import java.util.List;

import org.assertj.core.api.WritableAssertionInfo;
import org.assertj.core.internal.Failures;
import org.assertj.db.error.AssertJDBException;
import org.assertj.db.type.AbstractDbData;
import org.assertj.db.type.Row;

/**
 * Assertion methods about the data in a <code>{@link Row}</code>.
 * 
 * @author Régis Pouiller
 * 
 * @param <E> The class of the actual value (an sub-class of {@link AbstractDbData}).
 * @param <D> The class of the original assert (an sub-class of {@link AbstractDbAssert}).
 * @param <C> The class of the equivalent row assert (an sub-class of {@link AbstractColumnAssert}).
 * @param <CV> The class of the equivalent row assertion on the value (an sub-class of {@link AbstractColumnValueAssert}).
 * @param <R> The class of this assert (an sub-class of {@link AbstractRowAssert}).
 * @param <RV> The class of this assertion on the value (an sub-class of {@link AbstractRowValueAssert}).
 */
public abstract class AbstractRowAssert<E extends AbstractDbData<E>, D extends AbstractDbAssert<E, D, C, CV, R, RV>, C extends AbstractColumnAssert<E, D, C, CV, R, RV>, CV extends AbstractColumnValueAssert<E, D, C, CV, R, RV>, R extends AbstractRowAssert<E, D, C, CV, R, RV>, RV extends AbstractRowValueAssert<E, D, C, CV, R, RV>>
    extends AbstractSubAssert<E, D, R, RV, C, CV, R, RV> {

  /**
   * Row on which do the assertion.
   */
  private Row row;

  /**
   * To notice failures in the assertion.
   */
  private static Failures failures = Failures.instance();

  /**
   * Constructor.
   * 
   * @param originalDbAssert The original assert. That could be a {@link RequestAssert} or a {@link TableAssert}.
   * @param selfType Class of this assert (the sub assert) : a sub-class of {@code AbstractRowAssert}.
   * @param valueType Class of the assert on the value : a sub-class of {@code AbstractRowValueAssert}.
   */
  AbstractRowAssert(D originalDbAssert, Class<R> selfType, Class<RV> valueType, Row row) {
    super(originalDbAssert, selfType, valueType);
    this.row = row;
  }

  /** {@inheritDoc} */
  @Override
  protected List<Object> getValuesList() {
      return row.getValuesList();
  }

  /**
   * Returns assertion methods on the value corresponding to the column name in parameter.
   * 
   * @param columnName The column name.
   * @return An object to make assertions on the value.
   * @throws NullPointerException If the column name in parameter is null.
   * @throws AssertJDBException If there is no column with this name.
   */
  public RV value(String columnName) {
    if (columnName == null) {
      throw new NullPointerException("Column name must be not null");
    }
    List<String> columnsNameList = row.getColumnsNameList();
    int index = columnsNameList.indexOf(columnName.toUpperCase());
    if (index == -1) {
      throw new AssertJDBException("Column <%s> does not exist", columnName);
    }
    return getValueAssertInstance(index);
  }


  /** {@inheritDoc} */
  @Override
  protected void assertHasSize(WritableAssertionInfo info, int expected) {
    List<String> columnsNameList = row.getColumnsNameList();
    int size = columnsNameList.size();
    if (size != expected) {
      throw failures.failure(info, shouldHaveColumnsSize(size, expected));
    }
  }

  public R haveValuesEqualTo(Object... expected) {
    hasSize(expected.length);
    int index = 0;
    for (Object value : getValuesList()) {
      ValueType[] possibleTypes = ValueType.getPossibleTypesForComparison(expected[index]);
      ValueType type = ValueType.getType(value);
      if (!Arrays.asList(possibleTypes).contains(type)) {
        throw failures.failure(info, shouldBeTypeOfAny(value, type, possibleTypes));
      }
      if (!areEqual(value, expected[index])) {
        throw failures.failure(info, shouldBeEqual(getValuesList(), expected, info.representation()));
      }
      index++;
    }
    return myself;
  }
}

@@ -3573,16 +3573,553 @@
  %7D%0A  %7D%0A%0A
+  /**%0A   * Verifies that the values of a column are equal to values in parameter.%0A   * %3Cp%3E%0A   * Example where the assertion verifies that the values in the first %7B@code Row%7D of the %7B@code Table%7D are equal to%0A   * the values in parameter :%0A   * %3C/p%3E%0A   * %0A   * %3Cpre%3E%0A   * assertThat(table).row().haveValuesEqualTo(1, %22Text%22, TimeValue.of(9, 1));%0A   * %3C/pre%3E%0A   * %0A   * @param expected The expected values.%0A   * @return %7B@code this%7D assertion object.%0A   * @throws AssertionError If the value is not equal to the values in parameter.%0A   */%0A
   public

Add comments