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

com.fitbur.assertj.error.ShouldContainExactlyInAnyOrder Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.error;

import com.fitbur.assertj.internal.*;

import static com.fitbur.assertj.error.ShouldContainExactlyInAnyOrder.ErrorType.*;
import static com.fitbur.assertj.util.IterableUtil.*;

/**
 * Creates an error message indicating that an assertion that verifies a group of elements contains exactly a given set
 * of values and nothing else failed. A group of elements can be a collection, an array or a {@code String}.
 * 
 * @author Lovro Pandzic
 */
public class ShouldContainExactlyInAnyOrder extends BasicErrorMessageFactory {

  /**
   * Creates a new {@link ShouldContainExactlyInAnyOrder}.
   *
   * @param actual the actual value in the failed assertion.
   * @param expected values expected to be contained in {@code actual}.
   * @param notFound values in {@code expected} not found in {@code actual}.
   * @param notExpected values in {@code actual} that were not in {@code expected}.
   * @param comparisonStrategy the {@link ComparisonStrategy} used to evaluate assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldContainExactlyInAnyOrder(Object actual, Object expected, Iterable notFound,
                                                                   Iterable notExpected, ComparisonStrategy comparisonStrategy) {
    if (isNullOrEmpty(notExpected)) {
      return new ShouldContainExactlyInAnyOrder(actual, expected, notFound, NOT_FOUND_ONLY, comparisonStrategy);
    }

    if (isNullOrEmpty(notFound)) {
      return new ShouldContainExactlyInAnyOrder(actual, expected, notExpected, NOT_EXPECTED_ONLY, comparisonStrategy);
    }

    return new ShouldContainExactlyInAnyOrder(actual, expected, notFound, notExpected, comparisonStrategy);
  }

  private ShouldContainExactlyInAnyOrder(Object actual, Object expected, Iterable notFound, Iterable notExpected,
                                         ComparisonStrategy comparisonStrategy) {
    super("%n" +
          "Expecting:%n" +
          "  <%s>%n" +
          "to contain exactly in any order:%n" +
          "  <%s>%n" +
          "elements not found:%n" +
          "  <%s>%n" +
          "and elements not expected:%n" +
          "  <%s>%n%s", actual,
          expected, notFound, notExpected, comparisonStrategy);
  }

  private ShouldContainExactlyInAnyOrder(Object actual, Object expected, Iterable notFoundOrNotExpected, ErrorType errorType,
                                         ComparisonStrategy comparisonStrategy) {
    // @format:off
    super("%n" +
          "Expecting:%n" +
          "  <%s>%n" +
          "to contain exactly in any order:%n" +
          "  <%s>%n" + (errorType == NOT_FOUND_ONLY ?
          "but could not find the following elements:%n" : "but the following elements were unexpected:%n") +
          "  <%s>%n%s",
          actual, expected, notFoundOrNotExpected, comparisonStrategy);
    // @format:on
  }

  public enum ErrorType {
    NOT_FOUND_ONLY, NOT_EXPECTED_ONLY
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy