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

com.fitbur.assertj.error.ShouldContainOnlyKeys 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 static com.fitbur.assertj.util.IterableUtil.isNullOrEmpty;

import com.fitbur.assertj.internal.ComparisonStrategy;
import com.fitbur.assertj.internal.StandardComparisonStrategy;

/**
 * Creates an error message indicating that an assertion that verifies map contains only a given set of keys and
 * nothing else failed.
 * 
 * @author Joel Costigliola
 */
public class ShouldContainOnlyKeys extends BasicErrorMessageFactory {

  /**
   * Creates a new {@link ShouldContainOnlyKeys}.
   * 
   * @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}.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldContainOnlyKeys(Object actual, Object expected, Object notFound,
	                                                      Object notExpected) {
	return new ShouldContainOnlyKeys(actual, expected, notFound, notExpected, StandardComparisonStrategy.instance());
  }

  /**
   * Creates a new {@link ShouldContainOnlyKeys}.
   * 
   * @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}.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldContainOnlyKeys(Object actual, Object expected, Object notFound,
	                                                      Iterable notExpected) {
	if (isNullOrEmpty(notExpected)) {
	  return new ShouldContainOnlyKeys(actual, expected, notFound, StandardComparisonStrategy.instance());
	}
	return new ShouldContainOnlyKeys(actual, expected, notFound, notExpected, StandardComparisonStrategy.instance());
  }

  private ShouldContainOnlyKeys(Object actual, Object expected, Object notFound, Object notExpected,
	                            ComparisonStrategy comparisonStrategy) {
	super("%n" +
	      "Expecting:%n" +
	      "  <%s>%n" +
	      "to contain only following keys:%n" +
	      "  <%s>%n" +
	      "keys not found:%n" +
	      "  <%s>%n" +
	      "and keys not expected:%n" +
	      "  <%s>%n%s", actual,
	      expected, notFound, notExpected, comparisonStrategy);
  }

  private ShouldContainOnlyKeys(Object actual, Object expected, Object notFound, ComparisonStrategy comparisonStrategy) {
	super("%n" +
	      "Expecting:%n" +
	      "  <%s>%n" +
	      "to contain only following keys:%n" +
	      "  <%s>%n" +
	      "but could not find the following keys:%n" +
	      "  <%s>%n%s",
	      actual, expected, notFound, comparisonStrategy);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy