All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testng.internal.junit.ArrayComparisonFailure Maven / Gradle / Ivy

There is a newer version: 7.10.2
Show newest version
package org.testng.internal.junit;

import java.util.ArrayList;
import java.util.List;

/**
 * Thrown when two array elements differ
 */
public class ArrayComparisonFailure extends AssertionError {

    private static final long serialVersionUID= 1L;
    
    private final List fIndices= new ArrayList<>();
    private final String fMessage;
    private final AssertionError fCause;

    /**
     * Construct a new ArrayComparisonFailure with an error text and the array's
     * dimension that was not equal
     * @param cause the exception that caused the array's content to fail the assertion test 
     * @param index the array position of the objects that are not equal.
     */
    public ArrayComparisonFailure(String message, AssertionError cause, int index) {
        fMessage= message;
        fCause= cause;
        addDimension(index);
    }

    public void addDimension(int index) {
        fIndices.add(0, index);
    }

    @Override
    public String getMessage() {
        StringBuilder builder= new StringBuilder();
        if (fMessage != null)
            builder.append(fMessage);
        builder.append("arrays first differed at element ");
        for (int each : fIndices) {
            builder.append("[");
            builder.append(each);
            builder.append("]");
        }
        builder.append("; ");
        builder.append(fCause.getMessage());
        return builder.toString();
    }
    
    /**
     * {@inheritDoc}
     */
    @Override public String toString() {
        return getMessage();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy